cas单点登录服务器配置

      最近需要给三个.net MVC4系统做单点登录功能,自然想到曾经在java中使用过的单

点登录服务cas。参考了许多文章,这里只是记录下配置的过程,防止过段时间忘记。

  cas服务器可以采用https,也可以采用http,这里先采用简单的方式http,但是不安

全。https的配置涉及到证书,以后再总结。

      

      1.下载cas服务端代码

 下载地址https://github.com/apereo/cas/releases,我选择的版本是v4.2.7


      

    2.代码打包

       4.2.7使用的gradle构建工具,解压后找到目录下的cas-server-webapp,如下图,

 然后使用gradle命令将该项目编译打成war包(gradle的安装配置网上有很多),如果

需要修改源码,也可以使用gradle工具将项目转换eclipse项目,不然项目不能导入到

eclipse中。

    3.测试war包

     将cas-4.2.7\cas-server-webapp\build\libs文件夹中的cas-server-webapp-2.7.war

包考到tomcat的webapps目录,重命名为cas.war,启动tomcat。启动成功后在浏览器中

查看http://sso.demo.com:8070/cas/login(这里http://sso.demo.com/指向我本地机

器ip),如下图


      

 红色部分的提示不用管,用户名输入casuser,密码输入Mellon,点击登录,如下图

表明cas服务器部署成功。

     

       4.数据库配置(MYSQL)

  上面的用户名和密码是写死在cas.properties中,下面配置通过数据库来验证登录

  找到tomcat中解压后的cas.war文件,目录为apache-tomcat-8.0.39\webapps\,修改

apache-tomcat-8.0.39\webapps\cas\WEB-INF\deployerConfigContext.xml文件,一下

