spring boot 集成shiro的配置

  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.shirogroupId>  
  3.     <artifactId>shiro-coreartifactId>  
  4.     <version>${shiro.version}version>  
  5. dependency>  
  6. <dependency>  
  7.     <groupId>org.apache.shirogroupId>  
  8.     <artifactId>shiro-webartifactId>  
  9.     <version>${shiro.version}version>  
  10. dependency>  
  11. <dependency>  
  12.     <groupId>org.apache.shirogroupId>  
  13.     <artifactId>shiro-springartifactId>  
  14.     <version>${shiro.version}version>  
  15. dependency>  
  16. <dependency>  
  17.     <groupId>org.apache.shirogroupId>  
  18.     <artifactId>shiro-ehcacheartifactId>  
  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.   
  10. <property name="successUrl" value="/sa/index" />  
  11. <property name="filterChainDefinitions">  
  12.   
  13. <value>  
  14.   
  15. <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">  
  16. <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml" />  
  17. bean>  
  18.   
  19. <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />  
  20.   
  21.   
  22.   
  23. <bean  
  24. class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"  
  25. depends-on="lifecycleBeanPostProcessor">  
  26. <property name="proxyTargetClass" value="true" />  
  27. bean>  
  28. <bean  
  29. class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">  
  30. <property name="securityManager" ref="securityManager" />  
  31. bean>  
  32. 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 filterChainDefinitionMap = new LinkedHashMap();  
  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类请参考官方文档  

你可能感兴趣的:(spring)