CAS配置相关

环境:cas-client-java-2.1.1 cas-client-core-3.1.10.jar  cas-server-core-3.1.1.jar 

1.服务端
1.1配数据库连接池
相关包:jtds-1.0.2.jar; proxool-0.9.1.jar; proxool-cglib.jar; cas-server-support-jdbc-3.1.1.jar
web.xml  
 <!-- 注册连接池 -->
 <servlet>
      <servlet-name>proxoolServletConfigurator</servlet-name>
      <servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class>
      <init-param>
         <param-name>xmlFile</param-name>
         <param-value>/WEB-INF/proxool-config.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>
  
deployerConfigContext.xml
    <!--  注释掉默认的验证方式
 <bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />
  -->
 
 <!--用户名和密码验证接口 自定义的验证方式-->
    <bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
        <property name="dataSource" ref="casDataSource" />
        <property name="sql"
              value="select password from com_Users where lower(UserName) = lower(?)" />
 </bean>
 
   <!-- 连接池数据源 -->
    <bean id="casDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">  
         <property name="driverClassName"> 
             <value>org.logicalcobwebs.proxool.ProxoolDriver</value> 
         </property> 
         <property name="url"> 
             <value>proxool.spring_pro_pool</value> 
         </property> 
    </bean>
 
1.2 密码加密验证
另外,由于存放在数据库中的密码通常是加密过的,所以 AuthenticationHandler 在匹配时需要知道使用的加密方法,在 deployerConfigContext.xml 文件中我们可以为具体的 AuthenticationHandler 类配置一个 property,指定加密器类,比如对于 QueryDatabaseAuthenticationHandler,可以修改如清单7所示:  
<bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
  <property name="dataSource" ref=" casDataSource " />
  <property name="sql"
           value="select password from userTable where lower(userName) = lower(?)" />
  <property  name="passwordEncoder"  ref="myPasswordEncoder"/>
</bean>
<bean id="myPasswordEncoder"
            class="org.jasig.cas.authentication.handler.MyPasswordEncoder"/>
这里 MyPasswordEncoder 是根据实际情况自己定义的加密器,实现 PasswordEncoder 接口及其 encode() 方法。
 
1.3 修改默认界面 
      CAS页面文件放在Tomcat 6.0/webapps/cas/WEB-INF/view/jsp中,页面配置文件在Tomcat 6.0/webapps/cas/WEB-INF/classes中,比如default_views.properties,在这里指定登录用哪个页面,注销用哪个页面等等。
在Tomcat 6.0/webapps/cas/WEB-INF/cas.properties文件指定使用哪个皮肤页面。
      下面开始操作:
      1、进入Tomcat 6.0/webapps/cas/WEB-INF/view/jsp,复制default文件夹,并改名,再复制回来,如myth。
      2、进入Tomcat 6.0/webapps/cas/WEB-INF/classes,复制default_views.properties,并改名再复制回来,如myth_views.properties。打开文件,把里面的目录../jsp/default/ui修改为自己的路径,如../jsp/myth/ui。
      3、进入Tomcat 6.0/webapps/cas/WEB-INF,打开cas.properties文件,修改
cas.viewResolver.basename=myth_views
完成以上3步后,打开IE,进入http://localhost:8080/cas进行测试,如果成功则进行下一步。
有 4 个页面是必须的:
casConfirmView.jsp: 当用户选择了“ warn ”时会看到的确认界面
casGenericSuccess.jsp: 在用户成功通过认证而没有目的Service时会看到的界面
casLoginView.jsp: 当需要用户提供认证信息时会出现的界面
casLogoutView.jsp: 当用户结束 CAS 单点登录系统会话时出现的界面

 

