【问题标题】:CORS Preflight not sent to serverCORS 预检未发送到服务器
【发布时间】:2022-01-19 04:18:08
【问题描述】:

我有一个使用 Spring Security 和 Cors 过滤器的 Spring Boot 应用程序。有一个CorsFilter 如下,我尝试提出一个跨源请求

Axios.get("http://10.0.120.11:30500/user", { withCredentials: true })
  .then((data) => console.log(data));

来自托管在 http://localhost:8080 的页面。但是,浏览器给出了 401。奇怪的是浏览器没有发送任何预检 (OPTIONS) 请求。

@Configuration
@EnableWebMvc
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        List<String> list = new ArrayList<>();
        list.add("*");

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.setAllowedHeaders(list);
        corsConfiguration.setAllowedMethods(list);
        corsConfiguration.setAllowedOriginPatterns(Arrays.asList("http://localhost:8080"));

        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }
}

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .cors()
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .csrf()
                .disable()
                .formLogin()
                .disable()
                .httpBasic()
                .disable()
                .exceptionHandling()
                .authenticationEntryPoint(new LoginAuthenticationEntryPoint())
                //.authenticationEntryPoint(new RestAuthenticationEntryPoint())
                .and()
                .authorizeRequests()
                .antMatchers("/",
                        "/error",
                        "/favicon.ico",
                        "/**/*.png",
                        "/**/*.gif",
                        "/**/*.svg",
                        "/**/*.jpg",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.js")
                .permitAll()
                .antMatchers("/login/**","/auth/**", "/oauth2/**")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .oauth2Login()
                .clientRegistrationRepository(getClientRegistrationRepository())
                .authorizationEndpoint()
                .authorizationRequestResolver(new CustomAuthorizationRequestResolver(getClientRegistrationRepository(),"/oauth2/authorize"))
                .baseUri("/oauth2/authorize")
                .authorizationRequestRepository(cookieAuthorizationRequestRepository())
                .and()
                .redirectionEndpoint()
                .baseUri("/oauth2/callback/*")
                .and()
                .tokenEndpoint()
                .accessTokenResponseClient(new CustomTokenResponseClient())
                .and()
                .userInfoEndpoint()
                .userService(customOAuth2UserService)
                .and()
                .successHandler(oAuth2AuthenticationSuccessHandler)
                .failureHandler(oAuth2AuthenticationFailureHandler);

        // Add our custom Token based authentication filter
        http.addFilterBefore(cookieAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
    }

【问题讨论】:

    标签: spring-boot spring-security cors preflight


    【解决方案1】:

    浏览器可能不想为GET 请求发送飞行前请求。

    此外,对于可能对服务器数据造成副作用的 HTTP 请求方法(特别是 GET 以外的 HTTP 方法,或具有某些 MIME 类型的 POST),规范要求浏览器“预检”请求,请求支持的方法使用 HTTP OPTIONS 请求方法从服务器发送请求,然后在服务器“批准”后发送实际请求。

    参考:Cross-Origin Resource Sharing (CORS)

    【讨论】:

    • 我使用与 dotnet 核心服务器相同的场景(带有 cookie 凭据的 GET 请求),它按预期工作。我的服务器配置有什么遗漏吗?
    • 不太可能,因为飞行前由浏览器/客户端处理。
    • 确实,有问题的请求是simple 一个(即它没有预检)。 401 必须源于某些授权问题。这不是 CORS 问题。
    • @serkanz 为了它而触发预检有什么意义?预检与否,您的 cookie 未附加问题出在其他地方。
    • 是的@jub0bs 我发现我的身份验证cookie没有samesite属性。为了使用 JavaScript 进行跨源请求,它必须是 samesite=none 。现在我可以让它工作了。感谢 cmets。
    猜你喜欢
    • 1970-01-01
    • 2015-12-01
    • 2016-10-15
    • 2020-01-24
    • 2015-10-19
    • 1970-01-01
    • 2014-12-31
    • 2015-09-29
    • 2015-09-21
    相关资源
    最近更新 更多