spring security webflux 使用
官网:https://docs.spring.io/spring-security/site/docs/5.3.2.RELEASE/reference/html5/#reactive-applications
*********************
示例
***************
config 层
WebFluxSecurityConfig
@Configuration
@EnableWebFluxSecurity
public class WebFluxSecurityConfig {
@Bean
public MapReactiveUserDetailsService initMapReactiveUserDetailsService(){
UserDetails userDetails= User.builder().username("gtlx")
.passwordEncoder(initPasswordEncoder()::encode)
.password("123456")
.authorities("ROLE_USER")
.build();
return new MapReactiveUserDetailsService(userDetails);
}
@Bean
public SecurityWebFilterChain initSecurityWebFilterChain(ServerHttpSecurity http){
http.formLogin().and().authorizeExchange()
.pathMatchers("/hello").hasAuthority("ROLE_USER")
.pathMatchers("/**").permitAll();
return http.build();
}
@Bean
public PasswordEncoder initPasswordEncoder(){
return new BCryptPasswordEncoder();
}
}
***************
controller 层
HelloController
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(Principal principal){
return "hello "+principal.getName();
}
}
*********************
使用测试
localhost:8080/hello

认证通过后,输出:hello gtlx
|