我在Heroku上开发了一个简单的后端。
我已尝试禁用csrf,如How to Solve 403 Error in Spring Boot Post Request接受的答案所建议的那样。@十字路口也添加到所有rest控制器中。
任何帮助都非常感谢。
发布于 2020-01-02 10:13:29
跨原点,有问题寄存器CrossFilter Bean,也可以使用Access-Control-Allow-Origin或注册站点,您可以检查以下链接here
@Configruation
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
public String crossOriginAllowedHeaders="header1,header2, *" ;
public String crossOriginAllowedSites="site1,site2, * ";
@Override
protected void configure(HttpSecurity http) throws Exception {
)
http.cors()
.and()
.csrf()
.disable();
............
...........
.and()
.headers()
.frameOptions()
.sameOrigin().addHeaderWriter((request,response)->{
response.setHeader("Cache-Control","no-cache, no-store, max-age=0, must-revalidate, private");
response.setHeader("Pragma","no-cache");
response.setHeader("Access-Control-Allow-Origin",this.crossOriginAllowedSites);
})
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
protected CorsFilter crossFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(false);
config.setAllowedHeaders(Arrays.asList(crossOriginAllowedHeaders.split(",")));
config.setAllowedOrigins(Arrays.asList(crossOriginAllowedSites.split(",")));
//config.setAllowedHeaders("*"); whitelist all sites
//config.setAllowedOrigins("*"); whitelist all headers
config.addAllowedMethod(HttpMethod.OPTIONS);
config.addAllowedMethod(HttpMethod.GET);
config.addAllowedMethod(HttpMethod.POST);
config.addAllowedMethod(HttpMethod.PUT);
config.addAllowedMethod(HttpMethod.DELETE);
config.addExposedHeader("Authorization");
config.setMaxAge(new Long(1800));
source.registerCorsConfiguration("/api/**", config);
source.registerCorsConfiguration(MANAGEMENT, config);
source.registerCorsConfiguration("/v2/api-docs", config);
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}我希望这能起作用
https://stackoverflow.com/questions/59559179
复制相似问题