Shiro多Realm认证

ゝ一纸荒年。 2022-05-21 10:15 488阅读 0赞

查看源代码,发现:

  1. assertRealmsConfigured();
  2. Collection<Realm> realms = getRealms();
  3. if (realms.size() == 1) {
  4. return doSingleRealmAuthentication(realms.iterator().next(), authenticationToken);
  5. } else {
  6. return doMultiRealmAuthentication(realms, authenticationToken);
  7. }

shiro支持使用多Realm,来执行认证。
shiro通过将多个Realm封装到一个集合中一起使用。
当集合中的Realm有多个时,就采用多Realm的使用方式。

此时需要准备多个Realm用来测试,多个Realm继承Realm接口的实现类或直接实现Realm接口。

此时在spring的配置文件如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  3. <!-- 配置shiro的securityManager -->
  4. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  5. <property name="cacheManager" ref="cacheManager" />
  6. <!-- shiro中session相关配置与单个Realm属性配置 <property name="sessionMode" value="native" /> <property name="realm" ref="jdbcRealm" /> -->
  7. <!-- 使用多Realm时使用该配置配置多realm认证匹配器 -->
  8. <property name="authenticator" ref="authenticator"/>
  9. </bean>
  10. <!-- 添加多Realm认证匹配器 -->
  11. <bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
  12. <property name="realms">
  13. <list>
  14. <ref bean="jdbcRealm"/>
  15. <ref bean="secondRealm"/>
  16. </list>
  17. </property>
  18. </bean>
  19. <!-- shiro缓存管理器,采用第三方缓存 -->
  20. <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
  21. <!-- 采用bean方式,也可以采用指定类路径下的xml文件 <property name="cacheManager" ref="ehCacheManager"/> -->
  22. <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
  23. </bean>
  24. <!-- 第一个Realm -->
  25. <bean id="jdbcRealm" class="com.heiketu.shiro.realm.ShiroRealm">
  26. <!-- 配置自定义的凭证匹配器 -->
  27. <property name="credentialsMatcher">
  28. <!-- 内部构造:new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations)对象 通过创建SimpleHash对象来进行加密 -->
  29. <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
  30. <!-- 采用MD5算法加密 -->
  31. <property name="hashAlgorithmName" value="MD5"></property>
  32. </bean>
  33. </property>
  34. </bean>
  35. <!-- 第二个Realm -->
  36. <bean id="secondRealm" class="com.heiketu.shiro.realm.SecondRealm">
  37. <!-- 配置自定义的凭证匹配器 -->
  38. <property name="credentialsMatcher">
  39. <!-- 内部构造:new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations)对象 通过创建SimpleHash对象来进行加密 -->
  40. <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
  41. <!-- 采用MD5算法加密 -->
  42. <property name="hashAlgorithmName" value="SHA1"></property>
  43. </bean>
  44. </property>
  45. </bean>
  46. <!-- shiro生命周期Post处理器 -->
  47. <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
  48. <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor" />
  49. <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
  50. <property name="securityManager" ref="securityManager" />
  51. </bean>
  52. <bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
  53. <property name="securityManager" ref="securityManager" />
  54. </bean>
  55. <!-- shiro权限过滤:id要和web.xml中的ShiroFilter的filter-name一致[默认情况下, 如果不一致,spring会在filer的init-param中的targetBeanName配置参数中获取到对应的bean名称, 如果没有在容器中配置该bean,则会抛出NoSuchBeanDefinitionException异常] -->
  56. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  57. <property name="securityManager" ref="securityManager" />
  58. <!-- 登录页面 -->
  59. <property name="loginUrl" value="/login.jsp" />
  60. <!-- 登录成功页面 -->
  61. <property name="successUrl" value="/success.jsp" />
  62. <!-- 无权限页面 -->
  63. <property name="unauthorizedUrl" value="/unauthorized.jsp" />
  64. <!-- Shiro会通过Web.xml中的ShiroFilter过滤器调用该 id为shiroFilter的Bean,然后通过该Bean的property name为filterChainDefinitions中设置的value来 判断页面是否需要验证授权才能访问。 默认情况下,没有配置到value中的路径shiro不会拦截,而路径被标识为anon的则为 可以通过匿名访问[也就是可以直接访问],标识为authc则为需要授权才能访问的页面。 -->
  65. <property name="filterChainDefinitions">
  66. <value>
  67. <!-- anon:表示可以匿名访问 -->
  68. /login.jsp = anon
  69. /shiroRequest/login = anon
  70. /shiro/logout = logout
  71. <!-- authc:表示需要授权才可以访问 -->
  72. /** = authc
  73. </value>
  74. </property>
  75. </bean>
  76. </beans>

使用ModularRealmAuthenticator来配置多个Realm,然后通过DefaultWebSecurityManagerauthenticator属性将指向ModularRealmAuthenticator

发表评论

表情:
评论列表 (有 0 条评论,488人围观)

还没有评论,来说两句吧...

相关阅读