UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
AuthenticationContextHolder.setContext(authenticationToken);
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername 存储登录用户信息。
authentication = authenticationManager.authenticate(authenticationToken);
判断authentication是否通过,如果通过:
LoginUser loginUser = (LoginUser) authentication.getPrincipal();//获取登录用户信息
recordLoginInfo(loginUser.getUserId());
// 生成token
return tokenService.createToken(loginUser);
createToken方法中进行了把token值存入redis操作
public void refreshToken(LoginUser loginUser)
{
loginUser.setLoginTime(System.currentTimeMillis());
loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
// 根据uuid将loginUser缓存
String userKey = getTokenKey(loginUser.getToken());
redisCache.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
}
SpringSecurity AuthenticationManager 进行用户认证
最新推荐文章于 2024-04-29 15:10:45 发布