【问题标题】:Spring Security And Angular 6 HTTPS requestsSpring Security 和 Angular 6 HTTPS 请求
【发布时间】: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


【解决方案1】:

如下编辑您的主类并从控制器中删除所有@CrossOrigin。

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
@EnableOAuth2Sso
public class MongoTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(MongoTestApplication.class, args);
    }

 @SuppressWarnings("deprecation")
    @Bean
        public WebMvcConfigurer corsConfigurer()
        {
            return new WebMvcConfigurerAdapter() {
                @Override
                public void addCorsMappings(CorsRegistry registry) {
                    registry.addMapping("/**").allowedMethods("GET", "PUT", "POST", "DELETE", "OPTIONS");
                }    
            };
        }
}

【讨论】:

  • 我改变了我的课程,但结果相同。
猜你喜欢
  • 1970-01-01
  • 2023-01-11
  • 1970-01-01
  • 2013-02-10
  • 2015-10-04
  • 2019-04-08
  • 2017-02-04
  • 2016-08-31
  • 1970-01-01
相关资源
最近更新 更多