Search in sources :

Example 1 with LoginLog

use of com.hccake.ballcat.log.model.entity.LoginLog 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 2 with LoginLog

use of com.hccake.ballcat.log.model.entity.LoginLog in project ballcat by ballcat-projects.

the class LoginLogHandler method onAuthenticationSuccessEvent.

/**
 * 登陆成功时间监听 记录用户登录日志
 * @param event 登陆成功 event
 */
@EventListener(AuthenticationSuccessEvent.class)
public void onAuthenticationSuccessEvent(AuthenticationSuccessEvent event) {
    AbstractAuthenticationToken source = (AbstractAuthenticationToken) event.getSource();
    Object details = source.getDetails();
    if (!(details instanceof HashMap)) {
        return;
    }
    // https://github.com/spring-projects-experimental/spring-authorization-server
    if ("password".equals(((HashMap) details).get("grant_type"))) {
        LoginLog loginLog = prodLoginLog(source).setMsg("登陆成功").setStatus(LogStatusEnum.SUCCESS.getValue()).setEventType(LoginEventTypeEnum.LOGIN.getValue());
        loginLogService.save(loginLog);
    }
}
Also used : AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) LoginLog(com.hccake.ballcat.log.model.entity.LoginLog) HashMap(java.util.HashMap) EventListener(org.springframework.context.event.EventListener)

Example 3 with LoginLog

use of com.hccake.ballcat.log.model.entity.LoginLog in project ballcat by ballcat-projects.

the class LoginLogHandler method onAuthenticationFailureEvent.

/**
 * 监听鉴权失败事件
 * @param event the event
 */
@EventListener(AbstractAuthenticationFailureEvent.class)
public void onAuthenticationFailureEvent(AbstractAuthenticationFailureEvent event) {
    AbstractAuthenticationToken source = (AbstractAuthenticationToken) event.getSource();
    LoginLog loginLog = prodLoginLog(source).setMsg(event.getException().getMessage()).setEventType(LoginEventTypeEnum.LOGIN.getValue()).setStatus(LogStatusEnum.FAIL.getValue());
    loginLogService.save(loginLog);
}
Also used : AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) LoginLog(com.hccake.ballcat.log.model.entity.LoginLog) EventListener(org.springframework.context.event.EventListener)

Example 4 with LoginLog

use of com.hccake.ballcat.log.model.entity.LoginLog in project ballcat by ballcat-projects.

the class LoginLogHandler method onLogoutSuccessEvent.

/**
 * On logout success event.
 * @param event the event
 */
@EventListener(LogoutSuccessEvent.class)
public void onLogoutSuccessEvent(LogoutSuccessEvent event) {
    AbstractAuthenticationToken source = (AbstractAuthenticationToken) event.getSource();
    LoginLog loginLog = prodLoginLog(source).setMsg("登出成功").setEventType(LoginEventTypeEnum.LOGOUT.getValue());
    loginLogService.save(loginLog);
}
Also used : AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) LoginLog(com.hccake.ballcat.log.model.entity.LoginLog) EventListener(org.springframework.context.event.EventListener)

Aggregations

LoginLog (com.hccake.ballcat.log.model.entity.LoginLog)4 EventListener (org.springframework.context.event.EventListener)3 AbstractAuthenticationToken (org.springframework.security.authentication.AbstractAuthenticationToken)3 UserAgent (cn.hutool.http.useragent.UserAgent)1 HashMap (java.util.HashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1