CAS配置全过程说明:这里只是简单的配置了CAS单点登录的过程,并没有加入复杂的验证,也没有做MD5的校验。
输入数据库中存在的用户名跟密码就会登录成功一.首先需要下载cas-server-3.4.2-release.zip包,这个包里面包含CAS Server服务器的war包。
解压之后放到找到cas-server-3.4.2\modules 下的cas-server-webapp-3.4.2.war , 拷tomcat\webapps\下并改名为cas.war。
启动tomcat后会在webapps下看到cas文件,然后停止tomcat.二. 配置tomcat以及修改cas server的配置1.生成证书并导入到jdk的jre中请直接运行附件中的.bat文件会自动把证书生成在D盘根目录下并导入到jre 中这是bat脚本内容keytool -genkey -alias tomcatgecko -keyalg RSA -keystore d:\mykeystore -dname "CN=gecko-4d4611f2e, OU=gecko-4d4611f2e, O=gecko-4d4611f2e, L=SH, ST=SH, C=CN"-keypass changeit -storepass changeitkeytool -export -alias tomcatgecko -keystore d:\mykeystore -file d:\mycerts.crt -storepass changeitkeytool -import -keystore "%JAVA_HOME%/JRE/LIB/SECURITY/CACERTS" -file d:\mycerts.crt -alias tomcatgecko这里要注意:证书是要导入到tomcat使用的jdk。
不然CAS在跳转时会找不到本地域名脚本中红色的部分是写自己的计算机名称然后在C:\WINDOWS\system32\drivers\etc\hosts配置自己的本地域名,跟计算机名称相同2.启动tomcat之后会在tomcat\webapps下看到cas.war解压出来之后的cas文件夹;找到tomcat\conf\server.xml文件。
在xml中加入<Connector protocol="org.apache.coyote.http11.Http11NioProtocol"port="8443" minSpareThreads="5" maxSpareThreads="75"enableLookups="true" disableUploadTimeout="true"acceptCount="100" maxThreads="200"scheme="https" secure="true" SSLEnabled="true"clientAuth="false" sslProtocol="TLS"keystoreFile="D:\mykeystore"keystorePass="changeit"/>keystoreFile=”” 是指定证书的路径keystorePass=”” 是证书的密码3. 修改cas server 服务器配置打到cas\WEB-INF\deployerConfigContext.xml注释掉<!--<beanclass="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthentic ationHandler" />-->加入<bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"><property name="sql" value="select password from portal_user where username=?" /><property name="dataSource" ref="dataSource"/></bean>红色部分是sql语句,可以根据自己的需求修改修改后文件如下:<?xml version="1.0" encoding="UTF-8"?><!--| deployerConfigContext.xml centralizes into one file some of the declarative configuration that| all CAS deployers will need to modify.|| This file declares some of the Spring-managed JavaBeans that make up a CAS deployment.| The beans declared in this file are instantiated at context initialization time by the Spring| ContextLoaderListener declared in web.xml. It finds this file because this| file is among those declared in the context parameter "contextConfigLocation".|| By far the most common change you will need to make in this file is to change the last bean| declaration to replace the default SimpleTestUsernamePasswordAuthenticationHandler with| one implementing your approach for authenticating usernames and passwords.+--><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:p="/schema/p"xmlns:sec="/schema/security"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-3.0.xsd/schema/security/schema/security/spring-security-3.0.xsd"><!--| This bean declares our AuthenticationManager. The CentralAuthenticationService service bean| declared in applicationContext.xml picks up this AuthenticationManager by reference to its id,| "authenticationManager". Most deployers will be able to use the default AuthenticationManager| implementation and so do not need to change the class of this bean. We include the whole| AuthenticationManager here in the userConfigContext.xml so that you can see the things you will| need to change in context.+--><bean id="authenticationManager"class="org.jasig.cas.authentication.AuthenticationManagerImpl"><!--| This is the List of CredentialToPrincipalResolvers that identify what Principal is trying to authenticate.| The AuthenticationManagerImpl considers them in order, finding a CredentialToPrincipalResolver which| supports the presented credentials.|| AuthenticationManagerImpl uses these resolvers for two purposes. First, it uses them to identify the Principal| attempting to authenticate to CAS /login . In the default configuration, it is the DefaultCredentialsToPrincipalResolver| that fills this role. If you are using some other kind of credentials than UsernamePasswordCredentials, you will need to replace| DefaultCredentialsToPrincipalResolver with a CredentialsToPrincipalResolver that supports the credentials you are| using.|| Second, AuthenticationManagerImpl uses these resolvers to identify a service requesting a proxy granting ticket.| In the default configuration, it is the HttpBasedServiceCredentialsToPrincipalResolver that serves this purpose.| You will need to change this list if you are identifying services by something more or other than their callback URL.+--><property name="credentialsToPrincipalResolvers"><list><!--| UsernamePasswordCredentialsToPrincipalResolver supports the UsernamePasswordCredentials that we use for /login| by default and produces SimplePrincipal instances conveying the username from the credentials.|| If you've changed your LoginFormAction to use credentials other than UsernamePasswordCredentials then you will also| need to change this bean declaration (or add additional declarations) to declare a CredentialsToPrincipalResolver that supports the| Credentials you are using.+--><beanclass="ernamePasswordCredentialsToPrincipalResolv er" /><!--| HttpBasedServiceCredentialsToPrincipalResolver supports HttpBasedCredentials. It supports the CAS 2.0 approach of| authenticating services by SSL callback, extracting the callback URL from the Credentials and representing it as a| SimpleService identified by that callback URL.|| If you are representing services by something more or other than an HTTPS URL whereat they are able to| receive a proxy callback, you will need to change this bean declaration (or add additional declarations).+--><beanclass="org.jasig.cas.authentication.principal.HttpBasedServiceCredentialsToPrincipalResolver " /></list></property><!--| Whereas CredentialsToPrincipalResolvers identify who it is some Credentials might authenticate,| AuthenticationHandlers actually authenticate credentials. Here we declare the AuthenticationHandlers that| authenticate the Principals that the CredentialsToPrincipalResolvers identified. CAS will try these handlers in turn| until it finds one that both supports the Credentials presented and succeeds in authenticating.+--><property name="authenticationHandlers"><list><!--| This is the authentication handler that authenticates services by means of callback via SSL, thereby validating| a server side SSL certificate.+--><beanclass="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticati onHandler"p:httpClient-ref="httpClient" /><!--| This is the authentication handler declaration that every CAS deployer will need to change before deploying CAS| into production. The defaultSimpleTestUsernamePasswordAuthenticationHandler authenticates UsernamePasswordCredentials| where the username equals the password. You will need to replace this with an AuthenticationHandler that implements your| local authentication strategy. You might accomplish this by coding a new such handler and declaring| edu.someschool.its.cas.MySpecialHandler here, or you might use one of the handlers provided in the adaptors modules.+--><!--<beanclass="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthentic ationHandler" />--><beanclass="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"><property name="sql" value="select password from app_user where username=?" /> <property name="dataSource" ref="dataSource" /></bean></list></property></bean><!--This bean defines the security roles for the Services Management application. Simple deployments can use the in-memory version.More robust deployments will want to use another option, such as the Jdbc version.The name of this should remain "userDetailsService" in order for Spring Security to find it.To use this, you should add an entry similar to the following between the two value tags:battags=notused,ROLE_ADMINwhere battags is the username you want to grant access to. You can put one entry per line.--><sec:user-service id="userDetailsService"><sec:user name="@@THIS SHOULD BE REPLACED@@" password="notused"authorities="ROLE_ADMIN" /></sec:user-service><!--Bean that defines the attributes that a service may return. This example uses the Stub/Mock version. A real implementationmay go against a database or LDAP server. The id should remain "attributeRepository" though.--><bean id="attributeRepository"class="org.jasig.services.persondir.support.StubPersonAttributeDao"><property name="backingMap"><map><entry key="uid" value="uid" /><entry key="eduPersonAffiliation" value="eduPersonAffiliation" /><entry key="groupMembership" value="groupMembership" /></map></property></bean><!--Sample, in-memory data store for the ServiceRegistry. A real implementationwould probably want to replace this with the JPA-backed ServiceRegistry DAOThe name of this bean should remain "serviceRegistryDao".--><beanid="serviceRegistryDao"class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl" /><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><propertyname="driverClassName"><value>com.mysql.jdbc.Driver</value></property> <propertyname="url"><value>jdbc:mysql://localhost:3306/portal</value></property><property name="username"><value>portal</value></property><property name="password"><value>portal</value></property></bean></beans>找到cas\WEB-INF\spring-configuration\ticketGrantingTicketCookieGenerator.xml并修改成<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:p="/schema/p"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-2.0.xsd"><description>Defines the cookie that stores the TicketGrantingTicket. You most likely should never modify these (especially the "secure" property).You can change the name if you want to make it harder for people to guess.</description><bean id="ticketGrantingTicketCookieGenerator"class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"p:cookieSecure="false"p:cookieMaxAge="-1"p:cookieName="CASTGC"p:cookiePath="/cas" /></beans>红色部分代表修改过的地方说明:请在附件中把cas-server-3.4.2-release.zip解压后找到cas-server-3.4.2\modules\cas-server-support-jdbc-3.4.2.jar,把它拷贝到cas\WEB-INF\lib下;还要把数据库驱动拷贝进来(看用的是什么数据库)。