Java 如何在Spring Boot中禁用Spring Security中的CORS?
我想用SpringSecurity从SpringBoot(2.3.3.RELEASE)中完全删除CORS 对于Java 如何在Spring Boot中禁用Spring Security中的CORS?,java,spring,spring-boot,spring-security,cors,Java,Spring,Spring Boot,Spring Security,Cors,我想用SpringSecurity从SpringBoot(2.3.3.RELEASE)中完全删除CORS 对于HttpSecurity对象,有WebSecurityConfigurerAdapter\configure方法,我可以在其中调用cors().disable(),但它似乎不起作用 class MySecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void c
HttpSecurity
对象,有WebSecurityConfigurerAdapter\configure
方法,我可以在其中调用cors().disable()
,但它似乎不起作用
class MySecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().disable();
}
}
在上面的代码片段中,我从前端应用程序访问端点时仍然会遇到CORS错误
相反,我必须覆盖
webmvcconfiguer
界面中的addCorsMappings
,如下所示Hi您需要在spring boot项目中创建一个全局cors配置。创建一个类并用@configuration注释它。您可以遵循下面的示例
@Configuration
public class MyConfiguration {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("http://domain2.com")
.allowedMethods("PUT", "DELETE")
.allowedHeaders("header1", "header2", "header3")
.exposedHeaders("header1", "header2")
.allowCredentials(false).maxAge(3600);
}
};
}
}
以下是spring框架提供的完整指南如果没有配置,就无法启用CORS
http.cors().disable()
表示服务器不支持跨源资源共享。@dur表示它不支持什么?那么,它是否应该停止验证CORS相关的头文件?谢谢你解释我编辑了我的评论,它让人困惑。你的问题只是措辞上的问题。默认情况下不允许CORS,这意味着不允许共享。您必须通过配置启用CORS。我明白了,谢谢@dur感谢Isa,我已经在问题中给出了类似的示例:)