@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Profile("!test")
class SecurityConfiguration(private val issuersOAuth2Properties: IssuersOAuth2Properties) {
@Bean
@Throws(Exception::class)
fun filterChain(http: HttpSecurity): SecurityFilterChain? {
http.authorizeRequests().antMatchers(HttpMethod.OPTIONS).permitAll()
http.authorizeRequests().antMatchers("/swagger-ui.html").permitAll()
http.authorizeRequests().antMatchers("/auth").permitAll()
http.authorizeRequests().antMatchers("/actuator**").permitAll()
http.authorizeRequests().antMatchers("/swagger-ui.html", "/swagger-ui/**").permitAll()
http.csrf().disable()
//.cors{cors->cors.disable()}
http.authorizeRequests().antMatchers("/**").authenticated().and().oauth2ResourceServer{oauth2 ->
oauth2.authenticationManagerResolver(jwtIssuerAuthManager())
}.authorizeRequests().and().cors().disable()
return http.build()
}
@Bean
fun corsConfigurer(): WebMvcConfigurer? {
return object : WebMvcConfigurer {
override fun addCorsMappings(registry: CorsRegistry) {
registry.addMapping("/**").allowedMethods("*").allowedHeaders("*").allowCredentials(true)
}
}
}
@Bean
fun jwtIssuerAuthManager():AuthenticationManagerResolver<HttpServletRequest?>?{
return JwtIssuerAuthenticationManagerResolver(IssuerManager(issuersOAuth2Properties.issuers))
}
}
Kotlin用never版本的Spring做cors new。我没有找到任何工作的例子。用这个我得到一个错误:https://docs.spring.io/spring-security/reference/reactive/integrations/cors.html
应用程序启动失败
说明:
无法注册在类路径资源[me/xxx/xxx/config/SecurityConfiguration.class]中定义的Bean“springSecurityFilterChain”。已在类路径资源[org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]中定义了具有该名称的Bean,并且已禁用覆盖。
动作:
请考虑重命名其中一个Bean,或通过设置spring.main.allow-bean-definition-overriding=true来启用覆盖
1条答案
按热度按时间eblbsuwk1#
从错误消息中,我发现您的项目中可能声明了多个同名的
SecurityFilterChain
bean。您可以使用
@Bean(name = "youNewBeanName")
将其中一个Bean重命名为其他名称,或者只是简单地重命名函数名称。至于你的CORS,只需在你的
WebMvcConfigurer
中添加addAllowedOrigin("*")
就行了。但是,建议您创建
CorsConfigurationSource
source类型的Bean,这是一种更好的做法。你好
希望能有所帮助。
干杯干杯干杯