Компонент ServerHttpSecurity не найден
У меня есть класс конфигурации безопасности, в котором есть bean-компонент SecurityWebFilterChain. Для этого bean-компонента требуется экземпляр ServerHttpSecuirty, но spring сообщает, что он не может найти никаких bean-компонентов этого типа, хотя он создан во внешней библиотеке (org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfiguration). Я видел эту проблему на странице github, и они сказали, попробуйте другую версию, но я использую весеннюю загрузку 2.4.5, поэтому она должна работать.
Мой класс конфигурации безопасности:
@Configuration
public class SecurityConfig {
@Bean
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http,
JwtTokenProvider tokenProvider,
ReactiveAuthenticationManager reactiveAuthenticationManager) {
final String TAG_SERVICES = "/api/**";
return http.csrf(ServerHttpSecurity.CsrfSpec::disable)
.httpBasic(ServerHttpSecurity.HttpBasicSpec::disable)
.authenticationManager(reactiveAuthenticationManager)
.securityContextRepository(NoOpServerSecurityContextRepository.getInstance())
.authorizeExchange(it -> it
.pathMatchers(HttpMethod.POST, TAG_SERVICES).hasAnyRole("USER","ADMIN")
.pathMatchers(HttpMethod.PUT, TAG_SERVICES).hasAnyRole("USER","ADMIN")
.pathMatchers(HttpMethod.GET, TAG_SERVICES).hasAnyRole("USER","ADMIN")
.pathMatchers(HttpMethod.DELETE, TAG_SERVICES).hasAnyRole("USER","ADMIN")
.pathMatchers(TAG_SERVICES).authenticated()
.anyExchange().permitAll()
)
.addFilterAt(new JwtTokenAuthenticationFilter(tokenProvider), SecurityWebFiltersOrder.HTTP_BASIC)
.build();
}
}
Мой класс приложения
@ConfigurationPropertiesScan
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class}) открытый класс TestPlatformBackendApplication {
public static void main(String[] args) {
SpringApplication.run(TestPlatformBackendApplication.class, args);
}
}
Компонент внешней библиотеки:
@Bean({"org.springframework.security.config.annotation.web.reactive.HttpSecurityConfiguration.httpSecurity"})
@Scope("prototype")
ServerHttpSecurity httpSecurity() {
ServerHttpSecurityConfiguration.ContextAwareServerHttpSecurity http = new ServerHttpSecurityConfiguration.ContextAwareServerHttpSecurity();
return http.authenticationManager(this.authenticationManager()).headers().and().logout().and();
}
1 ответ
Как рекомендовал @Toerktumlare, я добавил
@EnableWebFluxSecurity
в мою конфигурацию безопасности:
@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {
Но я также добавил следующее к моему исключению в
@SpringBootApplication
аннотация.
@ConfigurationPropertiesScan
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class})
public class TestPlatformBackendApplication {
public static void main(String[] args) {
SpringApplication.run(TestPlatformBackendApplication.class, args);
}
}