spring5增加了reactive web模块,相应的在spring security中也增加了 [webflux-web-security] 模块,相对于spring security 在配置和使用方面有略微的差异,下面主要说明简单的配置和自定义用户信息的配置。
1、基本配置
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
@Configuration
@EnableWebFluxSecurity
public class WebFluxSecurityConfig{
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange()
//.pathMatchers("/loginPage").permitAll() //无需进行权限过滤的请求路径
.anyExchange().authenticated()
.and()
.httpBasic().and()
.formLogin()
//.loginPage("/loginPage") //自定义的登陆页面
;
return http.build();
}
}
以上配置为官方文档给出的基本配置,在项目启动的会有一串加密信息
Using generated security password: 38bb8fb7-fd82-4bfc-8243-8e23a5ab879c
就是项目默认的用户名是:user,密码为上面这一串信息
这里写图片描述
2、自定义用户信息
上面的配置只有一个用户信息,如何使用数据库中的用户信息呢? [官方文档] 给出的默认配置如下
@EnableWebFluxSecurity
public class HelloWebfluxSecurityConfig {
@Bean
public MapReactiveUserDetailsService userDetailsService() {
UserDetails user = User.withDefaultPasswordEncoder()
.username("user")
.password("user")
.roles("USER")
.build();
return new MapReactiveUserDetailsService(user);
}
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange()
.anyExchange().authenticated()
.and()
.httpBasic().and()
.formLogin();
return http.build();
}
}
在上述方面中User.withDefaultPasswordEncoder()已经被标注为@Deprecated,并且这种方式在项目启动的时候把用户给定死了仍不够灵活。
在spring-webflux-security中提供了一个接口 ReactiveUserDetailsService,官方文档给出的MapReactiveUserDetailsService就是实现这个接口,所以我们依样画葫芦实现这个接口就能使用数据库中的用户信息了。
public interface ReactiveUserDetailsService {
/**
* Find the {@link UserDetails} by username.
* @param username the username to look up
* @return the {@link UserDetails}. Cannot be null
*/
Mono
}
具体代码如下
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import com.jokerchen.reactiveWeb.service.UserService;
import reactor.core.publisher.Mono;
public class WebReactiveUserDetailsService
implements ReactiveUserDetailsService{
@Autowired
private UserService userService;
@Override
public Mono
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
com.jokerchen.reactiveWeb.entity.User defUser = userService.findUserByUsername(username);
UserDetails user = User.withUsername(defUser.getUsername())
.password(encoder.encode(defUser.getPassword()))
.roles(defUser.getRole())
.build();
return Mono.just(user);
}
}