JEESZ distributed framework -- single sign on integration scheme (3)

Multi project integrated single sign on configuration

After sso verification, when the client system needs to receive the results returned by sso system, it needs to define a Filter to get the returned results, and then do relevant processing for the returned results. If no processing is needed, the Filter can also be undefined here

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;

/**

When the SSO system is successfully logged in, the logged in userid will be returned to establish the session session according to this userid;

@ClassName: SessionFilter

* @Description: TODO(Here is a sentence to describe the function of this class)

*@authorjeesz

*@date2015-10-01

*

*/

public class SSO4InvokeContextFilter implements Filter{

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

private WebApplicationContext applicationContext;

public SSO4InvokeContextFilter() {

super();

}

This method is triggered when the filter is unregistered;

*/

public void destroy() {

//Do not do anything for the time being;

}

/**

*Get the user information according to the user id and put the user information into the session;

*@Title: doFilter

*Description: todo (use a sentence here to describe the function of this method)

* @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();

//Get the login user from the session;

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

if(userObject == null){

//Get user name;

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);

//Query the user information according to the user name, and put it into the session;

log.info("UserName:["+userName +"]Login success, client IP Address is["+IPUtil.getIpAddr(request)+"]´╝îLogin time is["+DateUtil.dateToString(new Date())+"]");

//Add login record;

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("Login history["+(hlBol?"Success":"fail")+"].");

}

chain.doFilter(request, response);

}

/**

* Set public properties;

* @Title: setCommonValue

* @Description: TODO(Here is a sentence describing the function of this method.)

* @throws

*/

private void setCommonValue(HttpServletRequest request,AbstractEntity entity){

if(request != null){

//Get the current object;

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

if(entity !=null){

String currUser = userEntity.getUserId();

//Set creator, creation date, modifier and modification time

entity.setCreatedBy(currUser);

entity.setModifiedBy(currUser);

entity.setCreationDate(DateUtil.getNowDate());

entity.setModifiedDate(DateUtil.getNowDate());

}

}

}

/**

* Initialize the Spring context;

*/

@Override

public void init(FilterConfig filterConfig) throws ServletException {

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

this.applicationContext = applicationContext;

}

}

Tags: Session Java Apache Spring

Posted on Wed, 12 Feb 2020 13:01:47 -0500 by StoneJunkie