一、准备所需jar包
cas\WEB-INF\lib中加入 c3p0-0.9.1.2.jar包、mysql-connector-java-5.1.21.jar包和cas-server-support-jdbc-4.0.0.jar包。
c3p0的jar包和mysql驱动的jar包可以在maven上拉下来,
cas-server-support-jdbc-4.0.0.jar 这个jar包可以在cas-server-4.0.0\modules目录下找到
二、Cas-Server(服务端)自定义验证规则
1、打开deployerConfigContext.xml
找到 primaryAuthenticationHandler ,因为实际开发中我们需要跟数据库做校验所以这段代码就需要注掉然后改为我们的验证规则
2、在deployerConfigContext.xml中给Cas配置数据源
class="com.mchange.v2.c3p0.ComboPooledDataSource" p:driverClass="com.mysql.jdbc.Driver" p:jdbcUrl="jdbc:mysql://localhost:3306/test3?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull" p:user="xxx" p:password="xxx" />
3、在deployerConfigContext.xml中给定义加密方式
class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" c:encodingAlgorithm="MD5" p:characterEncoding="UTF-8" />
4、在deployerConfigContext.xml中配置查询语句,这里的p:passwordEncoder对应着加密方式 p:dataSource 这个指向的就是之前配置的数据源
class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler" p:dataSource-ref="dataSource" p:sql="select password from cas_user where username=? and data_status='1'" p:passwordEncoder-ref="passwordEncoder" />
5、在deployerConfigContext.xml中配置新的验证规则,找到这段代码将这段代码注掉,细心的朋友或许已经发现primaryAuthenticationHandler
这个key指向的就是Cas 提供的默认账号密码配置,也就是我们一上来就注掉的呢段
三、CAS-Client(客户端)配置
1.打开 pom.xml文件引入client(客户端)jar包
org.jasig.cas.client cas-client-core 3.2.1
2、打开 web.xml配置加入cas配置
CAS Single Sign Out Filter class>org.jasig.cas.client.session.SingleSignOutFilter class>CAS Authentication Filter class>org.jasig.cas.client.authentication.AuthenticationFilter class>casServerLoginUrl http://localhost:8080/cas/login serverName http://localhost:8080 CAS Validation Filter class>org.jasig.cas.client.validation.Cas10TicketValidationFilter class>casServerUrlPrefix http://localhost:8080/cas serverName http://localhost:8080 redirectAfterValidation true CAS HttpServletRequest Wrapper Filter class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter class>CAS Assertion Thread Local Filter class>org.jasig.cas.client.util.AssertionThreadLocalFilter class>CAS Single Sign Out Filter /* CAS Authentication Filter /* CAS Validation Filter /* CAS HttpServletRequest Wrapper Filter /* CAS Assertion Thread Local Filter /* org.jasig.cas.client.session.SingleSignOutHttpSessionListener
四、运行测试
CAS自动拦截(PS:没有登录当然要拦截了)
随便输入点信息看看验证成功没有~~
输入正确的账号密码登录系统成功