[英]Spring Boot cors configuration allows all sources
我正在尝试使前端只能放置从请求到后端的位置。 问题是它不起作用,并且每个请求都被调用。 下面是配置Spring Security和cors的代码。
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable().authorizeRequests()
.antMatchers(HttpMethod.POST, SIGN_UP_URL).permitAll()
.antMatchers(HttpMethod.GET, "/groups").permitAll()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.addFilter(new JWTAuthenticationFilter(authenticationManager()))
.addFilter(new JWTAuthorizationFilter(authenticationManager()))
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("http://192.168.0.16:8080"));
configuration.setAllowedMethods(Arrays.asList("GET", "POST"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
您可能想将OPTIONS
http方法添加到CORS
允许的方法中。 大多数前端语言在发送真实的语言之前就发送了OPTIONS
请求。 就像是
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "OPTIONS"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.