【发布时间】:2020-09-17 01:01:57
【问题描述】:
我有以下实现 WebMvcConfigurer 接口并覆盖 addCorsMappings 方法的配置类。 (如此处所述:https://www.baeldung.com/spring-cors)
@Configuration
//@EnableWebMvc <- I tried with and without, no effect
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
}
我还有一个扩展 WebSecurityConfigurerAdapter 的配置类:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private CustomUserDetailsService customService = new CustomUserDetailsService();
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors().and()
.csrf().disable()
.authorizeRequests()
.antMatchers("/user/signup*").permitAll()
.and()
.authorizeRequests()
.antMatchers("/auth*").permitAll()
.and()
.authorizeRequests()
.antMatchers("/nummern/**").permitAll()
.and()
.authorizeRequests()
.antMatchers("/information/").hasRole("OWNER")
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.addFilter(new JwtAuthenticationFilter(authenticationManager()))
.addFilter(new JwtAuthorizationFilter(authenticationManager()))
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
每当我使用 Ajax 向授权端点发出 HTTP 请求时,都会收到以下错误:
从源“http://localhost:3000”访问“http://localhost:8080/auth?username=Example&password=Example”处的 XMLHttpRequest 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。
由于这种方法没有效果,我也尝试了 Spring Security 5 文档中的方法,但也没有用:
https://docs.spring.io/spring-security/site/docs/5.0.5.RELEASE/reference/htmlsingle/#cors
我不确定这是由于我的 CORS 配置还是某些 CSRF 配置(就我而言,CSRF 应该由于我的 HttpSecurity 配置而被禁用):
2020-05-29 13:52:40.377 DEBUG 21056 --- [nio-8080-exec-1] o.s.security.web.FilterChainProxy : /auth?username=Example&password=Example at position 3 of 15 in additional filter chain; firing Filter: 'HeaderWriterFilter'
2020-05-29 13:52:40.378 DEBUG 21056 --- [nio-8080-exec-1] o.s.security.web.FilterChainProxy : /auth?username=Example&password=Example at position 4 of 15 in additional filter chain; firing Filter: 'CsrfFilter'
2020-05-29 13:52:40.381 DEBUG 21056 --- [nio-8080-exec-1] o.s.security.web.csrf.CsrfFilter : Invalid CSRF token found for http://localhost:8080/auth?username=Example&password=Example
2020-05-29 13:52:40.382 DEBUG 21056 --- [nio-8080-exec-1] o.s.s.w.header.writers.HstsHeaderWriter : Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@4ea0d6af
2020-05-29 13:52:40.382 DEBUG 21056 --- [nio-8080-exec-1] w.c.HttpSessionSecurityContextRepository : SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
我已经为旧 Spring Security 版本尝试了各种其他建议的解决方案和 CORS 配置,但到目前为止,它们都没有对我的问题产生任何影响。
这似乎是类似问题的最新解决方案,但对我也不起作用:
编辑: 我尝试对 Firefox(和各种 HTTP 客户端)使用 CORS 禁用扩展,但除了被重定向到 /login 端点之外,我看不到任何差异,我还尝试使用以下方法禁用:
http.httpBasic().disable()
我的 Spring 应用程序在所有测试用例中仍然抛出与上述相同的错误。
编辑#2: 我也试过了:
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://localhost:3000");
}
};
}
【问题讨论】:
标签: java spring spring-boot