use of com.albedo.java.common.security.service.UserDetail in project albedo by somowhere.
the class TokenProvider method getAuthentication.
public Authentication getAuthentication(String token) {
Claims claims = getClaimsFromToken(token);
UserDetail userDetail = (UserDetail) userDetailsService.loadUserByUsername((String) claims.get(PRINCIPAL));
return new UsernamePasswordAuthenticationToken(userDetail, token, userDetail.getAuthorities());
}
use of com.albedo.java.common.security.service.UserDetail in project albedo by somowhere.
the class LoginUtil method getUserOnline.
public static UserOnlineDo getUserOnline(Authentication authentication) {
UserOnlineDo online = new UserOnlineDo();
HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
HttpSession session = request.getSession(false);
UserDetail user = SecurityUtil.getUser(authentication);
online.setSessionId(String.valueOf(session.getId()));
online.setDeptId(user.getDeptId());
online.setDeptName(user.getDeptName());
online.setUserId(user.getId());
online.setUsername(user.getUsername());
online.setStartTimestamp(LocalDateTimeUtil.of(session.getCreationTime()));
online.setLastAccessTime(LocalDateTimeUtil.of(session.getLastAccessedTime()));
online.setExpireTime((long) session.getMaxInactiveInterval());
online.setIpAddress(WebUtil.getIp(request));
online.setIpLocation(AddressUtil.getRegion(online.getIpAddress()));
online.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
UserAgent userAgent = UserAgentUtil.parse(online.getUserAgent());
online.setBrowser(userAgent.getBrowser().getName());
online.setOs(userAgent.getOs().getName());
return online;
}
use of com.albedo.java.common.security.service.UserDetail in project albedo by somowhere.
the class RedisSessionRegistry method registerNewSession.
@Override
public void registerNewSession(String sessionId, Object principal) {
ArgumentAssert.notEmpty(sessionId, "SessionId required as per interface contract");
ArgumentAssert.notNull(principal, "Principal required as per interface contract");
ArgumentAssert.isTrue(principal instanceof UserDetail, "Principal required as UserDetail");
if (log.isDebugEnabled()) {
log.debug("Registering session " + sessionId + ", for principal " + principal);
}
UserDetail userDetail = (UserDetail) principal;
if (getSessionInformation(sessionId) != null) {
removeSessionInformation(sessionId);
}
SessionInformation sessionInformation = new CustomSessionInformation(userDetail.getId(), sessionId, new Date());
redisTemplate.boundHashOps(getSessionIdsKey()).put(sessionId, sessionInformation);
Set<String> sessionsUsedByPrincipal = getPrincipals(userDetail.getId());
if (sessionsUsedByPrincipal == null) {
sessionsUsedByPrincipal = new CopyOnWriteArraySet();
Set<String> prevSessionsUsedByPrincipal = this.putIfAbsentPrincipals(userDetail.getId(), sessionsUsedByPrincipal);
if (prevSessionsUsedByPrincipal != null) {
sessionsUsedByPrincipal = prevSessionsUsedByPrincipal;
}
}
sessionsUsedByPrincipal.add(sessionId);
if (log.isTraceEnabled()) {
log.trace("Sessions used by '" + principal + "' : " + sessionsUsedByPrincipal);
}
Authentication authentication = SecurityUtil.getAuthentication();
if (authentication != null && authentication.isAuthenticated()) {
UserOnlineDo userOnlineDo = userOnlineService.getById(sessionId);
if (userOnlineDo == null) {
userOnlineDo = LoginUtil.getUserOnline(authentication);
SpringContextHolder.publishEvent(new SysUserOnlineEvent(userOnlineDo));
}
}
}
use of com.albedo.java.common.security.service.UserDetail in project albedo by somowhere.
the class AjaxAuthenticationFailureHandler method onAuthenticationFailure.
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {
String username = request.getParameter("username");
LoginUtil.isValidateCodeLogin(username, true, false);
String message = exception instanceof BadCredentialsException && "Bad credentials".equals(exception.getMessage()) ? "密码填写错误!" : exception.getMessage();
LogLoginDo logLoginDo = SysLogUtils.getSysLogLogin();
logLoginDo.setParams(HttpUtil.toParams(request.getParameterMap()));
logLoginDo.setUsername(username);
try {
UserDetail userDetails = (UserDetail) userDetailsService.loadUserByUsername(username);
if (userDetails != null) {
logLoginDo.setCreatedBy(userDetails.getId());
}
} catch (Exception e) {
log.debug("can not find createId by username[{}]", username);
}
logLoginDo.setTitle("用户登录失败");
logLoginDo.setDescription(message);
AsyncUtil.recordLogLogin(logLoginDo);
response.setStatus(HttpServletResponse.SC_OK);
WebUtil.renderJson(response, Result.buildFail(message));
}
Aggregations