I want to disable CORS for my frontend written in Angular, running locally on port 4200. I created a config but it doesn't seem to work.
我想禁用我的前端写在Angular的CORS,运行在本地端口4200上。我创建了一个配置,但它似乎不工作。
I get this error with Spring Security 6 after trying to start the application:
在尝试启动应用程序后,我在使用Spring Security6时收到以下错误:
Bean named 'corsFilter' is expected to be of type
'org.springframework.web.filter.CorsFilter' but was actually of type
'org.springframework.web.cors.UrlBasedCorsConfigurationSource'
I'm following this answer and this Spring guide.
我正在遵循这个答案和这本春季指南。
My pom.xml
公司简介
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>6.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>6.1.0</version>
</dependency>
My security config
我的安全配置
@Bean
public SecurityFilterChain configureSecurity(HttpSecurity http) throws Exception {
http
// ...
.cors(withDefaults());
return http.build();
}
@Bean
public CorsConfigurationSource corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.setAllowedOrigins(List.of("https://localhost:4200"));
config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return source;
}
What's wrong? I use bean of type CorsConfigurationSource
like they advice to me and Spring still wants CorsFilter
like it was in previous versions of Spring Security?
怎么了?我使用CorsConfigurationSource类型的Bean,就像他们给我的建议一样,而Spring仍然想要CorsFilter,就像在Spring Security的以前版本中一样?
更多回答
优秀答案推荐
Rename corsFilter
to corsConfigurationSource
.
CorscorsFilter到CorscorationSource。
更多回答
我是一名优秀的程序员,十分优秀!