java记录用户登录及session失效时间

时间:2020-8-29 作者:admin

loginAction调用方法,新增一条记录,用来记录登录情况

        PrpsLoginLog log=new PrpsLoginLog();
		String sessionId=request.getSession().getId();
		log.setSessionId(sessionId);
		log.setIp(this.getIpAddress(request));
		log.setUserCode(loginCode);
		log.setLgointime(new Date());
		loginLogService.saveLoginLog(log);

public  String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		System.out.println("x-forwarded-for ip: " + ip);
		if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
			// 多次反向代理后会有多个ip值,第一个ip才是真实ip
			if( ip.indexOf(",")!=-1 ){
				ip = ip.split(",")[0];
			}
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
			System.out.println("Proxy-Client-IP ip: " + ip);
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
			System.out.println("WL-Proxy-Client-IP ip: " + ip);
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
			System.out.println("HTTP_CLIENT_IP ip: " + ip);
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
			System.out.println("HTTP_X_FORWARDED_FOR ip: " + ip);
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("X-Real-IP");
			System.out.println("X-Real-IP ip: " + ip);
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
			System.out.println("getRemoteAddr ip: " + ip);
		}
		if("0:0:0:0:0:0:0:1".equals(ip)){
			try {
				ip =  InetAddress.getLocalHost().getHostAddress();
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("getLocal ip: " + ip);	
		}

		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateNowStr = sdf.format(d);
		System.out.println("登录IP: " + ip+"登录时间:ip"+dateNowStr);
		return ip;
	}

再写一个监听,监听session失效,更新session失效时间

package cn.com.cis.acic.sales.common.web;

import ins.framework.web.Struts2Action;

import java.util.Date;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import cn.com.cis.acic.sales.common.service.facade.LoginLogService;
import cn.com.cis.acic.sales.schema.model.PrpsLoginLog;

public class LoginLogListener extends Struts2Action implements HttpSessionListener {
	private LoginLogService loginLogService;
	
	public LoginLogService getLoginLogService() {
		return loginLogService;
	}

	public void setLoginLogService(LoginLogService loginLogService) {
		this.loginLogService = loginLogService;
	}

	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
		
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
		try{
			ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(arg0.getSession().getServletContext());
 			LoginLogService loginLogService = (LoginLogService)ctx.getBean("loginLogService"); 
			String userCode=(String) arg0.getSession().getAttribute("USERCODE");
			String sessionId=arg0.getSession().getId();
			if(!"".equals(userCode)&&userCode!=null){
			PrpsLoginLog log=loginLogService.findLogByuserCode(userCode,sessionId);
			if(log!=null){
			log.setExittime(new Date());
			loginLogService.saveLoginLog(log);
			}
		  }
		System.out.println(userCode+"用户退出");
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

web.xml 中配置以下监听

<listener>
  <listener-class>cn.com.cis.acic.sales.common.web.LoginLogListener</listener-class>
</listener>

简单表结构及效果如下:

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。