spring boot 集成shiro的配置

本文介绍如何在Spring Boot中集成Apache Shiro框架实现权限管理。包括Maven依赖配置、使用JavaConfig方式代替XML配置,以及如何自定义过滤链等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  spring boot提供了一个自带的认证框架,同时也提供自定义的javaconfig配置扩展,spring-sercurity同样也是优秀的框架,但是习惯了用apache shiro框架,而且原项目就是集成的shiro框架,到网上找了一下配置方式,没找到完全配置的方法,因此决定自己动手,丰衣足食!

        要在spring boot上集成其他框架,首先要会spring javaconfig方法,利用此方法同样可以配置其他模块,废话少说,开始。。。

        开始前需要导入maven依赖(shiro-web可选):

[html]  view plain  copy
  1.               <dependency>  
  2.     <groupId>org.apache.shiro</groupId>  
  3.     <artifactId>shiro-core</artifactId>  
  4.     <version>${shiro.version}</version>  
  5. </dependency>  
  6. <dependency>  
  7.     <groupId>org.apache.shiro</groupId>  
  8.     <artifactId>shiro-web</artifactId>  
  9.     <version>${shiro.version}</version>  
  10. </dependency>  
  11. <dependency>  
  12.     <groupId>org.apache.shiro</groupId>  
  13.     <artifactId>shiro-spring</artifactId>  
  14.     <version>${shiro.version}</version>  
  15. </dependency>  
  16. <dependency>  
  17.     <groupId>org.apache.shiro</groupId>  
  18.     <artifactId>shiro-ehcache</artifactId>  
  19.     <version>${shiro.version}</version>  
  20. </dependency>  


        原shiro集成spring的配置拿出来,如下:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"  
  5. default-lazy-init="true">  
  6. <description>Shiro安全配置 来源于: http://shiro.apache.org/spring.html  
  7. </description>  
  8. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
  9. <!-- Single realm app. If you have multiple realms, use the 'realms' property  
  10. instead. -->  
  11. <property name="realm" ref="ShiroRealmImpl" />  
  12. <property name="cacheManager" ref="shiroEhcacheManager" />  
  13. </bean>  
  14. <!-- Define the realm you want to use to connect to your back-end security  
  15. datasource: -->  
  16. <bean id="ShiroRealmImpl" class="com.wechatserver.web.services.system.impl.ShiroRealmImpl" />  
  17. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
  18. <property name="securityManager" ref="securityManager" />  
  19. <property name="loginUrl" value="/login" /> <!-- 没有权限或者失败后跳转的页面 -->  
  20. <property name="successUrl" value="/sa/index" />  
  21. <property name="filterChainDefinitions">  
  22. <!-- , roles[admin], perms[document:read] -->  
  23. <value>  
  24. <!--  
  25. /user/** = authc  
  26. /role/edit/* = perms[role:edit]  
  27. /role/save = perms [role:edit]  
  28. /role/list = perms [role:view]  
  29. -->  
  30. /sa/** = authc  
  31. /** = anon  
  32. </value>  
  33. </property>  
  34. </bean>  
  35. <!-- 用户授权/认证信息Cache, 采用EhCache 缓存 -->  
  36. <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">  
  37. <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml" />  
  38. </bean>  
  39. <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->  
  40. <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />  
  41. <!-- AOP式方法级权限检查 -->  
  42. <!-- Enable Shiro Annotations for Spring-configured beans. Only run after -->  
  43. <!-- the lifecycleBeanProcessor has run: -->  
  44. <bean  
  45. class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"  
  46. depends-on="lifecycleBeanPostProcessor">  
  47. <property name="proxyTargetClass" value="true" />  
  48. </bean>  
  49. <bean  
  50. class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">  
  51. <property name="securityManager" ref="securityManager" />  
  52. </bean>  
  53. </beans>   

好多类啊,没办法一个一个配置,javaconfig文件如下:

[java]  view plain  copy
  1. import java.util.LinkedHashMap;  
  2. import java.util.Map;  
  3.   
  4. import org.apache.shiro.cache.ehcache.EhCacheManager;  
  5. import org.apache.shiro.spring.LifecycleBeanPostProcessor;  
  6. import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;  
  7. import org.apache.shiro.spring.web.ShiroFilterFactoryBean;  
  8. import org.apache.shiro.web.mgt.DefaultWebSecurityManager;  
  9. import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;  
  10. import org.springframework.context.annotation.Bean;  
  11. import org.springframework.context.annotation.Configuration;  
  12.   
  13.   
  14. @Configuration  
  15. public class ShiroConfiguration {  
  16.   
  17.     private static Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();  
  18.   
  19.     @Bean(name = "ShiroRealmImpl")  
  20.     public ShiroRealmImpl getShiroRealm() {  
  21.         return new ShiroRealmImpl();  
  22.     }  
  23.   
  24.     @Bean(name = "shiroEhcacheManager")  
  25.     public EhCacheManager getEhCacheManager() {  
  26.         EhCacheManager em = new EhCacheManager();  
  27.         em.setCacheManagerConfigFile("classpath:ehcache-shiro.xml");  
  28.         return em;  
  29.     }  
  30.   
  31.     @Bean(name = "lifecycleBeanPostProcessor")  
  32.     public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {  
  33.         return new LifecycleBeanPostProcessor();  
  34.     }  
  35.   
  36.     @Bean  
  37.     public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {  
  38.         DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();  
  39.         daap.setProxyTargetClass(true);  
  40.         return daap;  
  41.     }  
  42.   
  43.     @Bean(name = "securityManager")  
  44.     public DefaultWebSecurityManager getDefaultWebSecurityManager() {  
  45.         DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();  
  46.         dwsm.setRealm(getShiroRealm());  
  47.         dwsm.setCacheManager(getEhCacheManager());  
  48.         return dwsm;  
  49.     }  
  50.   
  51.     @Bean  
  52.     public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor() {  
  53.         AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();  
  54.         aasa.setSecurityManager(getDefaultWebSecurityManager());  
  55.         return new AuthorizationAttributeSourceAdvisor();  
  56.     }  
  57.   
  58.     @Bean(name = "shiroFilter")  
  59.     public ShiroFilterFactoryBean getShiroFilterFactoryBean() {  
  60.         ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();  
  61.         shiroFilterFactoryBean  
  62.                 .setSecurityManager(getDefaultWebSecurityManager());  
  63.         shiroFilterFactoryBean.setLoginUrl("/login");  
  64.         shiroFilterFactoryBean.setSuccessUrl("/sa/index");  
  65.         filterChainDefinitionMap.put("/sa/**""authc");  
  66.         filterChainDefinitionMap.put("/**""anon");  
  67.         shiroFilterFactoryBean  
  68.                 .setFilterChainDefinitionMap(filterChainDefinitionMap);  
  69.         return shiroFilterFactoryBean;  
  70.     }  
  71.   
  72. }  

注意点:最后一个是filterChainDefinitionMap的初始化,Map用的是LinkedHashMap来初始化的,各位应用的时候将其配置成properties文件,然后初始化就ok了,改写好后直接启动Ok,搬运到spring boot应该是OK的。


别忘了在ehcache-shiro.xml

[html]  view plain  copy
  1. <ehcache updateCheck="false" name="shiroCache">  
  2.   
  3.     <defaultCache  
  4.             maxElementsInMemory="10000"  
  5.             eternal="false"  
  6.             timeToIdleSeconds="120"  
  7.             timeToLiveSeconds="120"  
  8.             overflowToDisk="false"  
  9.             diskPersistent="false"  
  10.             diskExpiryThreadIntervalSeconds="120"  
  11.             />  
  12. </ehcache>  

[java]  view plain  copy
  1. 备注:ShiroRealmImpl类请参考官方文档  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值