Cas 4. X single-point logon practices, cas4.x single-point operations
Tools used
Maven-3.3.9
Cas-4.1.4
Tomcat-7.0.57-win-x64
Cas-sample-java-webapp
1. Hello cas
1. Download Tomcat, decompress it, modify its server. xml, and add SSL support (Baidu ):
1 <Connector SSLEnabled="true" acceptCount="100" clientAuth="false"2 disableUploadTimeout="true" enableLookups="false" maxThreads="25"3 port="8443" keystoreFile="G:/work/xxx/tomcat.keystore" keystorePass="YOUR PASSWORD HERE"4 protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="https"5 secure="true" sslProtocol="TLS" />
1. Download the version of cas-server from the https://github.com/Jasig/cas/releases;
2. decompress the package and use the maven command mvn package to compile and package cas. Two problems may occur here:
A. failed to pass the unit test = "try to add-Dmaven. test. skip = true to skip;
B. compile to half of the compilation, but enter "=" to go to "cas-server-core" and run the command "mvn install-Dmaven. test. skip = true: Compile the core into a jar package before proceeding.
3. Copy the war package under the packaged cas-server-webapp to tomcat. Run tomcat first;
4. Use the account casuser and password Mellon to log on successfully.
5, download java-cas-client from the https://github.com/Jasig/java-cas-client, the above steps decompress the compilation, no operation after compilation, to be used.
6. Download cas-sample-java-webapp from the https://github.com/UniconLabs/cas-sample-java-webapp, decompress the above steps and compile it.
Ii. server configuration database connection Authentication
The above is the default configuration, the login account is actually in the cas/WEB-INF/deployerConfigContext. xml configuration is written to death. Of course it does not meet most of our actual use cases. Now we will change it to connect to the Mysql database for verification.
1. Go back to the cas directory compiled by maven and copy the cas-server-support-jdbc/target/cas-server-support-jdbc-4.1.4.jar to tomcat/webapp/cas/WEB-INF/lib, copy the mysql-connector-java-5.0.8-bin.jar to tomcat/webapp/cas/WEB-INF/lib. Of course, you can add dependencies in pom. xml under cas-server-webapp and then package and compile these dependencies.
2. Modify cas/WEB-INF/deployerConfigContext. xml. here you need to modify the following:
1 <bean id="primaryAuthenticationHandler"2 class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler">3 <property name="users">4 <map>5 <entry key="casuser" value="Mellon"/>6 </map>7 </property>8 </bean>
Comment out the above Code and add the following code:
1 <bean id="dataSource" 2 class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 3 <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 4 <property name="url" 5 value="jdbc:mysql://10.3.64.87:3306/cas?characterEncoding=utf-8&autoReconnect=true" /> 6 <property name="username" value="root" /> 7 <property name="password" value="123456" /> 8 </bean> 9 10 <bean id="primaryAuthenticationHandler"11 class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"12 p:dataSource-ref="dataSource"13 p:passwordEncoder-ref="MD5PasswordEncoder"14 p:sql="select password from user_info where user_name=?" />15 16 <bean id="MD5PasswordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder">17 <constructor-arg index="0">18 <value>MD5</value>19 </constructor-arg>20 </bean>
Here we have added the MySQL Data Connection dataSource and compiled an SQL statement to query user authentication information.
The modification is complete. Let's test whether MySQL can be used for authentication. Start tomcat.
Login successful. We have successfully connected to MYSQL.
3. Add a client
Here we will use the cas-sample-java-webapp to be used in step 1.
1. Deploy the compiled cas-sample-java-webapp/target/cas-sample-java-webapp.war to tomcat;
2. Find tomcat/webapp/cas-sample-java-webapp/WEB-INF/web. xml and delete the following code:
1 <init-param> 2 <param-name>redirectAfterValidation</param-name> 3 <param-value>true</param-value> 4 </init-param> 5 <init-param> 6 <param-name>useSession</param-name> 7 <param-value>true</param-value> 8 </init-param> 9 <init-param>10 <param-name>acceptAnyProxy</param-name>11 <param-value>true</param-value>12 </init-param>13 <init-param>14 <param-name>proxyReceptorUrl</param-name>15 <param-value>/client/proxyUrl</param-value>16 </init-param>17 <init-param>18 <param-name>proxyCallbackUrl</param-name>19 <param-value>https://zhuajindian.com:8443/client/proxyUrl</param-value>20 </init-param>
Replace multiple casServerUrlPrefix, serverName, and casServerLoginUrl with your own URL.
3. Modify deployerConfigContext. xml and find the following code:
1 <bean id="serviceRegistryDao" class="org.jasig.cas.services.JsonServiceRegistryDao"2 c:configDirectory="${service.registry.config.location:classpath:services}" />
Replace
1 <! -- Register the service --> 2 <bean id = "serviceRegistryDao" class = "org. jasig. cas. services. inMemoryServiceRegistryDaoImpl "3 p: registeredServices-ref =" registeredServicesList "/> 4 5 <util: list id =" registeredServicesList "> 6 <bean class =" org. jasig. cas. services. regexRegisteredService "7 p: id =" 0 "p: name =" HTTP and IMAP "p: description =" Allows HTTP (S) and IMAP (S) protocols "8 p: serviceId = "^ (https? | Http? | Imaps ?) : //. * "P: evaluationOrder =" 10000001 "type =" codeph "text =" codeph "/> 9 </util: list>
The serviceId here is what you tell the cas server to judge through this regular expression. What matches this regular expression is our own client. Of course, this is a wildcard match. You can use it based on your actual situation.
4. Restart tomcat and access the URL that is just filled in serverName.
We can see that we have successfully obtained the Authenticated UserId, that is, the login username. In this way, our simple demo is complete.
4. Customize the passable fields after logon to facilitate client reading
In our application scenarios, the parameters required by the client are not just the user name. We also need information such as userid. Next we will configure to obtain custom fields.
1. Find cas/WEB-INF/deployerConfigContext. xml and comment out the following code:
1 <bean id="attributeRepository" class="org.jasig.services.persondir.support.NamedStubPersonAttributeDao" 2 p:backingMap-ref="attrRepoBackingMap" /> 3 4 <util:map id="attrRepoBackingMap"> 5 <entry key="uid" value="uid" /> 6 <entry key="eduPersonAffiliation" value="eduPersonAffiliation" /> 7 <entry key="groupMembership" value="groupMembership" /> 8 <entry> 9 <key><value>memberOf</value></key>10 <list>11 <value>faculty</value>12 <value>staff</value>13 <value>org</value>14 </list>15 </entry>16 </util:map>
Replace:
1 <bean id="attributeRepository" class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao"> 2 <constructor-arg index="0" ref="dataSource" /> 3 <constructor-arg index="1" value="SELECT id,user_name,mobile,cid FROM user_info WHERE {0}" /> 4 <property name="queryAttributeMapping"> 5 <map> 6 <entry key="username" value="user_name" /> 7 </map> 8 </property> 9 <property name="resultAttributeMapping">10 <map>11 <entry key="id" value="userId" />12 <entry key="user_name" value="username" />13 <entry key="mobile" value="mobile" />14 <entry key="cid" value="cid" />15 </map>16 </property>17 </bean>
The SQL statement only needs to write the first half. For example, the key of the entry represents the field queried by the preceding SQL statement, and the value represents the parameter name that the server sends to the client, the client can retrieve the corresponding value through value.
2, modify cas/WEB-INF/view/jsp/protocol/2.0/casServiceValidationSuccess. jsp, add the following section
1 <cas: serviceResponse xmlns: cas = 'HTTP: // www.yale.edu/tp/cas'> 2 <cas: authenticationSuccess> 3 <cas: user >$ {fn: escapeXml (principal. id)} </cas: user> 4 <! -- This section --> 5 <c: if test = "$ {fn: length (assertion. chainedAuthentications [fn: length (assertion. chainedAuthentications)-1]. principal. attributes)> 0} "> 6 <cas: attributes> 7 <c: forEach var =" attr "items =" $ {assertion. chainedAuthentications [fn: length (assertion. chainedAuthentications)-1]. principal. attributes} "> 8 <cas: $ {fn: escapeXml (attr. key) }>$ {fn: escapeXml (attr. value)} </cas :$ {fn: escapeXml (attr. key)}> 9 </c: f OrEach> 10 </cas: attributes> 11 </c: if> 12 <! -- End --> 13 <c: if test = "$ {not empty pgtIou}"> 14 <cas: proxyGrantingTicket >$ {pgtIou} </cas: proxyGrantingTicket> 15 </c: if> 16 <c: if test = "$ {fn: length (chainedAuthentications)> 0}"> 17 <cas: proxies> 18 <c: forEach var = "proxy" items = "$ {chainedAuthentications}" varStatus = "loopStatus" begin = "0" end = "$ {fn: length (chainedAuthentications )} "step =" 1 "> 19 <cas: proxy >$ {fn: escapeXml (proxy. principal. id)} </cas: proxy> 20 </c: forEach> 21 </cas: proxies> 22 </c: if> 23 </cas: authenticationSuccess> 24 </cas: serviceResponse>
3. Restart tomcat and log on to the system:
The four parameters configured above show three. The original mobile field is not passed by default because there is no value.