Код: Выделить всё
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
Мобильная версия