spring security webflux 使用

论坛 期权论坛 脚本     
已经匿名di用户   2022-3-21 23:58   2378   0

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

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP