<!--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.sso2.com</param-value>;
</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.sso2.com</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>
6.编辑sso1,sso2 index.jsp页面,复制如下代码:
sso1 index.jsp:
<h3>这个是www.sso1.com</h3>
<dl>
<dt>你好我是sso1页面:</dt>
<h3><%= request.getRemoteUser()== null ? "null" : request.getRemoteUser() %></h3>
</dl>
<br/>
Sso2 index.jsp:
<h3>这个是www.sso2.com</h3>
<dl>
<dt>你好我是sso2页面:</dt>
<h3><%= request.getRemoteUser()== null ? "null" : request.getRemoteUser() %></h3>
</dl>
<br/>
7.通过eclipse发布到Tomcat服务器上去。找到Tomcat,webapps下的sso1,sso2文件夹,分别复制sso1和sso2下面的所有文件,找到Tomcat根目录下的sso1,sso2(就是我们前面步骤新建的目录),在sso1,sso2下新建ROOT目录,把刚刚复制的文件粘贴。
配置Tomcat 下server.xml文件,加入如下代码:
<Host name="www.sso1.com" appBase="sso1" unpackWARs="true" autoDeploy="true"></Host>
<Host name="www.sso2.com" appBase="sso2" unpackWARs="true" autoDeploy="true" ></Host>
8.重启Tomcat,打开浏览器输入网址:www.sso1.com:8080,输入用户名与密码,如果出现以下信息,则成功
在浏览器上输入www.sso1.com:8080,你会发现跳回到登录页面,不要担心。打开ticketGrantingTicketCookieGenerator.xml路径在apache-tomcat-7.0.40\cas\ROOT\WEB-INF\spring-configuration。找到p:cookieSecure="true",将其修改为p:cookieSecure="false",重启Tomcat,测试一下。
注:我们以上步骤返回的只有一个用户名,CAS服务器默认返回该信息。
第是节:修改单点登录默认界面
第一步:找到它自己的页面
打开cas工程,找到G:\SoftWare\tomcat\apache-tomcat-7.0.2\apache-tomcat-7.0.2\webapps\cas\WEB-INF\view\jsp\default这个目录,将其复制一份重命名放在G:\SoftWare\tomcat\apache-tomcat-7.0.2\apache-tomcat-7.0.2\webapps\cas\WEB-INF\view\jsp\目录下,
第二步:修改默认登录页面
将其命名为mydefalut如下图:
然后将apache-tomcat-7.0.2\webapps\cas\WEB-INF\classes
目录下的default_views.properties复制一份,重命名为mydefault_views.properties。修改里面的内容,将所有里面的路径中的default改成mydefault。然后修改WEB-INF下的cas.properties将里面的cas.viewResolver.basename后面的值修改成mydefault_views即:
cas.viewResolver.basename=mydefault_views
如果要修改登录界面,只需要修改casLoginView.jsp即可。
第五节:单点登出配置
第一步:配置单点登录监听器和过滤器
1.Tomcat的sso1,sso2的工程里,在web.xml里分别添加一下代码:
<listener>
<listener-class>
org.jasig.cas.client.session.SingleSignOutHttpSessionListener
</listener-class>
</listener>
<filter>
<filter-name>CAS Single Sign Out Filter</filter-name>
<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CAS Single Sign Out Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注意:这些代码应该放在web.xml文件其他过滤器的最前面。
2.在sso1,sso2的index.jsp页面添加以下代码:
<br/>
<a href = "http://jeesz.cn:8080/logout">单点退出</a>
3.重启Tomcat,访问http://jeesz.cn:8080/点击单点退出,如果成功,就会出现如下界面:
一般注销是跳到原项目的登录页面,所以我们需要对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://minglisoft.cn/technology
声明:该框架面向企业,是大型互联网分布式企业架构,后期会介绍linux上部署高可用集群项目。
有愿意了解框架技术或者源码的朋友直接加Q(2042849237)一起学习