Search in sources :

Example 1 with UserAgent

use of cn.hutool.http.useragent.UserAgent in project RuoYi-Flowable-Plus by KonBAI-Q.

the class UserActionListener method doLogin.

/**
 * 每次登录时触发
 */
@Override
public void doLogin(String loginType, Object loginId, SaLoginModel loginModel) {
    UserType userType = UserType.getUserType(loginId.toString());
    if (userType == UserType.SYS_USER) {
        UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
        String ip = ServletUtils.getClientIP();
        LoginUser user = LoginHelper.getLoginUser();
        String tokenValue = StpUtil.getTokenValueByLoginId(loginId);
        UserOnlineDTO dto = new UserOnlineDTO();
        dto.setIpaddr(ip);
        dto.setLoginLocation(AddressUtils.getRealAddressByIP(ip));
        dto.setBrowser(userAgent.getBrowser().getName());
        dto.setOs(userAgent.getOs().getName());
        dto.setLoginTime(System.currentTimeMillis());
        dto.setTokenId(tokenValue);
        dto.setUserName(user.getUsername());
        dto.setDeptName(user.getDeptName());
        RedisUtils.setCacheObject(Constants.ONLINE_TOKEN_KEY + tokenValue, dto, tokenConfig.getTimeout(), TimeUnit.SECONDS);
        log.info("user doLogin, useId:{}, token:{}", loginId, tokenValue);
    } else if (userType == UserType.APP_USER) {
    // app端 自行根据业务编写
    }
}
Also used : UserOnlineDTO(com.ruoyi.common.core.domain.dto.UserOnlineDTO) UserAgent(cn.hutool.http.useragent.UserAgent) LoginUser(com.ruoyi.common.core.domain.model.LoginUser) UserType(com.ruoyi.common.enums.UserType)

Example 2 with UserAgent

use of cn.hutool.http.useragent.UserAgent in project ballcat by ballcat-projects.

the class LoginLogHandler method prodLoginLog.

/**
 * 根据token和请求信息产生一个登陆日志
 * @param source AbstractAuthenticationToken 当前token
 * @return LoginLog 登陆日志
 */
private LoginLog prodLoginLog(AbstractAuthenticationToken source) {
    // 获取 Request
    HttpServletRequest request = LogUtils.getHttpServletRequest();
    LoginLog loginLog = new LoginLog().setLoginTime(LocalDateTime.now()).setIp(IpUtils.getIpAddr(request)).setStatus(LogStatusEnum.SUCCESS.getValue()).setTraceId(MDC.get(LogConstant.TRACE_ID)).setUsername(source.getName());
    // 根据 ua 获取浏览器和操作系统
    UserAgent ua = UserAgentUtil.parse(request.getHeader("user-agent"));
    if (ua != null) {
        loginLog.setBrowser(ua.getBrowser().getName()).setOs(ua.getOs().getName());
    }
    return loginLog;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) LoginLog(com.hccake.ballcat.log.model.entity.LoginLog) UserAgent(cn.hutool.http.useragent.UserAgent)

Example 3 with UserAgent

use of cn.hutool.http.useragent.UserAgent in project RuoYi-Vue-Plus by JavaLionLi.

the class UserActionListener method doLogin.

/**
 * 每次登录时触发
 */
@Override
public void doLogin(String loginType, Object loginId, SaLoginModel loginModel) {
    UserType userType = UserType.getUserType(loginId.toString());
    if (userType == UserType.SYS_USER) {
        UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
        String ip = ServletUtils.getClientIP();
        LoginUser user = LoginHelper.getLoginUser();
        String tokenValue = StpUtil.getTokenValueByLoginId(loginId);
        UserOnlineDTO dto = new UserOnlineDTO();
        dto.setIpaddr(ip);
        dto.setLoginLocation(AddressUtils.getRealAddressByIP(ip));
        dto.setBrowser(userAgent.getBrowser().getName());
        dto.setOs(userAgent.getOs().getName());
        dto.setLoginTime(System.currentTimeMillis());
        dto.setTokenId(tokenValue);
        dto.setUserName(user.getUsername());
        dto.setDeptName(user.getDeptName());
        RedisUtils.setCacheObject(Constants.ONLINE_TOKEN_KEY + tokenValue, dto, tokenConfig.getTimeout(), TimeUnit.SECONDS);
        log.info("user doLogin, useId:{}, token:{}", loginId, tokenValue);
    } else if (userType == UserType.APP_USER) {
    // app端 自行根据业务编写
    }
}
Also used : UserOnlineDTO(com.ruoyi.common.core.domain.dto.UserOnlineDTO) UserAgent(cn.hutool.http.useragent.UserAgent) LoginUser(com.ruoyi.common.core.domain.model.LoginUser) UserType(com.ruoyi.common.enums.UserType)

Example 4 with UserAgent

use of cn.hutool.http.useragent.UserAgent 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 5 with UserAgent

use of cn.hutool.http.useragent.UserAgent in project albedo by somowhere.

the class SysLogUtils method getSysLogLogin.

public LogLoginDo getSysLogLogin() {
    HttpServletRequest request = RequestHolder.getHttpServletRequest();
    String userAgentStr = request.getHeader(HttpHeaders.USER_AGENT);
    UserAgent userAgent = UserAgentUtil.parse(userAgentStr);
    String tempIp = WebUtil.getIp(request);
    return LogLoginDo.builder().ipAddress(tempIp).ipLocation(AddressUtil.getRegion(tempIp)).username(getUsername()).createdBy(getUserId()).createdDate(LocalDateTime.now()).loginDate(LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.NORM_DATE_FORMATTER)).userAgent(userAgentStr).browser(userAgent.getBrowser().getName()).os(userAgent.getOs().getName()).requestUri(URLUtil.getPath(request.getRequestURI())).build();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UserAgent(cn.hutool.http.useragent.UserAgent)

Aggregations

UserAgent (cn.hutool.http.useragent.UserAgent)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 Browser (cn.hutool.http.useragent.Browser)2 OS (cn.hutool.http.useragent.OS)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 UserOnlineDTO (com.ruoyi.common.core.domain.dto.UserOnlineDTO)2 LoginUser (com.ruoyi.common.core.domain.model.LoginUser)2 UserType (com.ruoyi.common.enums.UserType)2 SysLogininfor (com.ruoyi.system.domain.SysLogininfor)2 Async (org.springframework.scheduling.annotation.Async)2 UserAgentUtil (cn.hutool.http.useragent.UserAgentUtil)1 LogActionDTO (cn.sliew.scaleph.log.service.dto.LogActionDTO)1 LogLoginDTO (cn.sliew.scaleph.log.service.dto.LogLoginDTO)1 ApplicationProperties (com.albedo.java.common.core.config.ApplicationProperties)1 AddressUtil (com.albedo.java.common.core.util.AddressUtil)1 WebUtil (com.albedo.java.common.core.util.WebUtil)1 UserDetail (com.albedo.java.common.security.service.UserDetail)1 RandomUtil (com.albedo.java.common.security.util.RandomUtil)1 LogOperateDo (com.albedo.java.modules.sys.domain.LogOperateDo)1 PersistentTokenDo (com.albedo.java.modules.sys.domain.PersistentTokenDo)1