【发布时间】:2019-08-08 20:45:03
【问题描述】:
我在 Spring Boot 中的后端应用程序并使用 ssl 保护。我使用 OAuth2 facebook 登录。也是 Angular 7 中的前端应用程序并由 ssl 保护。我的问题是将请求 Angular 发送到我的 Spring Boot 应用程序。所有应用程序都是 https。
附:如果我将 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
SomeComponent.ts
val:any = {};
makeRequest(){
this.http.get("https://localhost:8443/api/products").subscribe(value => {this.val = value; console.log(this.val.key)});
}
错误
浏览器出错
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, …}
【问题讨论】:
-
您将 api 端的跨域设置为 192.168.1.106:4400,但从 localhost:8443/api/products 访问它。这些是完全不同的域名。尝试使用 IP 访问它(端口应该相同)。客户端和后端之间是否有代理服务器,因为您访问 8443 并希望它路由到端口 4400?
-
我尝试使用本地主机的所有应用程序。相同的结果。我觉得没关系。
-
用一个有效的解决方案来解决你遇到的同样的问题,试试这个答案stackoverflow.com/questions/40286549/spring-boot-security-cors。
-
我尽一切可能进行过滤。没有结果。只需给出
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource ...'之类的异常 -
Access-Control-Allow-Origin经常出错。尝试搜索更多。我建议搜索如何使用webpack proxy使正面和背面看起来像在同一个端口上工作。您使用的注释@CrossOrigin(...)不是最佳做法。
标签: java angular spring-boot cors spring-security-oauth2