JEESZ分佈式框架--單點登陸集成方案(三)

多項目集成單點登陸配置java

當sso驗證完成以後,客戶端系統須要接收sso系統返回的結果時,須要定義一個過濾器獲取返回結果,而後針對返回結果作相關處理.若是不須要作處理時,此處Filter也能夠不用定義.web

package com.common.web.filter;

import java.io.IOException;

import java.util.Date;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.jasig.cas.client.util.AssertionHolder;

import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.common.base.pbi.CommonConstants;

import com.common.base.util.DateUtil;

import com.common.base.util.IDUtil;

import com.common.base.util.IPUtil;

import com.common.base.util.UserUtil;

import com.common.core.busi.historylogin.manager.HistoryLoginManager;

import com.common.core.busi.login.manager.LoginManager;

import com.common.entity.common.AbstractEntity;

import com.common.entity.historylogin.HistoryLoginEntity;

import com.common.entity.user.UserEntity;

/**

 當成功登陸SSO系統時將會返回登陸的userid根據此userid創建session會話;spring

@ClassName: SessionFilter

* @Description: TODO(這裏用一句話描述這個類的做用)

*@authorjeesz

*@date2015-10-01

*

*/

public class SSO4InvokeContextFilter implements Filter{

private final static Log log = LogFactory.getLog(SSO4InvokeContextFilter.class);

private WebApplicationContext applicationContext;

public SSO4InvokeContextFilter() {

super();

}

過濾器註銷時,觸發此方法;apache

*/session

public void destroy() {app

//暫時不作任何處理;ide

}this

/**.net

* 根據用戶id獲取用戶信息而且把用戶信息放入session會話中;debug

*@Title: doFilter

* @Description: TODO(這裏用一句話描述這個方法的做用)

* @Params

* @throws

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest)req;

HttpServletResponse response = (HttpServletResponse)res;

HttpSession session =  request.getSession();

//從session中獲取登錄用戶;

Object userObject = session.getAttribute(CommonConstants.USER_INFO_SESSION);

if(userObject == null){

//獲取用戶名;

String userName = AssertionHolder.getAssertion().getPrincipal().getName();

LoginManager loginManager = applicationContext.getBean(LoginManager.class);

UserEntity userEntity = loginManager.loginByUserName(userName);

session.setAttribute(CommonConstants.USER_INFO_SESSION,userEntity);

session.setAttribute(CommonConstants.IS_SYSTEM_ADMIN,userEntity.getUserType()==1?true:false);

UserUtil.setLoginUserInfo(userEntity);

//根據用戶名查詢出用戶信息,並放入session中;

log.info("UserName:["+userName +"]登錄成功,客戶端IP地址爲["+IPUtil.getIpAddr(request)+"],登錄時間爲["+DateUtil.dateToString(new Date())+"]");

//添加登陸記錄;

HistoryLoginEntity historyLoginEntity = new HistoryLoginEntity();

historyLoginEntity.setUserId(userName);

historyLoginEntity.setHid(IDUtil.generateId());

historyLoginEntity.setLoginCount("1");

setCommonValue(request,historyLoginEntity);

boolean hlBol = applicationContext.getBean(HistoryLoginManager.class).addLoginRecord(historyLoginEntity);

log.debug("登陸歷史記錄["+(hlBol?"成功":"失敗")+"].");

}

chain.doFilter(request, response);

}

/**

* 設置公共屬性;

* @Title: setCommonValue

* @Description: TODO(這裏用一句話描述這個方法的做用)

* @throws

*/

private void setCommonValue(HttpServletRequest request,AbstractEntity entity){

if(request != null){

//獲取當前對象;

UserEntity userEntity = (UserEntity) request.getSession().getAttribute(CommonConstants.USER_INFO_SESSION);

if(entity !=null){

String currUser = userEntity.getUserId();

//設置建立人、建立日期、修改人、修改時間

entity.setCreatedBy(currUser);

entity.setModifiedBy(currUser);

entity.setCreationDate(DateUtil.getNowDate());

entity.setModifiedDate(DateUtil.getNowDate());

}

}

}

/**

* 初始化Spring上下文;

*/

@Override

public void init(FilterConfig filterConfig) throws ServletException {

WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(filterConfig.getServletContext());

this.applicationContext = applicationContext;

}

}
相關文章
相關標籤/搜索