2 客户端配置
2.1 web.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <description>cas client test</description>
    <!--CAS Authentication FILTER -->
    <filter>
     <filter-name>CAS Filter</filter-name>
     <filter-class>edu.yale.its.tp.cas.client.filter.CASFilter</filter-class>
     <init-param>
       <param-name>edu.yale.its.tp.cas.client.filter.loginUrl</param-name>
       <param-value>http://localhost:8080/cas/login</param-value>
     </init-param>
     <init-param>
       <param-name>edu.yale.its.tp.cas.client.filter.validateUrl</param-name>
       <param-value>http://localhost:8080/cas/serviceValidate</param-value>
     </init-param>
     <init-param>
       <param-name>edu.yale.its.tp.cas.client.filter.serverName</param-name>
       <param-value>localhost:8080</param-value>
     </init-param>
  </filter>
    <!-- CAS SINGLE SIGN OUT FILTER -->
    <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-mapping>
     <filter-name>CAS Filter</filter-name>
     <url-pattern>/*</url-pattern>
  </filter-mapping>
    <!-- SingleSignOutHttpSessionListener LISTENER -->
    <listener>
        <listener-class>
            org.jasig.cas.client.session.SingleSignOutHttpSessionListener
        </listener-class>
    </listener>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>


CASFilter 必需的参数
参数名 作用
edu.yale.its.tp.cas.client.filter.loginUrl  指定 CAS 提供登录页面的 URL
edu.yale.its.tp.cas.client.filter.validateUrl  指定 CAS 提供 service ticket 或 proxy ticket 验证服务的 URL
edu.yale.its.tp.cas.client.filter.serverName  指定客户端的域名和端口,是指客户端应用所在机器而不是 CAS Server 所在机器,该参数或 serviceUrl 至少有一个必须指定
edu.yale.its.tp.cas.client.filter.serviceUrl  该参数指定过后将覆盖 serverName 参数,成为登录成功过后重定向的目的地址

 

CASFilter 可选参数
参数名 作用
edu.yale.its.tp.cas.client.filter.proxyCallbackUrl  用于当前应用需要作为其他服务的代理(proxy)时获取 Proxy Granting Ticket 的地址
edu.yale.its.tp.cas.client.filter.authorizedProxy  用于允许当前应用从代理处获取 proxy tickets,该参数接受以空格分隔开的多个 proxy URLs,但实际使用只需要一个成功即可。当指定该参数过后,需要修改 validateUrl 到 proxyValidate,而不再是 serviceValidate
edu.yale.its.tp.cas.client.filter.renew  如果指定为 true,那么受保护的资源每次被访问时均要求用户重新进行验证,而不管之前是否已经通过
edu.yale.its.tp.cas.client.filter.wrapRequest  如果指定为 true,那么 CASFilter 将重新包装 HttpRequest,并且使 getRemoteUser() 方法返回当前登录用户的用户名
edu.yale.its.tp.cas.client.filter.gateway  指定 gateway 属性

2.2 客户端获取已登录的用户名:
// 以下两者都可以
session.getAttribute(CASFilter.CAS_FILTER_USER);
session.getAttribute("edu.yale.its.tp.cas.client.filter.user");
CASReceipt  receipt = (CASReceipt )session.getAttribute(edu.yale.its.tp.cas.client.filter.CASFilter.CAS_FILTER_RECEIPT);
通过 JSTL 获取登录用户名     
<c:out value="${sessionScope[CAS:'edu.yale.its.tp.cas.client.filter.user']}"/>
另外,CAS 提供了一个 CASFilterRequestWrapper 类,该类继承自HttpServletRequestWrapper,主要是重写了 getRemoteUser() 方法,只要在前面配置 CASFilter 的时候为其设置“ edu.yale.its.tp.cas.client.filter.wrapRequest ”参数为 true,就可以通过 getRemoteUser() 方法来获取登录用户名
CASFilterRequestWrapper  reqWrapper=new CASFilterRequestWrapper(request);
out.println("The logon user:" + reqWrapper.getRemoteUser());

3 修改成不用https
3.1 client 2.x:
修改客户端代码:edu.yale.its.tp.cas.client.filter.CASFilter 将所有判断是否是https连接的代码注释掉

3.2 server 3.3 或以上
cas/WEB-INF/spring-configuration /ticketGrantingTicketCookieGenerator.xml,这是在3.3版本中,
原有配置如下:
<bean id="ticketGrantingTicketCookieGenerator" class="org.springframework.web.util.CookieGenerator">
<property name="cookieSecure" value="true " />
<property name="cookieMaxAge" value="-1" />
<property name="cookieName" value="CASTGC" />
<property name="cookiePath" value="/cas" />
</bean>
将相关的cookieSecure 设为false

 <bean id="warnCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
  p:cookieSecure="false"
  p:cookieMaxAge="-1"
  p:cookieName="CASPRIVACY"
  p:cookiePath="/cas" />
 
 <bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
  p:cookieSecure="false"
  p:cookieMaxAge="-1"
  p:cookieName="CASTGC"
  p:cookiePath="/cas" />


<!--   现在是client 3.x 的配置方式
 <description>cas client test</description>
 <!--CAS Authentication FILTER
 <filter>
  <filter-name>CAS Authentication Filter</filter-name>
  <filter-class>
   org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
  <!-- cas server LOGIN URL
  <!-- https://www.test.com:8443/cas/login
  <init-param>
   <param-name>casServerLoginUrl</param-name>
   <param-value>http://localhost:8080/cas/login</param-value>
  </init-param>
  <!-- local web URL
  <init-param>
   <param-name>serverName</param-name>
   <param-value>http://localhost:8080</param-value>
  </init-param>
 </filter>
 <!-- CAS Validation FILTER
 <filter>
  <filter-name>CAS Validation Filter</filter-name>
  <filter-class>
   org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
  <!-- CAS SERVER URL -->
  <!-- https://www.test.com:8443/cas 
  <init-param>
   <param-name>casServerUrlPrefix</param-name>
   <param-value>https://www.test.com:8443/cas</param-value>
  </init-param>
  <!-- LOCAL web URL
  <init-param>
   <param-name>serverName</param-name>
   <param-value>http://www.testd.com:8080</param-value>
  </init-param>
  <!-- if validation false throw exception ; default true
  <init-param>
   <param-name>exceptionOnValidationFailure</param-name>
   <param-value>false</param-value>
  </init-param>
 </filter>
 <!-- cas security username on request.getRemoteUser()
 <filter>
  <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
  <filter-class>
   org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
 </filter>
 <!-- CAS SINGLE SIGN OUT FILTER
 <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-mapping>
  <filter-name>CAS Validation Filter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
  <filter-name>CAS Authentication Filter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
  <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
  <url-pattern>/index.jsp</url-pattern>
 </filter-mapping>

 <!-- SingleSignOutHttpSessionListener LISTENER
 <listener>
  <listener-class>
   org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
 </listener>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
-->

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值