cas 系统实例 服务端配置(一)

最近在研究cas 并对其进行改版,终于有所成就,写出来与大家分享一下,不足之处希望指教,共同进步。

首先下载casserver和casclient

下载地址分别为:

服务器端下载: http://www.jasig.org/cas_server_3_4_11_release

客户端下载:http://www.jasig.org/jasig-cas-client-java-version-321-releas

部署方法:

将casserver解压后取其中的modules/cas-server-webapp-3.4.11.war.将其重命名为casserver.war,然后放到tomcat的webapps中。

并修改cas.property里的路径,替换/cas/casserver

访问路径:http://localhost:8081/casserver到达登录


输入用户名abc,密码abc,即可登录成功。系统要求用户名=密码。

下面我们改版,让其支持数据库登录检测。

1.修改deployerConfigContext.xml文件,添加数据源配置

 
<bean id="casDataSource" class="org.apache.commons.dbcp.BasicDataSource">
   <property name="driverClassName">
      <value>com.mysql.jdbc.Driver</value>
   </property>
   <property name="url">
     <value>jdbc:mysql://192.168.1.100/ires?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true</value>
   </property>
   <property name="username">
      <value>ires</value>
    </property>
    <property name="password">
      <value>i709394</value>
   </property>
</bean>

  定义MD5的加密方式

<bean id="passwordEncoder"
class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName">
<constructor-arg value="MD5"/>
</bean>

配置authenticationManager下面的authenticationHandlers属性

<bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
     <property name="dataSource" ref="casDataSource" />
     <property name="sql" value="select community_password from community_user_info where lower(community_user_info.community_user) = lower(?)" />
     <property name="passwordEncoder" ref="passwordEncoder"/>
</bean>

 需要引入jar文件为:cas-server-support-jdbc-version.jar

修改点2:获取用户信息保存,方便各个客户端可以统一得到用户信息

1.定义attributeRepository,通过jdbc查询用户的详细信息,可以把用户表或用户的所属组织机构或角色等查询出来。

写道
<bean id="attributeRepository" class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao">
    <constructor-arg index="0" ref="casDataSource" />
    <constructor-arg index="1" value=" select * from USERS where USER_NAME = ? "/>
    <property name="queryAttributeMapping">
        <map>
          <entry key="username" value="uid"/>
       </map>
     </property>
     <property name="resultAttributeMapping">
      <map>
      <entry key="ID" value="id"/><!--要查询的附加属性,前者数据库属性,后者映射后的属性-->
      <entry key="USER_NAME" value="userName"/>
      <entry key="CHANNEL" value="chanel"/>
     <entry key="STATUS" value="status"/>
     </map>
</property>
</bean>

修改名称为serviceRegistryDao的bean,在list中选择针对自己选择的方式添加属性,我这里选择的是http://**类型,然后添加了需要额外查询的属性名

                 <property name="allowedAttributes">
                            <list>
                                <value>id</value>
                                <value>chanel</value>
                                <value>status</value>
                            </list>
                        </property>

总体如下。

<bean
		id="serviceRegistryDao"
        class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl">
            <property name="registeredServices">
                <list>
                    <bean class="org.jasig.cas.services.RegisteredServiceImpl">
                        <property name="id" value="0" />
                        <property name="name" value="HTTP" />
                        <property name="description" value="Only Allows HTTP Urls" />
                        <property name="serviceId" value="http://**" />
                        <property name="evaluationOrder" value="10000001" />
                        <property name="allowedAttributes">
                        	<list>
                                         <!--映射后的属性--> 
                                        <value>id</value>
                        		<value>chanel</value>
                        		<value>status</value>
                        	</list>
                        </property>
                    </bean>

                    <bean class="org.jasig.cas.services.RegisteredServiceImpl">
                        <property name="id" value="1" />
                        <property name="name" value="HTTPS" />
                        <property name="description" value="Only Allows HTTPS Urls" />
                        <property name="serviceId" value="https://**" />
                        <property name="evaluationOrder" value="10000002" />
                    </bean>

                    <bean class="org.jasig.cas.services.RegisteredServiceImpl">
                        <property name="id" value="2" />
                        <property name="name" value="IMAPS" />
                        <property name="description" value="Only Allows HTTPS Urls" />
                        <property name="serviceId" value="imaps://**" />
                        <property name="evaluationOrder" value="10000003" />
                    </bean>

                    <bean class="org.jasig.cas.services.RegisteredServiceImpl">
                        <property name="id" value="3" />
                        <property name="name" value="IMAP" />
                        <property name="description" value="Only Allows IMAP Urls" />
                        <property name="serviceId" value="imap://**" />
                        <property name="evaluationOrder" value="10000004" />
                    </bean>
                </list>
            </property>
        </bean>

SingleRowJdbcPersonAttributeDao.java 在下面下载。

配置authenticationManager中credentialsToPrincipalResolvers属性

<bean class="org.jasig.cas.authentication.principal.UsernamePasswordCredentialsToPrincipalResolver">
<property name="attributeRepository" ref="attributeRepository" /> </bean>

注意:默认cas登录服务器没有把用户信息传到客户端中 ,所以要修改WEB-INF\view\jsp\protocol\2.0\casServiceValidationSuccess.jsp文件,增加

 
 
<c:if test="${fn:length(assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.attributes) > 0}">
<cas:attributes>
<c:forEach var="attr" items="${assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.attributes}">

<cas:${fn:escapeXml(attr.key)}>${fn:escapeXml(attr.value)}</cas:${fn:escapeXml(attr.key)}>


</c:forEach>
</cas:attributes>
</c:if>
  取消https验证
 
在网络安全性较好,对系统安全没有那么高的情况下可以取消https验证,使系统更加容易部署。

1.修改ticketGrantingTicketCookieGenerator.xml

XML/HTML代码
<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
p:cookieSecure="false"
p:cookieMaxAge="-1"
p:cookieName="CASTGC"
p:cookiePath="/cas" />
p:cookieSecure改成false,客户端web.xml中单独服务器的链接改成http
  随机码,查看附件RandomNumberPictureController.rar
参考网站http://blog.csdn.net/turkeyzhou/article/details/5509725
自己修改页面时,注意修改路径web-info/cas.property文件

猜你喜欢

转载自01jiangwei01.iteye.com/blog/1434200