LoginSuccessHandler.java 1.21 KB
package com.bsth.security.handler;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

import com.bsth.common.Constants;
import com.bsth.entity.sys.SessionLog;
import com.bsth.entity.sys.SysUser;
import com.bsth.util.IpUtils;

public class LoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler{

	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws ServletException, IOException {
		
		SysUser user = (SysUser) authentication.getPrincipal();
		
		//日志
		SessionLog sLog = new SessionLog();
		sLog.setLoginDate(new Date());
		sLog.setUser(user);
		sLog.setIp(IpUtils.getIpAddr(request));
		
		//session里写入用户名
		request.getSession().setAttribute(Constants.SESSION_USERNAME, user.getUserName());
		super.onAuthenticationSuccess(request, response, authentication);  
    }
	
}