【问题标题】:Cors Error when using CorsFilter and spring security使用 CorsFilter 和 spring security 时出现 Cors 错误
【发布时间】:2021-05-09 15:30:33
【问题描述】:

我正在使用 Spring Boot 构建 API 服务。它使用基本身份验证进行身份验证。当客户端尝试连接 API 时,会收到 CORS 错误

在 Spring Boot 上,它会抛出错误

java.lang.IllegalArgumentException: 当 allowCredentials 为真时, allowedOrigins 不能包含特殊值“*”,因为它不能 在“Access-Control-Allow-Origin”响应标头上设置。允许 一组来源的凭据,明确列出它们或考虑 改用“allowedOriginPatterns”。

我试图找到 allowedOriginPatterns 用法的示例,但尚未找到。即使是它的文档-https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/CorsRegistration.html#allowedOriginPatterns-java.lang.String ...我仍然不知道我必须在 config.allowedOriginPatterns();

中放入什么模式

下面是我的 CorsFilter 代码,

@Configuration
public class RequestCorsFilter {

    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.setAllowedOrigins(Collections.singletonList("*"));
        config.setAllowedHeaders(Arrays.asList("Origin", "Content-Type", "Accept", "responseType", "Authorization"));
        config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH"));
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }      

}

这是我的验证码,

@Configuration
@EnableWebSecurity
public class AuthenConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth)
        throws Exception {
    auth
    .inMemoryAuthentication()
    .withUser("thor").password("{noop}P@ssw00rd")
    .authorities("USER");
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        String[] AUTH_WHITELIST = {
            // -- swagger ui
            "/v2/api-docs", 
            "/swagger-resources/**", 
            "/configuration/ui",
            "/configuration/security", 
            "/swagger-ui.html",
            "/webjars/**"
        };

        http
            .csrf().disable()
            .authorizeRequests()
            .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
            .antMatchers(AUTH_WHITELIST).permitAll() // whitelist URL permitted
            .antMatchers("/api").authenticated(); // others need auth
    }

}

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    使用config.setAllowedOriginPatterns("*") 而不是config.setAllowedOrigins(Collections.singletonList("*"));

    【讨论】:

    • 谢谢,它确实有效。但是你有一个错字,setAllowedOriginPatternsList 作为参数。您可能指的是“setAllowedOriginPattern”(单数形式),或者他们已将其更改为较新的版本。
    • setAllowedOriginPatterns 也接受 String 参数。我没有找到您所指的单数形式 setAllowedOriginPattern
    • 根据official documentation(以及我的代码),此方法有单数和复数形式。无论哪种方式,如果其他人面临同样的问题,都值得一提。也许我们指的是不同的版本?
    • 根据您提供的文档,没有单数形式的setAllowedOriginPattern,而是添加模式addAllowedOriginPattern。我想你看错了名字。
    • 确实,我的错,但我仍然找不到接受String 作为参数的setAllowedOriginPatterns。但config.setAllowedOriginPatterns(Arrays.asList("*")) 工作正常。
    【解决方案2】:
    config.setAllowedOrigins(Collections.singletonList("*"));
    

    必须更改此行。您应该列出所有应该可以访问您的应用程序的服务器。

    例如你使用 Angular,所以前端的开发服务器是 http://localhost:4200。你的生产服务器是https://you.server.domain.com

    那么你的配置列表应该是这样的

    config.setAllowedOrigins(List.of("http://localhost:4200","https://you.server.domain.com"));
    

    【讨论】:

      【解决方案3】:

      如果项目使用4000端口,yml配置

      allowedOrigins:
        - "http://localhost:4000"
      

      http://localhost:4000 替换 *
      春季文档:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#cors-configuration

      【讨论】:

        【解决方案4】:

        当您想在源中使用通配符“*”时,这不能设置为 true。

        config.setAllowCredentials(true);
        

        所以删除它

        【讨论】:

          猜你喜欢
          • 2017-11-21
          • 2023-01-12
          • 1970-01-01
          • 2023-03-26
          • 1970-01-01
          • 2023-02-16
          • 1970-01-01
          • 2020-10-04
          • 2015-06-20
          相关资源
          最近更新 更多