基于Spring Boot3实现Spring Security6 + JWT + Redis实现登录、token身份认证。
- 用户从数据库中获取。
- 使用RESTFul风格的APi进行登录。
- 使用JWT生成token。
- 使用Redis进行登录过期判断。
- 所有的工具类和数据结构在源码中都有。
系列文章指路👉
系列文章-基于SpringBoot3创建项目并配置常用的工具和一些常用的类
项目源码👉
/shijizhe/boot-test
这张图大家已经估计已经看过很多次了。
实现登录认证的过程,其实就是对上述的类按照自己的需求进行自定义扩展的过程。具体不多讲了,别的文章里讲得比我透彻。
show you my code.
将用户密码使用BCrypt加密存储。
@PostMapping("/register") @Operation(summary = "register", description = "用户注册") public Object register(@RequestBody @Valid UserRegisterDTO userRegisterDTO) { YaUser userById = userService.getUserById(userRegisterDTO.getUserId()); if(Objects.nonNull(userById)){ return BaseResult.fail("用户id已存在"); } try { BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); YaUser yaUser = UserRegisterMapper.INSTANCE.registerToUser(userRegisterDTO); yaUser.setUserPassword(encoder.encode(userRegisterDTO.getUserPassword())); userService.insertUser(yaUser); return BaseResult.success("用户注册成功"); }catch (Exception e){ return BaseResult.fail("用户注册过程中遇到异常:" + e); } }
我们使用RESTFul风格的API来代替表单进行登录。这个接口只是提供一个Swagger调用登录接口的入口,实际逻辑由Filter控制。
拦截指定的登录请求,交给AuthenticationProvider处理。对Provider返回的登录结果进行处理。
/** ** 拦截登陆过滤器 *
* * @author Ya Shi * @since 2024/3/21 16:20 */ @Slf4j public class YaLoginFilter extends UsernamePasswordAuthenticationFilter { private final RedisUtils redisUtils; private final Long expiration; public YaLoginFilter(AuthenticationManager authenticationManager, RedisUtils redisUtils, Long expiration) { this.expiration = expiration; this.redisUtils = redisUtils; super.setAuthenticationManager(authenticationManager); super.setPostOnly(true); super.setFilterProcessesUrl("/auth/login"); super.setUsernameParameter("userId"); super.setPasswordParameter("userPassword"); } @SneakyThrows @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { log.info("YaLoginFilter authentication start"); // 数据是通过 RequestBody 传输 UserLoginDTO user = JSON.parseObject(request.getInputStream(), StandardCharsets.UTF_8, UserLoginDTO.class); return super.getAuthenticationManager().authenticate( new UsernamePasswordAuthenticationToken(user.getUserId(), user.getUserPassword()) ); } @Override protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) { log.info("YaLoginFilter authentication success: {}", authResult); // 如果验证成功, 就生成Token并返回 UserDetails userDetails = (UserDetails) authResult.getPrincipal(); String userId = userDetails.getUsername(); String token = JwtUtils.generateToken(userId); response.setHeader(TOKEN_HEADER, TOKEN_PREFIX + token); // 将token存入Redis中 redisUtils.set(REDIS_KEY_AUTH_TOKEN + userId, token, expiration); log.info("YaLoginFilter authentication end"); // 将UserDetails存入redis中 redisUtils.set(REDIS_KEY_AUTH_USER_DETAIL + userId, JSON.toJSONString(userDetails), 1, TimeUnit.DAYS); ServletUtils.renderResult(response, new BaseResult<>(ResultEnum.SUCCESS.code, "登陆成功")); log.info("YaLoginFilter authentication end"); } /** * 如果 attemptAuthentication 抛出 AuthenticationException 则会调用这个方法 */ @Override protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException { log.info("YaLoginFilter authentication failed: {}", failed.getMessage()); ServletUtils.renderResult(response, new BaseResult<>(ResultEnum.FAILED_UNAUTHORIZED.code, "登陆失败:" + failed.getMessage())); }
调用UserDetailsService查询用户的账户、权限信息与登录接口输入的账户、密码对比。认证通过则返回用户信息。
/** ** 自定义认证 *
* * @author Ya Shi * @since 2024/3/21 15:00 */ @Component public class YaAuthenticationProvider implements AuthenticationProvider { @Autowired YaUserDetailService userDetailService; @Autowired PasswordEncoder passwordEncoder; @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { // 获取用户输入的用户名和密码 String username = authentication.getName(); String password = authentication.getCredentials().toString(); UserDetails userDetails = userDetailService.loadUserByUsername(username); boolean matches = passwordEncoder.matches(password, userDetails.getPassword()); if(!matches){ throw new AuthenticationException("User password error."){}; } return new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities()); } @Override public boolean supports(Class> authentication) { return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication); } }
从数据库中查询出用户的信息,供AuthenticationProvider登录认证时使用。
/** ** 继承UserDetailsService,实现自定义登陆认证 *
* * @author Ya Shi * @since 2024/3/19 11:32 */ @Service public class YaUserDetailService implements UserDetailsService { @Autowired UserService userService; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { YaUser user = userService.getUserById(username); if(Objects.isNull(user)){ throw new UsernameNotFoundException("User not Found."); } Listroles = userService.listRoleById(username); List authorities = new ArrayList<>(roles.size()); roles.forEach( role -> authorities.add(new SimpleGrantedAuthority(role.getRoleId()))); return new User(username, user.getUserPassword(), authorities); } }
/** ** Spring Security 配置文件 *
* * @author Ya Shi * @since 2024/2/29 11:27 */ @Configuration @EnableWebSecurity // 开启网络安全注解 public class YaSecurityConfig { @Autowired private AuthenticationConfiguration authenticationConfiguration; @Autowired private RedisUtils redisUtils; @Value("${ya-app.auth.jwt.expiration:1800}") private Long expiration; @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http // 禁用basic明文验证 .httpBasic().disable() // 禁用csrf保护 .csrf().disable() // 禁用session .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) // 身份认证过滤器 .authenticationManager(authenticationManager(authenticationConfiguration)) .authenticationProvider(new YaAuthenticationProvider()) .authorizeHttpRequests(authorizeHttpRequests -> authorizeHttpRequests // 允许OPTIONS请求访问 .requestMatchers(HttpMethod.OPTIONS, "/**").permitAll() // 允许登录/注册接口访问 .requestMatchers(HttpMethod.POST, "/auth/login").permitAll() .requestMatchers(HttpMethod.POST, "/auth/register").permitAll() // 允许匿名接口访问 .requestMatchers("/anon/**").permitAll() // 允许swagger访问 .requestMatchers("/swagger-ui/**").permitAll() .requestMatchers("/doc.html/**").permitAll() .requestMatchers("/v3/api-docs/**").permitAll() .requestMatchers("/webjars/**").permitAll() .anyRequest().authenticated() ) .addFilterAt(new YaLoginFilter(authenticationManager(authenticationConfiguration), redisUtils, expiration), UsernamePasswordAuthenticationFilter.class) // 让校验Token的过滤器在身份认证过滤器之前 .addFilterBefore(new YaTokenFilter(redisUtils, expiration), YaLoginFilter.class) // 禁用默认登出页 .logout().disable(); return http.build(); } @Bean public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception { return config.getAuthenticationManager(); } @Bean public WebSecurityCustomizer webSecurityCustomizer() { return (web) -> web.ignoring() .requestMatchers("/webjars/**") .requestMatchers("/swagger-ui/**", "/doc.html/**", "/v3/api-docs/**") .requestMatchers("/anon/**"); } /** * 使用BCrypt加密密码 */ @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } }
*调用 SecurityContextHolder.getContext().setAuthentication()将用户信息存入Security上下文中,完成身份认证。
/** ** 每次请求过滤token *
* * @author Ya Shi * @since 2024/3/21 16:52 */ @Slf4j public class YaTokenFilter extends OncePerRequestFilter { private final RedisUtils redisUtils; private final Long expiration; private static final SetWHITE_LIST = Stream.of( "/auth/register", "/auth/login" ).collect(Collectors.toSet()); public YaTokenFilter(RedisUtils redisUtils, Long expiration) { this.redisUtils = redisUtils; this.expiration = expiration; } @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { log.info("YaTokenFilter doFilterInternal start"); final String authorization = request.getHeader(AuthConstants.TOKEN_HEADER); log.info("YaTokenFilter ya-auth-token: {}", authorization); // 白名单 if (WHITE_LIST.contains(request.getServletPath())) { chain.doFilter(request, response); return; } // 1.请求头中没有携带token if (StrUtil.isBlank(authorization)) { ServletUtils.renderResult(response, new BaseResult<>(ResultEnum.FAILED_UNAUTHORIZED)); return; } // 携带token final String token = authorization.replace(AuthConstants.TOKEN_PREFIX, ""); String userId; // 2.提供的token异常 try { userId = JwtUtils.extractUserId(token); }catch (Exception e){ log.error("YaTokenFilter doFilterInternal 解析jwt异常:{}", e.toString()); ServletUtils.renderResult(response, new BaseResult<>(ResultEnum.FAILED_UNAUTHORIZED.code, "凭证异常")); return; } String redisToken = redisUtils.getString(AuthConstants.REDIS_KEY_AUTH_TOKEN + userId); // 3.token过期 if(StrUtil.isBlank(redisToken)){ ServletUtils.renderResult(response, new BaseResult<>(ResultEnum.FAILED_UNAUTHORIZED.code, "登录已过期,请重新登录过期。")); return; } // 4.提供的token是合法的,但是redis中的token又被使用登录功能重新刷新了一下,导致不一致。 if(!Objects.equals(redisToken, token)){ ServletUtils.renderResult(response, new BaseResult<>(ResultEnum.FAILED_UNAUTHORIZED.code, "账号在别处登陆。")); return; } // token续期 redisUtils.set(REDIS_KEY_AUTH_TOKEN + userId, token, expiration); // 获取用户信息和权限 String userDetailStr = redisUtils.getString(AuthConstants.REDIS_KEY_AUTH_USER_DETAIL + userId); UserDetails userDetails; if(Objects.isNull(userDetailStr)){ userDetails = yaUserDetailService().loadUserByUsername(userId); redisUtils.set(REDIS_KEY_AUTH_USER_DETAIL + userId, JSON.toJSONString(userDetails), 1, TimeUnit.DAYS); }else{ userDetails = initUser(userDetailStr); } SecurityContextHolder.getContext().setAuthentication( new UsernamePasswordAuthenticationToken( userDetails, userDetails.getPassword(), userDetails.getAuthorities() ) ); log.info("YaTokenFilter doFilterInternal end"); chain.doFilter(request, response); } private YaUserDetailService yaUserDetailService(){ return SpringContextUtils.getBean(YaUserDetailService.class); } private User initUser(String userJsonStr){ JSONObject userJson = JSON.parseObject(userJsonStr); String userId = userJson.getString("username"); JSONArray authArray = userJson.getJSONArray("authorities"); List authorities = new ArrayList<>(authArray.size()); for(int i=0; i< authArray.size();i++){ JSONObject authObj = authArray.getJSONObject(i); authorities.add(new SimpleGrantedAuthority(authObj.getString("authority"))); } return new User(userId, "[PROTECTED]", authorities); } }
已经登录的用户,可以从Security的上下文中获取用户的账号、基本信息、权限等。可以将其封装为工具类。因为练手的用户表较为简单,也没有部分、员工、角色、权限等概念,因此仅封装了getUserId做抛砖引玉的作用。可以根据实际使用自己封装更多的方法。
public static String getUserId() { if (Objects.isNull(SecurityContextHolder.getContext().getAuthentication())) { return null; } UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); if (Objects.isNull(userDetails)) { return null; } return userDetails.getUsername(); }
JWT本身是无状态的,但是我们后端将jwt存到redis里,相当于手动使JWT变得有状态了。那么我们在登出时就需要清空Redis中的jwt。
/** ** 登出成功 *
* * @author Ya Shi * @since 2024/3/28 10:47 */ @Slf4j public class YaLogoutSuccessHandler implements LogoutSuccessHandler { private final RedisUtils redisUtils; public YaLogoutSuccessHandler(RedisUtils redisUtils) { this.redisUtils = redisUtils; } @Override public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { final String authorization = request.getHeader(AuthConstants.TOKEN_HEADER); // 1.请求头中没有携带token if (StrUtil.isBlank(authorization)) { ServletUtils.renderResult(response, BaseResult.successWithMessage("没有登录信息,无需退出")); return; } // 携带token final String token = authorization.replace(AuthConstants.TOKEN_PREFIX, ""); String userId; // 2.提供的token异常 try { userId = JwtUtils.extractUserId(token); }catch (Exception e){ log.error("YaLogoutHandler logout 解析jwt异常:{}", e.toString()); ServletUtils.renderResult(response, new BaseResult<>(ResultEnum.FAILED_UNAUTHORIZED.code, "凭证异常")); return; } // 清空Redis redisUtils.delete(REDIS_KEY_AUTH_TOKEN + userId); log.info("YaLogoutSuccessHandler onLogoutSuccess"); ServletUtils.renderResult(response, BaseResult.successWithMessage("退出登录成功")); } }
@Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http ... // 前面的配置忽略 .logout().logoutUrl("/auth/logout").logoutSuccessHandler(new YaLogoutSuccessHandler(redisUtils)); return http.build(); }