一般注销是跳到原项目的登录页面,所以我们需要对CAS做如下配置:
1. 修改服务端cas-servlet.xml配置(apache-tomcat-7.0.40\cas\ROOT\WEB-INF),找到
<bean id="logoutController" class="org.jasig.cas.web.LogoutController" />
增加属性 p:followServiceRedirects="true"
2.修改客户端,sso1,sso2 index.jsp文件将原来的单点登出的URL修改成:
Sso1:http://jeesz.cn:8080/logout?service=http://www.sso1.com:8080
Sso2:http://jeesz.cn:8080/logout?service=http://www.sso2.com:8080
重启Tomcat,测试正常。
第六节:多项目集成单点登录配置
第一步:单点登录系统与其他项目集成
在WEB 项目中的WEB-INF目录下的web.xml文件,添加以下配置.
<!--SSO客户端配置 用于单点退出,该过滤器用于实现单点登出功能,可选配置 -->
<listener>
<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
</listener>
<!-- 该过滤器用于实现单点登出功能,可选配置。 -->
<filter>
<filter-name>SingleSignOutFilter</filter-name>
<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SingleSignOutFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 该过滤器负责对Ticket的校验工作,必须启用它 -->
<filter>
<filter-name>CASValidationFilter</filter-name>
<filter-class>
org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
<init-param>
<param-name>casServerUrlPrefix</param-name>
<param-value>https://jeesz.cn:8443/cas</param-value>;
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>http://www.sso3.com:6060</param-value> ; <!—客户端URL地址-->
</init-param>
<init-param>
<param-name>useSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>exceptionOnValidationFailure</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>redirectAfterValidation</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CASValidationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 该过滤器负责用户的认证工作,必须启用它 -->
<filter>
<filter-name>CASFilter</filter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
<init-param>
<param-name>casServerLoginUrl</param-name>
<param-value>https://jeesz.cn:8443/cas/login</param-value>;
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>http:// www.sso3.com:6060</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CASFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 允许通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。 -->
<filter>
<filter-name>CASHttpServletRequestWrapperFilter</filter-name>
<filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CASHttpServletRequestWrapperFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 该过滤器可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。 比如AssertionHolder.getAssertion().getPrincipal().getName()。 -->
<filter>
<filter-name>CASAssertionThreadLocalFilter</filter-name>
<filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CASAssertionThreadLocalFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 拦截成功登录SSO系统之后返回的数据并做相关处理. -->
<filter>
<filter-name>SSO4InvokeContextFilter</filter-name>
<filter-class>com.common.web.filter.SSO4InvokeContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SSO4InvokeContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
当sso验证完成之后,客户端系统需要接收sso系统返回的结果时,需要定义一个过滤器获取返回结果,然后针对返回结果做相关处理.
注意:如果不需要做处理时,此处Filter也可以不用定义.
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会话;
* @ClassName: SessionFilter
* @Description: TODO(这里用一句话描述这个类的作用)
* @author jeesz
* @date 2015-10-01
*
*/
public class SSO4InvokeContextFilter implements Filter{
private final static Log log = LogFactory.getLog(SSO4InvokeContextFilter.class);
private WebApplicationContext applicationContext;
public SSO4InvokeContextFilter() {
super();
}
/**
* 过滤器注销时,触发此方法;
*/
public void destroy() {
//暂时不做任何处理;
}
/**
* 根据用户id获取用户信息并且把用户信息放入session会话中;
* @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;
}
}
喜欢学习的朋友可以收藏,欢迎大家一起分享交流,本文更多源码来源:http://×××/technology
来源:oschina
链接:https://my.oschina.net/u/4414119/blog/4341967