分享

CAS和Shiro在spring中集成

 KILLKISS 2015-07-08

shiro是权限管理框架,现在已经会利用它如何控制权限。为了能够为多个系统提供统一认证入口,又研究了单点登录框架cas。因为二者都会涉及到对session的管理,所以需要进行集成。

 

Shiro在1.2.0的时候提供了cas的集成因此在项目中添加shiro-cas的依赖
    <dependency>
       <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-cas</artifactId>
       <version>${shiro.version}</version>
    </dependency>

 

Shirocas集成后,cas client的配置更加简单了。原理就是将casFilter添加到到shiroFilter的filterChain中。 shiroFilter是在web.xml中定义的,前文已经讲过。


在Spring项目中集成Shiro和CAS

  1. <?xmlversionxmlversion="1.0" encoding="UTF-8"?>  
  2. <beansxmlnsbeansxmlns="http://www./schema/beans"  
  3. xmlns:xsi="http://www./2001/XMLSchema-instance"  
  4. xsi:schemaLocation="http://www./schema/beanshttp://www./schema/beans/spring-beans-2.5.xsd"  
  5. default-lazy-init="true">  
  6.    
  7. <beanidbeanid="shiroFilter"class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
  8. <propertynamepropertyname="securityManager" ref="securityManager" />  
  9.    
  10. <!--没有单点登录下的配置:没有权限或者失败后跳转的页面 -->  
  11. <!--<property name="loginUrl" value="/login/toLoginAction"/> -->  
  12.    
  13. <!--有单点登录的配置:登录 CAS 服务端地址,参数 service 为服务端的返回地址 -->   
  14. <propertynamepropertyname="loginUrl"  
  15. value="http://localhost:18080/cas/login?service=http://localhost:8080/gxpt_web_qx_login/shiro-cas"/>  
  16. <!--<property name="successUrl" value="/page/index.jsp"/> -->  
  17. <propertynamepropertyname="successUrl" value="/indexAction" />  
  18.    
  19. <propertynamepropertyname="filters">  
  20. <map>  
  21. <!--添加casFilter到shiroFilter -->  
  22. <entrykeyentrykey="casFilter" value-ref="casFilter">  
  23. </entry>  
  24. </map>  
  25. </property>  
  26.    
  27.                  <propertynamepropertyname="filterChainDefinitions">  
  28. <value>  
  29. /shiro-cas= casFilter  
  30. /styles/**= anon  
  31. /**= user  
  32. </value>  
  33. </property>  
  34.    
  35. <!--没有单点登录下的配置: -->  
  36. <!--<property name="filterChainDefinitions">  
  37. <value>  
  38. /styles/**= anon  
  39. /login/loginAction= anon  
  40. /login/logoutAction= logout  
  41. /**= user  
  42. </value>  
  43. </property>-->  
  44. </bean>  
  45.    
  46. <beanidbeanid="casFilter" class="org.apache.shiro.cas.CasFilter">  
  47. <!--配置验证错误时的失败页面(Ticket 校验不通过时展示的错误页面) -->  
  48. <propertynamepropertyname="failureUrl" value="/page/error.jsp" />  
  49. </bean>  
  50.    
  51. <beanidbeanid="securityManager"class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
  52. <!--Single realm app. If you have multiple realms, use the 'realms' property  
  53. instead.-->  
  54. <!--没有单点登录下的配置: -->          
  55. <!--<property name="realm" ref="shiroDbRealm" /> -->  
  56.    
  57. <propertynamepropertyname="realm" ref="casRealm" />  
  58. <propertynamepropertyname="subjectFactory" ref="casSubjectFactory" />  
  59.    
  60. <propertynamepropertyname="cacheManager" ref="shiroEhcacheManager" />  
  61. </bean>  
  62.    
  63. <beanidbeanid="casRealm" class="web.qx.login.shiro.MyCasRealm">  
  64. <propertynamepropertyname="defaultRoles" value="ROLE_USER"/>   
  65. <propertynamepropertyname="casServerUrlPrefix"value="http://localhost:18080/cas" />  
  66. <!--客户端的回调地址设置,必须和上面的shiro-cas过滤器拦截的地址一致 -->  
  67. <propertynamepropertyname="casService"  
  68. value="http://localhost:8080/gxpt_web_qx_login/shiro-cas"/>  
  69. </bean>  
  70.    
  71. <!--Define the realm you want to use to connect to your back-end security  
  72. datasource:-->  
  73. <!--  
  74. <beanidbeanid="shiroDbRealm"class="web.qx.login.shiro.ShiroDbRealm">  
  75. <propertynamepropertyname="loginService"ref="login-loginBean"></property>  
  76. </bean>  
  77.  -->  
  78.    
  79. <beanidbeanid="casSubjectFactory"class="org.apache.shiro.cas.CasSubjectFactory" />  
  80.    
  81. <!--用户授权/认证信息Cache, 采用EhCache 缓存 -->  
  82. <beanidbeanid="shiroEhcacheManager"class="org.apache.shiro.cache.ehcache.EhCacheManager">  
  83. <propertynamepropertyname="cacheManagerConfigFile"value="classpath:config/ehcache-shiro.xml" />  
  84. </bean>  
  85.    
  86.    
  87. <!--保证实现了Shiro内部lifecycle函数的bean执行 -->  
  88. <beanidbeanid="lifecycleBeanPostProcessor"class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />  
  89.    
  90.    
  91. <!--AOP式方法级权限检查 -->  
  92. <!--Enable Shiro Annotations for Spring-configured beans. Only run after -->  
  93. <!--the lifecycleBeanProcessor has run: -->  
  94. <bean  
  95. class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"  
  96. depends-on="lifecycleBeanPostProcessor">  
  97. <propertynamepropertyname="proxyTargetClass" value="true" />  
  98. </bean>  
  99. <bean  
  100. class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">  
  101. <propertynamepropertyname="securityManager" ref="securityManager" />  
  102. </bean>  
  103.    
  104. </beans>  

 

没有单点登录情况下的话,登录认证和授权认证默认在AuthorizingRealm的doGetAuthorizationInfo和doGetAuthenticationInfo中进行,所以我这里是通过shiroDbRealm(继承AuthorizingRealm的自定义类)覆写doGetAuthorizationInfo和doGetAuthenticationInfo,实现自定义登录认证和授权认证。

 

有单点登录情况下,登录认证是在casserver进行的,那么执行流程是这样的:用户从 cas server登录成功后,跳到cas client的CasRealm执行默认的doGetAuthorizationInfo和doGetAuthenticationInfo,此时doGetAuthenticationInfo做的工作是把登录用户信息传递给shiro,保持默认即可,而对于授权的处理,可以通过MyCasRealm(继承CasRealm的自定义类)覆写doGetAuthorizationInfo进行自定义授权认证。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多