【发布时间】: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