本文简单实现在spring框架下对apache shiro与jasig-cas 整合 实现单点登录多点注销统一权限管理平台,功能简单,主要大致讲讲入门配置。
一、环境:
1、后台框架:spring3.2+struts2+hibernate4+apache-shiro1.2.1+jasig-cas3.4.1(具体相关依赖包请自行上官网下载);
2、数据库:mysql5.x;
3、servlet容器:tomcat6;
二、首先CAS服务器搭建
1、官网下载cassever包,解压后将modules文件夹里的 cas-server-webapp-*.war 复制到 tomcat webapps文件夹下;
2、启动tomcat,输入相同的用户名和密码登录,将提示登录成功,这个cas默认的认证方式,刷新页面将回到登录页面;
3、修改cas默认的认证方式: 做web开发,我们一般用数据库用户密码方式认证:
a、修改web-inf下的deployerConfigContext.xml,注释掉默认的验证:
<bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />
b、添加获取认证信息的数据源:
<bean id="casdataSource" name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" >
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value><![CDATA[jdbc:mysql://localhost:3306/cas?useUnicode=true&characterEncoding=UTF-8]]></value>
</property>
<property name="username" >
<value>root</value>
</property>
<property name="password" >
<value>root</value>
</property>
</bean>
c、在注释掉的默认验证后增加bean,以下表示从该数据源的t_user表中获取认证信息,需要使用加密的自行google
<bean id="QueryDatabaseAuthenticationHandler" class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
<property name="dataSource" ref="casdataSource"></property>
<property name="sql" value="select USER_PWD from T_USER where USER_NAME=?"></property>
</bean>
4、cas协议,cas默认使用https协议,如果对安全要求不高,可使用http协议,我这里使用http协议
a、修改deployerConfigContext.xml,
<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
p:httpClient-ref="httpClient" p:requireSecure="false"/><!-- require 属性为true 开启ssl -->
b、修改spring-configuration下的ticketGrantingTicketCookieGenerator.xml
<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
p:cookieSecure="false"
p:cookieMaxAge="-1"
p:cookieName="CASTGC"
p:cookiePath="/cas" />
<!-- 使用http协议: cookieSecure 为false -->
</beans>
以上,完成使用jdb数据源http协议的简单cas server端配置,可以启动tomcat 输入地址查看,这时候认证将需要与数据库里的用户信息匹配还能认证成功!
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICdzFWRoRXdvN1LclHdpZXYyd2LcBzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX90zdihGbXJmZW1mY2J1MjZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39zMwczNwEzMxETOxcDM0EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
三、客户端client1配置
client1 相关其他配置就不详讲了,主要讲述spring与shiro与cas的相关配置
1、首先引入客户端所需jar包:spring相关jar包,shiro jar包,cas client jar包(下载cas client modules 下会有cas-client-core-.jar)
2、web.xml 相关配置
加载相关配置文件与监听
<context-param>
<description>相关配置文件加载</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring*.xml</param-value>
</context-param>
<!-- 相关监听加载 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>com.areo.kongtan.listener.SessionListenerHandler</listener-class>
</listener>
shiro filter配置,shiro在1.2后开始支持的cas
<filter>
<description>
shrio权限过滤,DelegatingFilterProxy 作用是自动到 spring 容器查找名字为 shiroFilter(filter-name)的 bean并把所有 Filter 的操作委托给它
</description>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
以上完成client端的web配置 下一篇讲client端spring整合相关配置