CAS4.0关联mysql数据库

来源:互联网 发布:数据移植测试 编辑:程序博客网 时间:2024/05/16 07:53

步骤如下(亲测可用)

一:

在cas-4.0.0\cas-server-webapp\pom.xml中添加依赖后(如下方所示),打开cmd在cas-4.0.0\cas-server-webapp文件夹下运行mvn clean package,然后将cas-4.0.0\cas-server-webapp\target下的cas.war包部署至tomcat

Xml代码  收藏代码
  1. <dependency>  
  2.     <groupId>org.jasig.cas</groupId>  
  3.     <artifactId>cas-server-support-jdbc</artifactId>  
  4.     <version>${project.version}</version>  
  5.     <type>jar</type>  
  6. </dependency>  
  7.   
  8. <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->  
  9. <dependency>  
  10.     <groupId>commons-dbcp</groupId>  
  11.     <artifactId>commons-dbcp</artifactId>  
  12.     <version>1.4</version>  
  13. </dependency>  
  14.   
  15. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->  
  16. <dependency>  
  17.     <groupId>mysql</groupId>  
  18.     <artifactId>mysql-connector-java</artifactId>  
  19.     <version>5.1.6</version>  
  20. </dependency>  

 

二:

本地创建数据库,并新建表 cas_user,创建语句如下载

Sql代码  收藏代码
  1. create table cas_user (  
  2.     id bigint not null auto_increment,  
  3.     email varchar(255),  
  4.     username varchar(255) not null unique,  
  5.     name varchar(255),  
  6.     password varchar(255),  
  7.     primary key (id)  
  8. ) ENGINE=InnoDB;  

 

三:

