这次我给大家讲讲如何在shiro中整合cas框架,以及扩展自定义的角色和资源体系,啰嗦话不多说了,直接上代码说明
第一步,搭建cas服务器,我也不说拉,这个大家用现有的cas服务就行了
第二步,先加入cas-client的包到我们的项目,然后再下载个shiro-cas.jar也放到项目里
第三步配置shiro中的cas设置
<description>shiro配置</description>
<!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="shiroCacheManager" />
<property name="sessionManager" ref="sessionManager" />
<property name="realm" ref="casRealm" />
<property name="subjectFactory" ref="casSubjectFactory" />
<!-- <property name="realm" ref="simpleUserRealm" /> -->
</bean>
<!-- 会话管理器 -->
<bean id="sessionManager"
class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionValidationSchedulerEnabled" value="false" />
<property name="sessionDAO" ref="sessionDAO" />
<property name="globalSessionTimeout" value="600000" />
</bean>
<!-- 缓存管理器 -->
<bean id="shiroCacheManager"
class="com.silvery.security.shiro.cache.SimpleShiroCacheManager">
<property name="cache" ref="shiroCache" />
</bean>
<!-- 缓存实现类,注入自定义缓存机制 -->
<bean id="shiroCache" class="com.silvery.security.shiro.cache.SimpleShiroCache">
<property name="cacheManager" ref="simpleCacheManager" />
</bean>
<!-- 会话读写实现类 -->
<bean id="sessionDAO" class="com.silvery.security.shiro.session.CacheSessionDAO" />
<!-- 用户认证实现 -->
<bean id="simpleUserRealm" class="com.silvery.security.shiro.realm.SimpleUserRealm" />
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<bean id="casFilter" class="org.apache.shiro.cas.CasFilter">
<!-- 配置验证错误时的失败页面 -->
<property name="failureUrl"
value="https://cas.test.com:8443/login?service=http://test.com/mh/cas/login.do" />
</bean>
<bean id="casRealm" class="com.silvery.security.shiro.realm.SimpleCasRealm">
<property name="defaultRoles" value="ROLE_USER" />
<property name="casServerUrlPrefix" value="https://cas.test.com:8443" />
<!-- 客户端的回调地址设置,必须和下面的过滤器拦截的地址一致 -->
<property name="casService" value="http://test.com/mh/cas/login.do" />
</bean>
<!-- 如果要实现cas的remember me的功能,需要用到下面这个bean,并设置到securityManager的subjectFactory中 -->
<bean id="casSubjectFactory" class="org.apache.shiro.cas.CasSubjectFactory" />
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod"
value="org.apache.shiro.SecurityUtils.setSecurityManager" />
<property name="arguments" ref="securityManager" />
</bean>
<!-- 过滤链配置 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl"
value="https://cas.test.com:8443/login?service=http:/