SpringMvc 下集成shiro與cas


  由於內部系統越來越多,單點登錄已經是一個較優選擇。之前各個系統都集成了shiro作為權限管理,

所以必須要把shiro與cas集成。

  集成步驟:

首先在POM中引入shiro-cas包

<dependency>
  <groupId>org.apache.shiro</groupId>
  <artifactId>shiro-cas</artifactId>
  <version>1.2.2</version>
</dependency>

添加一個MyCasRealm繼承CasRealm,在其中重寫doGetAuthorizationInfo方法,用於設置權限

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

  String username = (String)principals.getPrimaryPrincipal();
  SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
  authorizationInfo.setRoles(roles);
  authorizationInfo.setStringPermissions(permission);
  return authorizationInfo;
}

 

在springShiro.xml(系統內部shiro配置文件)中添加

 

<bean id="casRealm" class="com.shijie99.order.common.shiro.MyCasRealm">
  <property name="cachingEnabled" value="true"/>
  <property name="authenticationCachingEnabled" value="true"/>
  <property name="authenticationCacheName" value="authenticationCache"/>
  <property name="authorizationCachingEnabled" value="true"/>
  <property name="authorizationCacheName" value="authorizationCache"/>
  <property name="casServerUrlPrefix" value="http://127.0.0.1:8080/cas"/>
  <property name="casService" value="http://127.0.0.1:8580/innersystem/cas"/>
</bean>

 

<bean id="casFilter" class="org.apache.shiro.cas.CasFilter">
<property name="failureUrl" value="/unauthorized"/>
</bean>

 

在shiroFilter中添加

<property name="loginUrl" value="http://127.0.0.1:8080/cas?service=http://127.0.0.1:8580/innersystem/cas"/>

在filters中添加

<entry key="cas" value-ref="casFilter"/>

在其filterChainDefinitions中添加

 /cas = cas

 

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="http://127.0.0.1:8080/cas?service=http://127.0.0.1:8580/innersystem/cas"/>
<property name="filters">
<util:map>
<!-- <entry key="authc" value-ref="formAuthenticationFilter"/> -->
<entry key="sysUser" value-ref="sysUserFilter"/>
<entry key="cas" value-ref="casFilter"/>
</util:map>
</property>
<property name="filterChainDefinitions">
<value>
/unauthorized = anon
/cas = cas
/logout = logout
/resources/** = anon
/** = user
</value>
</property>
</bean>

 

需要注意的是,之前在casRealm中存在

<property name="credentialsMatcher" ref="credentialsMatcher"/> 

這個配置,集成后一直報重定向循環。

由於credentialsMatcher是繼承了HashedCredentialsMatcher,這個方法認證ticket是用

char[]類型的,導致類型轉換失敗。而之前沒有配置失敗URL,所以一直重定向。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM