---
title: "CAS单点登录HTTP配置"
author: "rocye"
date: "2015-03-02T18:14:30Z"
tags: []
categories: ["开源软件"]
slug: "cas-http"
draft: false
lastmod: "2015-03-03T15:14:57Z"
id: 2188064
url: "https://rocye.iteye.com/blog/2188064"
---

**本文目录：**

 

- 一、概述
- 二、演示环境
- 三、部署CAS-Server相关的Tomcat
- 四、部署CAS-Client相关的Tomcat
- 五、测试验证SSO

 **一、概述**

 最近在研究Sina单点登录，网上有博主分析了它的登录机制，并且还给出了仿Sina的Ajax异步单点登录的思路及代码片断，因为第一次接触CAS，没办法一下子构建自己想要的异步单点登录项目，所以开始在网上不停的查找相关的文章，由于网上大多数的CAS配置都是基于HTTPS的，需要生成证书，导入证书到jre中，但是Sina的单点登录却不是基于HTTPS的，所以此文的目的是对CAS单点登录HTTP配置进行测试。

 

CAS的官网：[https://www.apereo.org/cas](https://www.apereo.org/cas) 

参考文章：[http://www.micmiu.com/enterprise-app/sso/sso-cas-sample/](http://www.micmiu.com/enterprise-app/sso/sso-cas-sample/) 

        [http://blog.csdn.net/ycyk\_168/article/details/18668951](http://blog.csdn.net/ycyk_168/article/details/18668951)

   

**二、测试环境**

 

本文测试过程在同一个机器上的（也可以在三台实体机器或者三个的虚拟机上），环境如下：

- Windows7 64位
- JDK 1.6.0\_10
- Tomcat 6.0.43
- CAS-server-4.0.0、CAS-client-3.3.3

 

根据演示需求，用修改hosts 文件的方法添加域名最简单方便（这个非常重要），在文件 C:\\Windows\\System32\\drivers\\etc\\hosts 文件中添加三条：

 #test CAS SSO Start

 127.0.0.1      sso.rocye.com

 127.0.0.1      appone.rocye.com

 127.0.0.1      apptwo.rocye.com

 #test CAS SSO End

 

- sso.rocye.com  =&gt;&gt; 对应部署cas server的tomcat，这个虚拟域名还用于证书生成
- appone.rocye.com  =&gt;&gt;  对应部署app1 的tomcat
- apptwo.rocye.com   =&gt;&gt; 对应部署app2 的tomcat

 **三、部署CAS-Server相关的Tomcat**

 3.1 解压apache-tomcat-6.0.43.zip并重命名后的路径为 e:\\sso\\tomcat-cas

  

 3.2 下载官网最新版cas-server-4.0.0-release.zip 为例，解压提取cas-server-4.0.0\\modules\\ cas-server-webapp-4.0.0.war文件，把文件复制到 e:\\sso\\tomcat-cas\\webapps\\ 目下，并重命名为：cas.war启动tomcat-cas，得到项目发布文件夹cas

  

 3.3 修改WEB-INFO\\deployerConfigContext.xml加入p:requireSecure="false"

 &lt;bean id="proxyAuthenticationHandler"

 class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"

 p:httpClient-ref="httpClient" **p:requireSecure="false"** /&gt;

 

 3.4 修改WEB-INF\\spring-configuration\\ticketGrantingTicketCookieGenerator.xml，修改p:cookieSecure="false"

&lt;bean id="ticketGrantingTicketCookieGenerator"

class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"

**p:cookieSecure="false"**

p:cookieMaxAge="-1"

p:cookieName="CASTGC"

p:cookiePath="/cas" /&gt;

  

3.5 修改WEB-INF\\spring-configuration\\warnCookieGenerator.xml，修改p:cookieSecure="false"

 &lt;bean id="warnCookieGenerator"

 class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"

 **p:cookieSecure="false"**

 p:cookieMaxAge="-1"

 p:cookieName="CASPRIVACY"

 p:cookiePath="/cas" /&gt;

   

3.6 重新启动Tomcat，在地址栏输入: [http://sso.rocye.com:8080/cas/login](http://sso.rocye.com:8080/cas/login)回车：

 

![](http://dl2.iteye.com/upload/attachment/0106/3006/93ae5147-d631-3de4-9230-9f66cb4bf318.png)  
 

 输入登录名：**casuser**密码：**Mellon**  得到登录成功页面：

 

![](http://dl2.iteye.com/upload/attachment/0106/3008/549940e8-963a-363a-8d2e-1b7a2b0bcd33.png)  
  

这里不用管登录页上面的红色提示信息：“Non-secure Connection…”，这只是这个登录页面对于URL不是https开头的一个验证提示信息，继续登录得到“登录成功”表示已经部署成功。

  

**四、部署CAS-Server相关的Tomcat**

 4.1 解压apache-tomcat-6.0.43.zip并重命名后的路径为：e:\\sso\\tomcat-app1，修改conf下的配置文件server.xml如下：

 &lt;Server port="**18005**" shutdown="SHUTDOWN"&gt;

 …

 &lt;Connector port="18080" protocol="HTTP/1.1"

                connectionTimeout="20000"

                redirectPort="**18443**" /&gt;

 &lt;Connector port="**18009**" protocol="AJP/1.3" redirectPort="**18443**" /&gt;

  

4.2 以官网最新版cas-client-3.3.3-release.zip为例，解压提取cas-client-3.3.3\\modules\\ cas-client-core-3.3.3.jar文件，以tomcat默认自带的 webapps\\examples 作为演示的简单web项目，把jar文件复制到 webapps\\ examples项目的lib文件夹下，同时还要复制slf4j-api-1.7.1.jar。

  

4.3 在tomcat-app1\\webapps\\examples\\WEB-INF\\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>      

<filter>  
    <filter-name>CAS Filter</filter-name>  
    <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>  
    <init-param>  
        <param-name>casServerLoginUrl</param-name>  
        <param-value>http://sso.rocye.com:8080/cas/login</param-value>  
    </init-param>  
    <init-param>  
        <param-name>serverName</param-name>  
        <param-value>http://appone.rocye.com:18080</param-value>  
   </init-param>  
</filter>  

<filter-mapping>  
    <filter-name>CAS Filter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>  

<!-- 该过滤器负责对Ticket的校验工作，必须启用它 --> 
<filter>  
    <filter-name>CAS Validation Filter</filter-name>  
    <filter-class>  
        org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>  
    <init-param>  
        <param-name>casServerUrlPrefix</param-name>  
        <param-value>http://sso.rocye.com:8080/cas</param-value>  
    </init-param>  
    <init-param>  
        <param-name>serverName</param-name>  
        <param-value>http://appone.rocye.com:18080</param-value>  
    </init-param>  
</filter>  

<filter-mapping>  
    <filter-name>CAS Validation Filter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>     

<!--  
    该过滤器负责实现HttpServletRequest请求的包裹，  
    比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名，可选配置。  
--> 
<filter>  
    <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
    <filter-class>  
        org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>  
</filter>  

<filter-mapping>  
    <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>    

<!--  
该过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。  
比如AssertionHolder.getAssertion().getPrincipal().getName()。  
--> 
<filter>  
    <filter-name>CAS Assertion Thread Local Filter</filter-name>  
    <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>  
</filter>  
<filter-mapping>  
    <filter-name>CAS Assertion Thread Local Filter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>      
<!-- ======================== 单点登录结束 ======================== -->
```

  

4.4 按照上面的4.1-4.3步骤得到tomcat-app2，相应端口改成28080…配置文件添加的单点登录配置也要对应修改好。

  

**五、测试验证SSO**

 预期流程：打开app1 url —-&gt; 跳转cas server 验证 —-&gt; 显示app1的应用 —-&gt; 打开app2 url —-&gt; 显示app2应用 —-&gt; 注销cas server —-&gt; 打开app1/app2 url —-&gt; 重新跳转到cas server 验证。

 启动之前配置好的三个Tomcat，分别为：tomcat-cas、tomcat-app1、tomcat-app2

 

打开浏览器输入链接地址：[http://appone.rocye.com:18080/examples/servlets/servlet/HelloWorldExample](http://appone.rocye.com:18080/examples/servlets/servlet/HelloWorldExample)它会跳转到登录页面：

 ![](http://dl2.iteye.com/upload/attachment/0106/3010/51ca99d3-8622-38f0-9406-4d331e4228b3.png)

 

输入登录名：**casuser**密码：**Mellon**  得到下面的页面：

 ![](http://dl2.iteye.com/upload/attachment/0106/3012/3d3732e1-3ba0-373e-bc41-da250affa84a.png)

 再在地址栏输入app2的地址：[http://apptwo.rocye.com:28080/examples/servlets/servlet/HelloWorldExample](http://apptwo.rocye.com:28080/examples/servlets/servlet/HelloWorldExample)它会直接进入下面的页面，不需要再登录：

 ![](http://dl2.iteye.com/upload/attachment/0106/3014/0e0046b8-1535-3681-96b5-13bc1151444e.png)

 紧接着打开一个新Tab页输入：[http://sso.rocye.com:8080/cas/logout](http://sso.rocye.com:8080/cas/logout)

 ![](http://dl2.iteye.com/upload/attachment/0106/3016/5fd36ab3-6624-342b-9e5e-b6b150b43eb2.png)

 

再次打开之前访问的两个页面：

 [http://appone.rocye.com:18080/examples/servlets/servlet/HelloWorldExample](http://appone.rocye.com:18080/examples/servlets/servlet/HelloWorldExample)

 [http://apptwo.rocye.com:28080/examples/servlets/servlet/HelloWorldExample](http://apptwo.rocye.com:28080/examples/servlets/servlet/HelloWorldExample)

 都会直接跳到登录页，表示成功。

 

 本文到此已结束，下一步将会整合此文中的功能到Spring+Struts+Mybatis框架上，并升级为ajax异步的方式。