spring boot 集成shiro的配置方法
springboot提供了一个自带的认证框架,同时也提供自定义的javaconfig配置扩展,spring-sercurity同样也是优秀的框架,但是习惯了用apacheshiro框架,而且原项目就是集成的shiro框架,到网上找了一下配置方式,没找到完全配置的方法,因此决定自己动手,丰衣足食!
要在springboot上集成其他框架,首先要会springjavaconfig方法,利用此方法同样可以配置其他模块,废话少说,开始。。。
开始前需要导入maven依赖(shiro-web可选):
org.apache.shiro shiro-core ${shiro.version} org.apache.shiro shiro-web ${shiro.version} org.apache.shiro shiro-spring ${shiro.version} org.apache.shiro shiro-ehcache ${shiro.version}
原shiro集成spring的配置拿出来,如下:
Shiro安全配置来源于:http://shiro.apache.org/spring.html /sa/**=authc /**=anon
好多类啊,没办法一个一个配置,javaconfig文件如下:
importjava.util.LinkedHashMap; importjava.util.Map; importorg.apache.shiro.cache.ehcache.EhCacheManager; importorg.apache.shiro.spring.LifecycleBeanPostProcessor; importorg.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; importorg.apache.shiro.spring.web.ShiroFilterFactoryBean; importorg.apache.shiro.web.mgt.DefaultWebSecurityManager; importorg.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; @Configuration publicclassShiroConfiguration{ privatestaticMapfilterChainDefinitionMap=newLinkedHashMap (); @Bean(name="ShiroRealmImpl") publicShiroRealmImplgetShiroRealm(){ returnnewShiroRealmImpl(); } @Bean(name="shiroEhcacheManager") publicEhCacheManagergetEhCacheManager(){ EhCacheManagerem=newEhCacheManager(); em.setCacheManagerConfigFile("classpath:ehcache-shiro.xml"); returnem; } @Bean(name="lifecycleBeanPostProcessor") publicLifecycleBeanPostProcessorgetLifecycleBeanPostProcessor(){ returnnewLifecycleBeanPostProcessor(); } @Bean publicDefaultAdvisorAutoProxyCreatorgetDefaultAdvisorAutoProxyCreator(){ DefaultAdvisorAutoProxyCreatordaap=newDefaultAdvisorAutoProxyCreator(); daap.setProxyTargetClass(true); returndaap; } @Bean(name="securityManager") publicDefaultWebSecurityManagergetDefaultWebSecurityManager(){ DefaultWebSecurityManagerdwsm=newDefaultWebSecurityManager(); dwsm.setRealm(getShiroRealm()); dwsm.setCacheManager(getEhCacheManager()); returndwsm; } @Bean publicAuthorizationAttributeSourceAdvisorgetAuthorizationAttributeSourceAdvisor(){ AuthorizationAttributeSourceAdvisoraasa=newAuthorizationAttributeSourceAdvisor(); aasa.setSecurityManager(getDefaultWebSecurityManager()); returnnewAuthorizationAttributeSourceAdvisor(); } @Bean(name="shiroFilter") publicShiroFilterFactoryBeangetShiroFilterFactoryBean(){ ShiroFilterFactoryBeanshiroFilterFactoryBean=newShiroFilterFactoryBean(); shiroFilterFactoryBean .setSecurityManager(getDefaultWebSecurityManager()); shiroFilterFactoryBean.setLoginUrl("/login"); shiroFilterFactoryBean.setSuccessUrl("/sa/index"); filterChainDefinitionMap.put("/sa/**","authc"); filterChainDefinitionMap.put("/**","anon"); shiroFilterFactoryBean .setFilterChainDefinitionMap(filterChainDefinitionMap); returnshiroFilterFactoryBean; } }
注意点:最后一个是filterChainDefinitionMap的初始化,Map用的是LinkedHashMap来初始化的,各位应用的时候将其配置成properties文件,然后初始化就ok了,改写好后直接启动Ok,搬运到springboot应该是OK的。
别忘了在ehcache-shiro.xml
备注:ShiroRealmImpl类请参考官方文档
总结
以上所述是小编给大家介绍的springboot集成shiro的配置方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。