二、Spring Reactive Security自定义登录页

添加配置类:

@Configuration
public class MyReactiveSecurityConfig {

@Bean
public ReactiveUserDetailsService reactiveUserDetailsService() {
UserDetails user = User.withUsername("user")
.password("12345")
.roles("USER")
.build();
return new MapReactiveUserDetailsService(user);
}

@Bean
public PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance();
}

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange(exchanges -> exchanges
.pathMatchers("/doLogin").permitAll()
.anyExchange().authenticated()
)
.formLogin()
.loginPage("/doLogin")
.and()
.csrf()
.disable();
return http.build();
}
}

loginPage是配置登录页面和登录接口的,我找了下api,没看到单独设置登录接口的api。同时pathMatchers("/doLogin").permitAll()设置doLogin允许任何人访问。

 

添加thymeleaf依赖:


org.springframework.boot
spring-boot-starter-thymeleaf

在application.properties配置thymeleaf:

spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
#设置thymeleaf页面的后缀
spring.thymeleaf.suffix=.html
#设置thymeleaf页面的存储路径
spring.thymeleaf.prefix=classpath:/templates/

 
在resources目录templates下新建doLogin.html:







Document















登录页面来自于:https://blog.csdn.net/bhegi_seg/article/details/123394504。注意:表单提交的地址是/doLogin

 
新增controller处理doLogin地址到页面的映射:

@Controller
public class LoginController {

@GetMapping("/doLogin")
public String login() {
return "doLogin";
}
}

posted @ 2023-06-05 20:54  shigp1  阅读(136)  评论(0)    收藏  举报