【问题标题】:Spring security addCorsMappings has no effectSpring security addCorsMappings 没有效果
【发布时间】:2020-10-30 11:50:12
【问题描述】:

我正在使用 angular 5.0.5 和 spring boot 2.0.2.RELEASE

angular 在 localhost:4200 上运行 spring 在 localhost:8181 上运行

试图在 spring security 中配置 cors,所以我在 WebSecurityConfigurerAdapter 中启用了 cors

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and()
                .csrf().disable()
                .authorizeRequests().antMatchers(authorizedPaths).permitAll()
                .anyRequest().authenticated()
                .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and().addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
    }

并且我使用 WebMvcConfigurer 进行 cors 配置,但即使我输入假原点并将 httpMethod 限制为仅 DELETE 我的 Angular 应用程序仍然可以访问后端并且不会像我没有任何一样被拒绝cors配置

@Configuration
@EnableWebMvc
public class WebMvcConfigurerImpl implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://fake-domain.com")
                .allowedMethods("DELETE")
                .allowedHeaders("header1", "header2", "header3")
                .exposedHeaders("header1", "header2")
                .allowCredentials(true)
                .maxAge(3600);
    }
}

即使使用邮递员,我仍然可以使用 HTTP 请求到达后端 在发布之前尝试让它工作已经有一段时间了,所以我有点绝望

【问题讨论】:

  • 以下建议对您有帮助吗?
  • 使用注解和 CorsRegistry 确保您没有在 ymls 中使用不同配置在多个地方配置 CORS
  • 我删除了我的 WebMvcConfigurer,并遵循了答案中建议的解决方案,我现在拥有的唯一配置是 CorsConfigurationSource,即使使用假的来源、方法和标头,一切仍然可以访问.

标签: angular spring spring-security cors


【解决方案1】:

您可以在扩展 WebSecurityConfigurerAdapter

的同一类中创建 CorsConfigurationSource bean
@Bean
CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(asList("*"));
    configuration.setAllowedMethods(asList("HEAD",
            "GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
    // setAllowCredentials(true) is important, otherwise:
    // The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
    configuration.setAllowCredentials(true);
    // setAllowedHeaders is important! Without it, OPTIONS preflight request
    // will fail with 403 Invalid CORS request
    configuration.setAllowedHeaders(
            asList("Authorization", "Cache-Control", "Content-Type", "Access-Control-Allow-Origin",
                    "Access-Control-Expose-Headers", "Access-Control-Allow-Headers"));
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);
    return source;
}

请参考https://github.com/techiesantosh/taskmanager-service/blob/develop/src/main/java/com/web/taskmanager/config/TaskConfig.java

【讨论】:

  • 我按照链接添加了 corsConfigurationSource 并再次添加了虚假的来源、有限的方法和虚假的标头,但我仍然可以访问后端并使用 Web 服务。我想知道当后端和前端在 localhost 时是否考虑了 cors 配置
猜你喜欢
  • 2020-07-11
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 2012-02-03
  • 2021-01-16
  • 2018-12-25
  • 2019-05-22
相关资源
最近更新 更多