мое бэкэнд-приложение в весенней загрузке и защищено ssl. Я использовал вход в Facebook OAuth2. Также внешнее приложение в Angular 7 и защищено ssl. Моя проблема заключается в отправке запросов Angular в мое приложение Spring boot. Все приложения https.
P.S. Все работает, если я добавляю URL-адрес в webSecurity.ignoring(). и не защитить мой бэкэнд. я думаю, что какая-то проблема с безопасностью и HTTPS-запросами. СПАСИБО ЗА ПОМОЩЬ.
Бэкэнд
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);
}
}
ВНЕШНИЙ ИНТЕРФЕЙС
SomeComponent.html ‹ кнопка (щелчок)="makeRequest()"> СДЕЛАТЬ ЗАПРОС ‹ /button >
SomeComponent.ts
val:any = {};
makeRequest(){
this.http.get("https://localhost:8443/api/products").subscribe(value => {this.val = value; console.log(this.val.key)});
}
ОШИБКА
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
. Попробуйте поискать еще. Я предлагаю поискать, как использоватьwebpack proxy
, чтобы передняя и задняя части выглядели так, как будто они работают на одном порту. Аннотация@CrossOrigin(...)
, которую вы использовали, не является лучшей практикой. - person Amir Choubani   schedule 19.03.2019Access-Control-Allow-Origin
. С решением, которое вы предложили, вы сказали весенней безопасности, эй, игнорируйте любой запрос типа get и post ect, поступающий извне на URL-адрес /api/** - person Amir Choubani   schedule 19.03.2019