-
首先,添加拦截器LogInterceptor.java
package com.les.cas.interceptor; import com.les.cas.entity.SysLogonLog; import org.apache.commons.collections4.Predicate; import org.jasig.cas.CentralAuthenticationService; import org.jasig.cas.ticket.Ticket; import org.jasig.cas.ticket.TicketGrantingTicket; import org.jasig.cas.util.ApplicationContextProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.util.StringUtils; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; import java.sql.Date; import java.util.*; /** * Created with IDEA * author:zhangyan * Date:2019-05-24 * Time:14:59 */ public class LogInterceptor extends HandlerInterceptorAdapter { @Autowired private CentralAuthenticationService centralAuthenticationService; @Value("${sso.sessions.include.tgt:false}") private boolean includeTicketGrantingTicketId; /** * The enum Sso session attribute keys. */ private enum SsoSessionAttributeKeys { AUTHENTICATION_LOGID("authentication_logid"), AUTHENTICATION_USERID("authentication_userid"), AUTHENTICATION_USERNAME("authentication_username"), AUTHENTICATION_DEPTID("authentication_deptid"), AUTHENTICATION_DEPTNAME("authentication_deptname"), AUTHENTICATION_DATE("authentication_date"), TICKET_GRANTING_TICKET("ticket_granting_ticket"); private String attributeKey; /** * Instantiates a new Sso session attribute keys. * * @param attributeKey the attribute key */ SsoSessionAttributeKeys(final String attributeKey) { this.attributeKey = attributeKey; } @Override public String toString() { return this.attributeKey; } } /** * 登录操作日志记录 * * @param request * @param response * @param handler * @param ex */ public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { //判断是否为登录操作 if (request.getMethod().toLowerCase().equals("post") && request.getRequestURI().toLowerCase().contains("login")) { SysLogonLog SysLogonLog = new SysLogonLog(); getInfo(SysLogonLog, request, response, ex); saveLog(SysLogonLog); } } /** * 填充用户登录日志内容 * add by baoyidao 2019-05-25 * @param SysLogonLog * @param request * @param response */ public void getInfo(SysLogonLog SysLogonLog, HttpServletRequest request, HttpServletResponse response, Exception ex) { //获取session Collection<Map<String, Object>> ssoSessions = getSsoSessions(); Iterator<Map<String, Object>> iterator = ssoSessions.iterator(); Map<String, Object> next = iterator.next(); System.out.println("LogInterceptor : " + next); //用户信息(个人和部门) SysLogonLog.setId(stringIsNullToEmpty(next.get(SsoSessionAttributeKeys.AUTHENTICATION_LOGID.toString()))); SysLogonLog.setUserid(stringIsNullToEmpty(next.get(SsoSessionAttributeKeys.AUTHENTICATION_USERID.toString()))); SysLogonLog.setUsername(stringIsNullToEmpty(next.get(SsoSessionAttributeKeys.AUTHENTICATION_USERNAME.toString()))); SysLogonLog.setDeptid(stringIsNullToEmpty(next.get(SsoSessionAttributeKeys.AUTHENTICATION_DEPTID.toString()))); SysLogonLog.setDeptname(stringIsNullToEmpty(next.get(SsoSessionAttributeKeys.AUTHENTICATION_DEPTNAME.toString()))); //系统和浏览器信息 String userAgent = request.getHeader("user-agent"); String os = getOS(userAgent); SysLogonLog.setMac(os); // SysLogonLog.setBrowser(userAgent); //登录时间
CAS 4.1.5 添加登录及登出日志,基于拦截器(简陋的实现,自学用)
最新推荐文章于 2023-10-27 12:05:53 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)