配置数据库相关文件,在tomcat-for-cas\webapps\cas\WEB-INF\deployerConfigContext.xml中配置对应的datasource,数据库地址,用户名,密码,以及查询用户的sql。需要注意的是,如果是自己建的表,要把相应的字段名,数据库名替换掉,以及,不要忘记注释掉默认用户名密码的配置(casuser/Mellon)。以下配置可以全拷贝下载

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!--  
  3.     Licensed to Jasig under one or more contributor license  
  4.     agreements. See the NOTICE file distributed with this work  
  5.     for additional information regarding copyright ownership.  
  6.     Jasig licenses this file to you under the Apache License,  
  7.     Version 2.0 (the "License"); you may not use this file  
  8.     except in compliance with the License.  You may obtain a  
  9.     copy of the License at the following location:  
  10.       http://www.apache.org/licenses/LICENSE-2.0  
  11.   
  12.     Unless required by applicable law or agreed to in writing,  
  13.     software distributed under the License is distributed on an  
  14.     "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  
  15.     KIND, either express or implied.  See the License for the  
  16.     specific language governing permissions and limitations  
  17.     under the License.  
  18. -->  
  19. <!--  
  20. | deployerConfigContext.xml centralizes into one file some of the declarative configuration that  
  21. | all CAS deployers will need to modify.  
  22. |  
  23. | This file declares some of the Spring-managed JavaBeans that make up a CAS deployment.    
  24. | The beans declared in this file are instantiated at context initialization time by the Spring   
  25. | ContextLoaderListener declared in web.xml.  It finds this file because this  
  26. | file is among those declared in the context parameter "contextConfigLocation".  
  27. |  
  28. | By far the most common change you will need to make in this file is to change the last bean  
  29. | declaration to replace the default authentication handler with  
  30. | one implementing your approach for authenticating usernames and passwords.  
  31. +-->  
  32. <beans xmlns="http://www.springframework.org/schema/beans"  
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  34.        xmlns:p="http://www.springframework.org/schema/p"  
  35.        xmlns:c="http://www.springframework.org/schema/c"  
  36.        xmlns:tx="http://www.springframework.org/schema/tx"  
  37.        xmlns:util="http://www.springframework.org/schema/util"  
  38.        xmlns:sec="http://www.springframework.org/schema/security"  
  39.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  40.        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
  41.        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd  
  42.        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">  
  43.     <!--  
  44.        | The authentication manager defines security policy for authentication by specifying at a minimum  
  45.        | the authentication handlers that will be used to authenticate credential. While the AuthenticationManager  
  46.        | interface supports plugging in another implementation, the default PolicyBasedAuthenticationManager should  
  47.        | be sufficient in most cases.  
  48.        +-->  
  49.     <bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager">  
  50.         <constructor-arg>  
  51.             <map>  
  52.                 <!--  
  53.                    | IMPORTANT  
  54.                    | Every handler requires a unique name.  
  55.                    | If more than one instance of the same handler class is configured, you must explicitly  
  56.                    | set its name to something other than its default name (typically the simple class name).  
  57.                      
  58.                    -->  
  59.                     <entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver" />  
  60.                  <entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" />  
  61.             </map>  
  62.         </constructor-arg>  
  63.         <!-- Uncomment the metadata populator to allow clearpass to capture and cache the password  
  64.              This switch effectively will turn on clearpass.  
  65.         <property name="authenticationMetaDataPopulators">  
  66.            <util:list>  
  67.               <bean class="org.jasig.cas.extension.clearpass.CacheCredentialsMetaDataPopulator"  下载
  68.                     c:credentialCache-ref="encryptedMap" />  
  69.            </util:list>  
  70.         </property>  
  71.         -->  
  72.         <!--  
  73.            | Defines the security policy around authentication. Some alternative policies that ship with CAS:  
  74.            |  
  75.            | * NotPreventedAuthenticationPolicy - all credential must either pass or fail authentication  
  76.            | * AllAuthenticationPolicy - all presented credential must be authenticated successfully  
  77.            | * RequiredHandlerAuthenticationPolicy - specifies a handler that must authenticate its credential to pass  
  78.            -->  
  79.         <property name="authenticationPolicy">  
  80.             <bean class="org.jasig.cas.authentication.AnyAuthenticationPolicy" />  
  81.         </property>  
  82.     </bean>  
  83.     <!-- Required for proxy ticket mechanism. -->  
  84.     <bean id="proxyAuthenticationHandler"  
  85.           class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"  
  86.           p:httpClient-ref="httpClient" p:requireSecure="true" />  
  87.     <!--  
  88.        | TODO: Replace this component with one suitable for your enviroment.  
  89.        |  
  90.        | This component provides authentication for the kind of credential used in your environment. In most cases  
  91.        | credential is a username/password pair that lives in a system of record like an LDAP directory.  
  92.        | The most common authentication handler beans:  
  93.        |  
  94.        | * org.jasig.cas.authentication.LdapAuthenticationHandler  
  95.        | * org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler  
  96.        | * org.jasig.cas.adaptors.x509.authentication.handler.support.X509CredentialsAuthenticationHandler  
  97.        | * org.jasig.cas.support.spnego.authentication.handler.support.JCIFSSpnegoAuthenticationHandler  
  98.        -->  
  99.     <bean id="primaryAuthenticationHandler"  
  100.           class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">  
  101.           <property name="dataSource" ref="dataSource"/>  
  102.           <property name="sql" value="select password from cas_user where username = ?"/>  
  103.     </bean>  
  104.   
  105.     <!-- Required for proxy ticket mechanism -->  
  106.     <bean id="proxyPrincipalResolver"  
  107.           class="org.jasig.cas.authentication.principal.BasicPrincipalResolver" />  
  108.     <!--  
  109.        | Resolves a principal from a credential using an attribute repository that is configured to resolve  
  110.        | against a deployer-specific store (e.g. LDAP).  
  111.        -->  
  112.     <bean id="primaryPrincipalResolver"  
  113.           class="org.jasig.cas.authentication.principal.PersonDirectoryPrincipalResolver" >  
  114.         <property name="attributeRepository" ref="selfAttributeRepository" />  
  115.     </bean>  
  116.   
  117.     <!--  
  118.     Bean that defines the attributes that a service may return.  This example uses the Stub/Mock version.  A real implementation  
  119.     may go against a database or LDAP server.  The id should remain "attributeRepository" though.  
  120.     +-->  
  121.     <bean id="attributeRepository" class="org.jasig.services.persondir.support.StubPersonAttributeDao"  
  122.             p:backingMap-ref="attrRepoBackingMap" />  
  123.       
  124.     <util:map id="attrRepoBackingMap">  
  125.         <entry key="uid" value="uid" />  
  126.         <entry key="eduPersonAffiliation" value="eduPersonAffiliation" />   
  127.         <entry key="groupMembership" value="groupMembership" />  
  128.     </util:map>  
  129.     <!--   
  130.     Sample, in-memory data store for the ServiceRegistry. A real implementation  
  131.     would probably want to replace this with the JPA-backed ServiceRegistry DAO  
  132.     The name of this bean should remain "serviceRegistryDao".  
  133.     +-->  
  134.     <bean id="serviceRegistryDao" class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl"  下载
  135.             p:registeredServices-ref="registeredServicesList" />  
  136.     <util:list id="registeredServicesList">  
  137.         <bean class="org.jasig.cas.services.RegexRegisteredService"  
  138.               p:id="0" p:name="HTTP and IMAP" p:description="Allows HTTP(S) and IMAP(S) protocols"  
  139.               p:serviceId="^(https?|imaps?)://.*" p:evaluationOrder="10000001" />  
  140.         <!--  
  141.         Use the following definition instead of the above to further restrict access  
  142.         to services within your domain (including sub domains).  
  143.         Note that example.com must be replaced with the domain you wish to permit.  
  144.         This example also demonstrates the configuration of an attribute filter  
  145.         that only allows for attributes whose length is 3.  
  146.         -->  
  147.         <!--  
  148.         <bean class="org.jasig.cas.services.RegexRegisteredService">  
  149.             <property name="id" value="1" />  
  150.             <property name="name" value="HTTP and IMAP on example.com" />  
  151.             <property name="description" value="Allows HTTP(S) and IMAP(S) protocols on example.com" />  
  152.             <property name="serviceId" value="^(https?|imaps?)://([A-Za-z0-9_-]+\.)*example\.com/.*" />  
  153.             <property name="evaluationOrder" value="0" />  
  154.             <property name="attributeFilter">  
  155.               <bean class="org.jasig.cas.services.support.RegisteredServiceRegexAttributeFilter" c:regex="^\w{3}$" />   
  156.             </property>  
  157.         </bean>  
  158.         -->  
  159.     </util:list>  
  160.       
  161.     <bean id="auditTrailManager" class="com.github.inspektr.audit.support.Slf4jLoggingAuditTrailManager" />  
  162.       
  163.     <bean id="healthCheckMonitor" class="org.jasig.cas.monitor.HealthCheckMonitor" p:monitors-ref="monitorsList" />  
  164.     
  165.     <util:list id="monitorsList">  
  166.       <bean class="org.jasig.cas.monitor.MemoryMonitor" p:freeMemoryWarnThreshold="10" />  
  167.       <!--  
  168.         NOTE  
  169.         The following ticket registries support SessionMonitor:  
  170.           * DefaultTicketRegistry  
  171.           * JpaTicketRegistry  
  172.         Remove this monitor if you use an unsupported registry.  
  173.       -->  
  174.       <bean class="org.jasig.cas.monitor.SessionMonitor"  
  175.           p:ticketRegistry-ref="ticketRegistry"  
  176.           p:serviceTicketCountWarnThreshold="5000"  
  177.           p:sessionCountWarnThreshold="100000" />  
  178.     </util:list>  
  179.       
  180.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  181.                    <property name="driverClassName">  
  182.                            <value>com.mysql.jdbc.Driver</value>  
  183.                    </property>  
  184.                    <property name="url">  
  185.                             <value>jdbc:mysql://localhost:3306/test</value>  
  186.                    </property>  
  187.                    <property name="username">  
  188.                             <value>root</value>  
  189.                    </property>  
  190.                    <property name="password">  
  191.                             <value>123456</value>  
  192.                    </property>  
  193.     </bean>  
  194.       
  195.     <bean id="selfAttributeRepository"  
  196.   class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao">  
  197.   <constructor-arg index="0" ref="dataSource" />  
  198.   <constructor-arg index="1"  
  199.    value="select username,password from cas_user where {0}" />  
  200.     
  201.   <!-- 组装sql用的查询条件属性 -->   
  202.   <property name="queryAttributeMapping">  
  203.    <map>  
  204.        <!-- key必须是uername而且是小写否则会导致取不到用户的其它信息,value对应数据库用户名字段,系统会自己匹配 -->  
  205.     <entry key="username" value="username" />  
  206.     <entry key="password" value="password" />  
  207.    </map>  
  208.   </property>  
  209.   <property name="resultAttributeMapping">  
  210.    <map>  
  211.        <!-- key为对应的数据库字段名称,value为提供给客户端获取的属性名字,系统会自动填充值 -->  
  212.     <entry key="username" value="username"></entry>  
  213.     <entry key="password" value="password"></entry>  
  214.    </map>  
  215.   </property>  
  216.  </bean>   
  217. </beans>  
0 0