是我参考网上的一些配置,经测试可以使用

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:sec="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">


    <util:map id="authenticationHandlersResolvers">
        <entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver" /> 
        <entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" />
    </util:map>


    <util:list id="authenticationMetadataPopulators">
        <ref bean="successfulHandlerMetaDataPopulator" />
        <ref bean="rememberMeAuthenticationMetaDataPopulator" />
    </util:list>
 
	 <bean id="attributeRepository" class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao" >
        <constructor-arg index="0" ref="dataSource"/>
       <constructor-arg index="1">
			<value>select * from s_user where {0}</value>
	   </constructor-arg>
        <property name="queryAttributeMapping">
            <map>		
			   <!-- queryAttributeMapping username是登录框的id,value数据库字段 -->
				 <entry key="username" value="user_name"/>
				 <entry key="password" value="password"/>	
            </map>
        </property>
         <property name="resultAttributeMapping">
                <map>			
				 <!-- resultAttributeMapping key是数据库中字段,value是返回给客户端的属性名-->
					<entry key="user_name" value="userName"/>
					<entry key="password" value="password"/>
					<entry key="email" value="email"/>
					<entry key="mobile" value="mobile"/>
					<entry key="nick_name" value="nickName"/>
                  </map>
             </property>
     </bean>


    <!-- 
    Sample, in-memory data store for the ServiceRegistry. A real implementation
    would probably want to replace this with the JPA-backed ServiceRegistry DAO
    The name of this bean should remain "serviceRegistryDao".
    +-->
    <bean id="serviceRegistryDao" class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl"
            p:registeredServices-ref="registeredServicesList" />


    <util:list id="registeredServicesList">
        <bean class="org.jasig.cas.services.RegexRegisteredService"
              p:id="0" p:name="HTTP and IMAP" p:description="Allows HTTP HTTPS and IMAPS protocols"
              p:serviceId="^(http?|https?|imaps?)://.*" p:evaluationOrder="10000001" />
    </util:list>


	<!--begin 从数据库中的用户表中读取 -->
    <bean  id="MD5PasswordEncoder"   class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder"   autowire="byName">   
        <constructor-arg value="MD5"/>
    </bean>


   <bean   id="queryDatabaseAuthenticationHandler"   name="primaryAuthenticationHandler"    class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">  
        <property  name="passwordEncoder"   ref="MD5PasswordEncoder"/>
    </bean> 


   <alias  name="dataSource"  alias="queryDatabaseDataSource"/>


   <bean  id="dataSource"
      class="com.mchange.v2.c3p0.ComboPooledDataSource"
      p:driverClass="com.mysql.jdbc.Driver"
      p:jdbcUrl="jdbc:mysql://127.0.0.1:3306/sys_core?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull&amp;useSSL=false"
      p:user="root"
      p:password="123456"
      p:initialPoolSize="6"
      p:minPoolSize="6"
      p:maxPoolSize="18"
      p:maxIdleTimeExcessConnections="1200"
      p:checkoutTimeout="10000"
      p:acquireIncrement="6"
      p:acquireRetryAttempts="5"
      p:acquireRetryDelay="2000"
      p:idleConnectionTestPeriod="30"
      p:preferredTestQuery="select 1"/>
    <!--end  从数据库中的用户表中读取 -->


     <alias name="personDirectoryPrincipalResolver" alias="primaryPrincipalResolver" />


    <alias name="serviceThemeResolver" alias="themeResolver" />


    <alias name="jsonServiceRegistryDao" alias="serviceRegistryDao" />


    <alias name="defaultTicketRegistry" alias="ticketRegistry" />
    
    <alias name="ticketGrantingTicketExpirationPolicy" alias="grantingTicketExpirationPolicy" />
    <alias name="multiTimeUseOrTimeoutExpirationPolicy" alias="serviceTicketExpirationPolicy" />


    <alias name="anyAuthenticationPolicy" alias="authenticationPolicy" />
    <alias name="acceptAnyAuthenticationPolicyFactory" alias="authenticationPolicyFactory" />


    <bean id="auditTrailManager"
          class="org.jasig.inspektr.audit.support.Slf4jLoggingAuditTrailManager"
          p:entrySeparator="${cas.audit.singleline.separator:|}"
          p:useSingleLine="${cas.audit.singleline:false}"/>


    <alias name="neverThrottle" alias="authenticationThrottle" />


    <util:list id="monitorsList">
        <ref bean="memoryMonitor" />
        <ref bean="sessionMonitor" />
    </util:list>


    <alias name="defaultPrincipalFactory" alias="principalFactory" />
    <alias name="defaultAuthenticationTransactionManager" alias="authenticationTransactionManager" />
    <alias name="defaultPrincipalElectionStrategy" alias="principalElectionStrategy" />
    <alias name="tgcCipherExecutor" alias="defaultCookieCipherExecutor" />


	<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"  
                    p:httpClient-ref="httpClient"/>
</beans>

      

    在相应的cas.properties文件中找到# cas.jdbc.authn.query.sql=,添加查询语句,如下

# cas.jdbc.authn.query.sql=
cas.jdbc.authn.query.sql=select password from s_user where user_name=?
#cas.jdbc.authn.query.sql=select password from t_user where account=?
    

    数据库的主要字段如下图

    经过测试需要在apache-tomcat-8.0.39_dist\webapps\cas\WEB-INF\lib中添加

 mysql-connector-java-5.1.41.jar

 cas-server-support-jdbc-4.2.7.jar

 commons-collections4-4.1.jar三个包,否则有异常。

 配置完成后重启tomcat,使用数据库中的用户名和密码登录,跳转到一下页面表示成功



    5.服务器其他一些配置

     cas4.2以上做了一些框架的优化,4.2以下的版本一般都在xml文件中配置,4.2以上统一提取到了cas.properties文件中。以下是我修改的一些配置

      #默认显示中文
      locale.default=zh_CN

      # 使用http,如果不配置将不能生成相应的cookie,不能达到单点登录的效果
      tgc.secure=false

      warn.cookie.secure=false

      

      server.name=https:127.0.0.1:8070

      host.name=org.zkyg

      修改apache-tomcat-8.0.39_dist\webapps\cas\WEB-INF\classes\services目录下的Apereo-10000002.json文件

       "serviceId" : "^(https|imaps)://.*"   改为"^http.*"


 至此服务端配置完成。

 


     

      






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值