前言
前置阅读
spring boot security快速使用示例
spring boot security之前后端分离配置
说明
实际场景,我们一般是把用户信息保存在db中(也可能是调用三方接口),需要自定义用户信息加载或认证部分的逻辑,下面提供一个示例。
代码示例
定义用户bean
@AllArgsConstructor
@Data
public class User {private String username;private String password;
}
定义Mapper
示例,代码写死了,并不是实际从数据库或某个存储查询用户信息:
@Component
public class UserMapper {public User select(String username) {return new User(username, "pass");}
}
定义加载用户数据的类
UserDetailsService 是spring security内置的加载用户信息的接口,我们只需要实现这个接口:
@Slf4j
@Component
public class UserDetailsServiceImpl implements UserDetailsService {public static final UserDetails INVALID_USER =new org.springframework.security.core.userdetails.User("invalid_user", "invalid_password", Collections.emptyList());private final UserMapper userMapper;public UserDetailsServiceImpl(UserMapper userMapper) {this.userMapper = userMapper;}@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {// 根据用户名从数据库查询用户信息User user = userMapper.select(username);if (user == null) {/*** 如果没查询到这个用户,考虑两种选择:* 1. 返回一个标记无效用户的常量对象* 2. 返回一个不可能认证通过的用户*/return INVALID_USER;
// return new User(username, System.currentTimeMillis() + UUID.randomUUID().toString(), Collections.emptyList());}/*** 这里返回的用户密码是否为库里保存的密码,是明文/密文,取决于认证时密码比对部分的实现,每个人的场景不一样,* 因为使用的是不加密的PasswordEncoder,所以可以返回明文*/return new org.springframework.security.core.userdetails.User(username, user.getPassword(), Collections.emptyList());}
}
自定义认证的bean配置
@Configuration
public class WebConfiguration {@Beanpublic PasswordEncoder passwordEncoder() {// 示例,不对密码进行加密处理return NoOpPasswordEncoder.getInstance();}@Beanpublic AuthenticationManager authenticationManager(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {DaoAuthenticationProvider provider = new DaoAuthenticationProvider();// 设置加载用户信息的类provider.setUserDetailsService(userDetailsService);// 比较用户密码的时候,密码加密方式provider.setPasswordEncoder(passwordEncoder);return new ProviderManager(Arrays.asList(provider));}}
注意,因为这个是示例,AuthenticationProvider使用的是spring security的DaoAuthenticationProvider ,在实际场景中,如果不满足可以自定义实现或者继承DaoAuthenticationProvider ,重写其中的:additionalAuthenticationChecks方法,主要就是认证检查的,默认实现如下:
@Override@SuppressWarnings("deprecation")protected void additionalAuthenticationChecks(UserDetails userDetails,UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {if (authentication.getCredentials() == null) {this.logger.debug("Failed to authenticate since no credentials provided");throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));}String presentedPassword = authentication.getCredentials().toString();// 就是比对下请求传过来的密码和根据该用户查询的密码是否一致,passwordEncoder是根据不同的加密算法进行加密,示例我们用的是NoOpPasswordEncoder,也就是原始明文比对if (!this.passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {this.logger.debug("Failed to authenticate since password does not match stored value");throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));}}
定义登录接口
@RequestMapping("/login")
@RestController
public class LoginController {private final AuthenticationManager authenticationManager;public LoginController(AuthenticationManager authenticationManager) {this.authenticationManager = authenticationManager;}@PostMapping()public Object login(@RequestBody User user) {try {// 使用定义的AuthenticationManager进行认证处理Authentication authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword()));// 认证通过,设置到当前上下文,如果当前认证过程后续还有处理的逻辑需要的话。这个示例是没有必要了SecurityContextHolder.getContext().setAuthentication(authenticate);return "login success";}catch (Exception e) {return "login failed";}}/*** 获取验证码,需要的话,可以提供一个验证码获取的接口,在上面的login里把验证码传进来进行比对*/@GetMapping("/captcha")public Object captcha() {return "1234";}
}
自定义HttpSecurity
@Component
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {// 在这里自定义配置http.authorizeRequests()// 登录相关接口都允许访问.antMatchers("/login/**").permitAll().anyRequest().authenticated().and().exceptionHandling()// 认证失败返回401状态码,前端页面可以根据401状态码跳转到登录页面.authenticationEntryPoint((request, response, authException) ->response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase())).and().cors()// csrf是否决定禁用,请自行考量.and().csrf().disable()// 采用http 的基本认证..httpBasic();}
}
测试
示例中,用户密码写死是:pass,
用一个错误的密码试一下,响应登录失败:
使用正确的密码,响应登录成功: