use of com.hb0730.boot.admin.security.model.LoginUser in project boot-admin by hb0730.
the class LogoutSuccessHandlerImpl method onLogoutSuccess.
@SneakyThrows
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
LOGGER.debug("logout success <<<<<");
User loginUser = tokenService.getLoginUser(request);
if (null != loginUser) {
tokenService.delLoginUser(request);
AsyncManager.me().execute(AsyncFactory.recordLoginLog(loginUser.getUsername(), StatusEnum.SUCCESS, "登出成功"));
}
Result<String> result = R.success("注销成功");
response.setStatus(200);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().print(JsonUtils.objectToJson(result));
}
use of com.hb0730.boot.admin.security.model.LoginUser in project boot-admin by hb0730.
the class RedisTokenServiceImpl method verifyAccessToken.
@Override
public void verifyAccessToken(HttpServletRequest request) {
User loginUser = getLoginUser(request);
if (Objects.nonNull(loginUser)) {
Date expireTime = loginUser.getExpireTime();
Date currentTime = DateUtils.now();
long refreshTime = TimeUnit.MILLISECONDS.convert(super.getProperties().getRefreshTime(), super.getProperties().getTimeUnit());
// 校验 刷新
if (expireTime.getTime() - currentTime.getTime() <= refreshTime) {
String accessToken = getAccessToken(request);
String accessTokenKey = getAccessTokenKey(accessToken);
stringRedisTemplate.opsForValue().set(accessTokenKey, loginUser.getToken(), super.getProperties().getExpireTime(), super.getProperties().getTimeUnit());
refreshAccessToken(loginUser);
}
}
}
use of com.hb0730.boot.admin.security.model.LoginUser in project boot-admin by hb0730.
the class UerOnlineServiceImpl method getOnline.
/**
* 获取在线缓存用户信息
*
* @return 缓存用户
*/
private List<UserOnlineDTO> getOnline() {
Map<String, UserDetails> online = tokenService.getOnline();
if (!CollectionUtils.isEmpty(online)) {
List<UserOnlineDTO> lists = Lists.newArrayList();
for (Map.Entry<String, UserDetails> detailsEntry : online.entrySet()) {
UserOnlineDTO dto = new UserOnlineDTO();
dto.setTokenId(detailsEntry.getKey());
User loginUser = (User) detailsEntry.getValue();
BeanUtil.copyProperties(loginUser, dto);
lists.add(dto);
}
return lists;
}
return Lists.newArrayList();
}
use of com.hb0730.boot.admin.security.model.LoginUser in project boot-admin by hb0730.
the class LoginServiceImpl method login.
@Nullable
public LoginUser login(@NonNull String username, @NonNull String password) {
Authentication authenticate = null;
try {
// see com.hb0730.boot.admin.security.service.UserDetailsServiceImpl#loadUserByUsername
authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
} catch (Exception e) {
if (e.getCause() instanceof com.hb0730.boot.admin.exceptions.UsernameNotFoundException) {
AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, StatusEnum.FAIL, "用户不存在"));
throw new LoginException(ResponseStatusEnum.USER_NAME_NOT_FONT, "用户不存在");
} else if (e instanceof BadCredentialsException) {
AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, StatusEnum.FAIL, "用户名或者密码错误"));
throw new LoginException(ResponseStatusEnum.USER_PASSWORD_ERROR, "用户名或者密码错误");
} else {
AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, StatusEnum.FAIL, e.getMessage()));
throw new LoginException(ResponseStatusEnum.USE_LOGIN_ERROR, "登录异常,请稍后尝试", e);
}
}
User user = (User) authenticate.getPrincipal();
String accessToken = tokenService.createAccessToken(user);
LoginUser loginUser = BeanUtil.toBean(user, LoginUser.class);
assert loginUser != null;
loginUser.setAccessToken(accessToken);
AsyncManager.me().execute(AsyncFactory.recordLoginLog(username, StatusEnum.SUCCESS, "登录成功"));
return loginUser;
}
use of com.hb0730.boot.admin.security.model.LoginUser in project boot-admin by hb0730.
the class AuthenticationTokenFilter method doFilterInternal.
@Override
protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull FilterChain filterChain) throws ServletException, IOException {
LOGGER.debug("authentication token<<<<");
User loginUser = tokenService.getLoginUser(request);
if (Objects.nonNull(loginUser) && Objects.isNull(SecurityUtils.getAuthentication())) {
tokenService.verifyAccessToken(request);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}
filterChain.doFilter(request, response);
}
Aggregations