| | |
| | | // [1] 获取 username 和 password |
| | | String userName = (String) authentication.getPrincipal(); |
| | | String password = (String) authentication.getCredentials(); |
| | | log.info("用户数据查询======================================"); |
| | | // [2] 使用用户名从数据库读取用户信息 |
| | | SecurityUserDetails userDetails = this.loadUserDetailsService.loadUserByUsername(userName); |
| | | log.info("用户数据查询======================================:{}", userDetails); |
| | | // 判断是否被封禁 |
| | | // userDetails.setEnabled(userDetails.getStatus()); |
| | | // userDetails.setEnabled(userDetails.getState()); |
| | | // [3] 检查用户信息 |
| | | if (Objects.isNull(userDetails)) { |
| | | throw new UsernameNotFoundException("用户账户不存在"); |
| | | // } else if (userDetails.isEnabled() || userDetails.getState()) { |
| | | // throw new DisabledException(userName + " 用户已被禁用或删除,请联系管理员"); |
| | | } else if (!userDetails.isAccountNonExpired()) { |
| | | throw new AccountExpiredException(userName + " 账号已过期"); |
| | | } else if (!userDetails.isAccountNonLocked()) { |
| | | throw new LockedException(userName + " 账号已被锁定"); |
| | | } else if (!userDetails.isCredentialsNonExpired()) { |
| | | throw new LockedException(userName + " 凭证已过期"); |
| | | } |
| | | else if (userDetails.getState()) { |
| | | throw new DisabledException("用户已被禁用或删除,请联系管理员"); |
| | | } |
| | | // else if (!userDetails.isAccountNonExpired()) { |
| | | // throw new AccountExpiredException(userName + " 账号已过期"); |
| | | // } else if (!userDetails.isAccountNonLocked()) { |
| | | // throw new LockedException(userName + " 账号已被锁定"); |
| | | // } else if (!userDetails.isCredentialsNonExpired()) { |
| | | // throw new LockedException(userName + " 凭证已过期"); |
| | | // } |
| | | // [4] 数据库用户的密码,一般都是加密过的 |
| | | String encryptedPassword = userDetails.getPassword(); |
| | | |
| | |
| | | // 把 userDetails 作为 principal 的好处是可以放自定义的 UserDetails,这样可以存储更多有用的信息,而不只是 username, |
| | | // 默认只有 username,这里的密码使用数据库中保存的密码,而不是用户输入的明文密码,否则就暴露了密码的明文 |
| | | // 不暴露密码 |
| | | userDetails.setPassword(null); |
| | | // userDetails.setPassword(null); |
| | | UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails.getUsername(), null, null); |
| | | // 设置详情 |
| | | //String userInfo = JsonUtils.toJsonString(userDetails); |