Search in sources :

Example 1 with UserDetail

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());
}
Also used : UserDetail(com.albedo.java.common.security.service.UserDetail) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 2 with UserDetail

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;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UserDetail(com.albedo.java.common.security.service.UserDetail) UserOnlineDo(com.albedo.java.modules.sys.domain.UserOnlineDo) HttpSession(javax.servlet.http.HttpSession) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) UserAgent(cn.hutool.http.useragent.UserAgent)

Example 3 with UserDetail

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));
        }
    }
}
Also used : UserDetail(com.albedo.java.common.security.service.UserDetail) SessionInformation(org.springframework.security.core.session.SessionInformation) SysUserOnlineEvent(com.albedo.java.common.security.event.SysUserOnlineEvent) UserOnlineDo(com.albedo.java.modules.sys.domain.UserOnlineDo) Authentication(org.springframework.security.core.Authentication) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet)

Example 4 with UserDetail

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));
}
Also used : UserDetail(com.albedo.java.common.security.service.UserDetail) LogLoginDo(com.albedo.java.modules.sys.domain.LogLoginDo) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) AuthenticationException(org.springframework.security.core.AuthenticationException)

Aggregations

UserDetail (com.albedo.java.common.security.service.UserDetail)4 UserOnlineDo (com.albedo.java.modules.sys.domain.UserOnlineDo)2 UserAgent (cn.hutool.http.useragent.UserAgent)1 SysUserOnlineEvent (com.albedo.java.common.security.event.SysUserOnlineEvent)1 LogLoginDo (com.albedo.java.modules.sys.domain.LogLoginDo)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1 AuthenticationException (org.springframework.security.core.AuthenticationException)1 SessionInformation (org.springframework.security.core.session.SessionInformation)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1