Почему приложение весенней загрузки не может найти bean-компонент ServerHttpSecurity?JAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Почему приложение весенней загрузки не может найти bean-компонент ServerHttpSecurity?

Сообщение Anonymous »

У меня класс,

Код: Выделить всё

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.server.SecurityWebFilterChain;
import reactor.core.publisher.Mono;

import java.net.URI;

@Configuration
@EnableReactiveMethodSecurity
public class SecurityConfig {

@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
return http
.authorizeExchange(exchanges ->
exchanges
.pathMatchers("/", "/home").permitAll()
.pathMatchers("/sinke-resources").authenticated()
.anyExchange().authenticated()
)
.oauth2Login(oauth2Login ->
oauth2Login
.authenticationSuccessHandler((webFilterExchange, authentication) ->
Mono.fromRunnable(() ->
webFilterExchange.getExchange().getResponse()
.getHeaders().setLocation(URI.create("/sinke-resources")))
.then())
.authenticationFailureHandler((webFilterExchange, exception) ->
Mono.fromRunnable(() ->
webFilterExchange.getExchange().getResponse()
.getHeaders().setLocation(URI.create("/failed-auth")))
.then())
)
.build();
}

@Bean
public UserDetailsService userDetailsService() {
UserDetails user = User.builder()
.username("user")
.password(passwordEncoder().encode("password"))
.roles("USER")
.build();

return new InMemoryUserDetailsManager(user);
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}

Когда я запускаю приложение, я получаю сообщение,

Код: Выделить всё

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method securityWebFilterChain in org.sinke.oauth2.config.SecurityConfig required a bean of type 'org.springframework.security.config.web.server.ServerHttpSecurity' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.security.config.web.server.ServerHttpSecurity' in your configuration.

Я прочитал текст
, удалил @EnableReactiveMethodSecurity и добавил @EnableWebFluxSecurity
так что мой код стал,

Код: Выделить всё

@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {

@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
return http
.authorizeExchange(exchanges ->
exchanges
.pathMatchers("/", "/home").permitAll()
.pathMatchers("/sinke-resources").authenticated()
.anyExchange().authenticated()
)
.oauth2Login(oauth2Login ->
oauth2Login
.authenticationSuccessHandler((webFilterExchange, authentication) ->
Mono.fromRunnable(() ->
webFilterExchange.getExchange().getResponse()
.getHeaders().setLocation(URI.create("/sinke-resources")))
.then())
.authenticationFailureHandler((webFilterExchange,  exception) ->
Mono.fromRunnable(() ->
webFilterExchange.getExchange().getResponse()
.getHeaders().setLocation(URI.create("/failed-auth")))
.then())
)
.build();
}

@Bean
public UserDetailsService userDetailsService() {
UserDetails user = User.builder()
.username("user")
.password(passwordEncoder().encode("password"))
.roles("USER")
.build();

return new InMemoryUserDetailsManager(user);
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}

и у меня была ошибка,

Код: Выделить всё

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'conversionServicePostProcessor', defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/security/config/annotation/web/reactive/WebFluxSecurityConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

Почему я делаю неправильно?

Подробнее здесь: https://stackoverflow.com/questions/786 ... urity-bean
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»