对预检请求的响应未通过访问控制检查:它没有HTTP正常状态

时间:2019-01-19 21:42:04

标签: spring spring-boot spring-mvc cors spring-config

我正在尝试为Spring和Angular实现针对CORS问题的解决方案。到目前为止,我设法创建了此配置:

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class SimpleCORSFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

        chain.doFilter(req, res);
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void destroy() {
    }

}

但是我得到Access to XMLHttpRequest at 'http://123.123.123.123:8080/api/oauth/token' from origin 'http://123.123.123.123' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

您知道如何添加HTTP ok状态吗?

PS。我尝试过的第二种方式:

@Configuration
@EnableResourceServer
public class ResourceSecurityConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.resourceId("resource_id").stateless(true);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/users/**").permitAll()
                .anyRequest().authenticated()
                .and()
        .cors().disable()
        .authorizeRequests()
        .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
        .anyRequest()
        .fullyAuthenticated()
        .and()
        .httpBasic()
        .and()
        .csrf().disable();
    }

    @Bean
    public CorsConfigurationSource corsConfigurationSources() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"));
        configuration.setAllowedHeaders(Arrays.asList("authorization", "content-type", "x-auth-token"));
        configuration.setExposedHeaders(Arrays.asList("x-auth-token"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}

使用第二种配置,我得到has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status. auth:1 Failed to load resource: the server responded with a status of 404 (Not Found)

达到此结果的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

很少评论:

  1. 要解决您的问题,您应该使用response.setStatus(200);
  2. 您当前的实现未使用Spring。它使用Java过滤器。 Spring具有内置的CORS处理工具,例如,请参见this
  3. 由于您接受所有来源,因此您当前的实施方式并不安全,并且容易受到“ CORS攻击”的影响。这使攻击者可以代表您的用户执行操作并读取其个人信息。为防止这种情况,您应检查来源是否与您的来源白名单相符

答案 1 :(得分:0)

您已通过配置.cors().disable()在ResourceSecurityConfig中禁用了跨源资源共享。尝试删除disable()部分,然后离开.cors()部分。这将在您应用程序的安全性部分中启用CORS,并使它在预检请求中返回OK状态。