【发布时间】:2019-08-05 00:42:10
【问题描述】:
我有一个 spring boot + spring security 应用程序,我允许 CORS(跨域)调用该应用程序。
我定义了以下 spring 安全配置,
http
.authorizeRequests()
.antMatchers( "/transaction/**").hasRole(SOME_ROLES)
.antMatchers( "/", "/anonymous/pay").permitAll()
.anyRequest().authenticated()
.and()
.csrf().disable()
.addFilterBefore(new StatelessLoginFilter(LOGIN_FILTER_URL, tokenAuthenticationService, authenticationManager()), UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService), UsernamePasswordAuthenticationFilter.class)
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).maximumSessions(1);
并且我在CORS下面添加了配置,
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/anonymous/pay")
.allowedOrigins("http://mydomain:8080")
.allowedMethods("PUT", "DELETE", "OPTIONS")
.allowedHeaders("header1", "header2", "header3")
.exposedHeaders("header1", "header2")
.allowCredentials(true).maxAge(3600);
// Add more mappings...
}
}
问题:
当从不同来源(跨源)在http://mydomain:8080/anonymous/pay 上发送请求时,过滤器仍会被调用,而且没有发送到实际/pay 调用的标头。请注意,在发送实际请求之前,Chrome 会向服务器发送 OPTIONS 调用。但是在实际请求中发送的/pay 中缺少标头。一旦触发StatelessAuthenticationFilter,HTTP 请求就不会携带在/pay 调用中发送的headers。
有什么见解吗?
【问题讨论】:
-
显示带有
OPTIONS调用和实际调用标头的请求和响应。例如浏览器中开发工具的屏幕截图 (F12)。
标签: spring-boot spring-mvc spring-security http-headers cross-domain