aplikasi backend saya di boot musim semi dan diamankan dengan ssl. Saya menggunakan login facebook OAuth2. Juga aplikasi frontend di Angular 7 dan diamankan dengan ssl. Masalah saya adalah mengirimkan permintaan Angular ke Aplikasi boot Spring saya. Semua aplikasi adalah https.
P.S. Semua berfungsi jika saya menambahkan url ke webSecurity.ignoring(). dan tidak mengamankan backend saya. menurut saya ada masalah dengan keamanan dan permintaan HTTPS. TERIMAKASIH ATAS BANTUANNYA.
BELAKANG
SecurityConfig.java
@RestController
@CrossOrigin(origins = "https://192.168.1.106:4400")
@Configuration
@Order(1000)
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserRepo userRepo;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors().and()
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/unauth/**").permitAll()
.antMatchers(HttpMethod.POST, "/unauth/upload").permitAll()
.antMatchers(HttpMethod.POST, "/api/**").authenticated()
.antMatchers(HttpMethod.PUT, "/api/**").authenticated()
.antMatchers(HttpMethod.DELETE, "/api/**").authenticated()
.antMatchers(HttpMethod.GET, "/api/**").authenticated()
.anyRequest().permitAll()
.and().logout().logoutSuccessUrl("/").permitAll();
}
@Override
public void configure(WebSecurity webSecurity) {
webSecurity.ignoring().antMatchers(HttpMethod.GET, "/unauth/**");
webSecurity.ignoring().antMatchers(HttpMethod.POST, "/unauth/**");
}
webSecurity.ignoring().antMatchers(HttpMethod.POST, "/unauth/**");
}
SomeRestController.java
@RestController
@CrossOrigin(origins = "https://192.168.1.106:4400")
@RequestMapping ("/api")
public class ProductService {
@Autowired
private ProductRepo productRepo;
@CrossOrigin(origins = "https://192.168.1.106:4400")
@GetMapping("/products")
public List<Product> getProducts(){
return productRepo.findAll();
}
SpringBootApplication.java
@SpringBootApplication
@EnableOAuth2Sso
@CrossOrigin(origins = {"https://192.168.1.106:4400"}, allowCredentials = "false")
public class MongoTestApplication {
public static void main(String[] args) {
SpringApplication.run(MongoTestApplication.class, args);
}
}
PALING DEPAN
SomeComponent.html ‹ tombol (klik)="makeRequest()"> BUAT PERMINTAAN ‹ /button >
SomeComponent.ts
val:any = {};
makeRequest(){
this.http.get("https://localhost:8443/api/products").subscribe(value => {this.val = value; console.log(this.val.key)});
}
KESALAHAN
error in browser
Access to XMLHttpRequest at 'https://localhost:8443/api/brands' from origin 'https://192.168.1.106:4400' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
core.js.pre-build-optimizer.js:15714 ERROR n {headers: t, status: 0, statusText: "Unknown Error", url: "https://localhost:8443/api/brands", ok: false, …}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource ...'
- person Ibrahim Rajabli   schedule 18.03.2019Access-Control-Allow-Origin
. Cobalah untuk mencari lebih banyak. Saya menyarankan untuk mencari cara menggunakanwebpack proxy
agar bagian depan dan belakang terlihat seperti bekerja pada port yang sama. Anotasi@CrossOrigin(...)
yang Anda gunakan bukan praktik terbaik. - person Amir Choubani   schedule 19.03.2019Access-Control-Allow-Origin
. Dengan solusi yang Anda sarankan, Anda berkata kepada keamanan pegas hei, abaikan semua permintaan jenis get dan post dll yang datang dari luar ke url /api/** - person Amir Choubani   schedule 19.03.2019