springboot集成shiro的示例分析-成都快上网建站

springboot集成shiro的示例分析

这篇文章给大家分享的是有关springboot集成shiro的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

创新互联建站服务项目包括顺平网站建设、顺平网站制作、顺平网页制作以及顺平网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,顺平网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到顺平省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

我们开发时候有时候要把传统spring shiro转成spring boot项目,或者直接集成,name我们要搞清楚一个知识,就是 xml配置和spring bean代码配置的关系,这一点很重要,因为spring boot是没有xml配置文件的(也不绝对,spring boot也是可以引用xml配置的)

引入依赖:

  
   ehcache-core
   net.sf.ehcache
   2.5.0
  
  
   org.apache.shiro
   shiro-ehcache
   1.2.2
  

  
     org.slf4j
     slf4j-api
     1.7.25
    
  
   org.apache.shiro
   shiro-core
   1.2.3
  
  
   org.apache.shiro
   shiro-web
   1.2.3
  
  
   org.apache.shiro
   shiro-spring
   1.2.3
  

如果你出现错误 找不到slf4j,引入依赖


   org.slf4j
   slf4j-log4j12
   1.7.25

传统xml配置如下:



 
 
 
  
 
 
 
 
 
  
  
  
 
 
 
  
 
  
  
   
    
    
    
   
  
  
   
    
    /login = verCode,anon
    /getCode = anon
    /logout = logout
    /plugin/** = anon
    
    /user/**=per
    
    /** = authc
   
  
 

 
 
  
  
  
 

 
 
  
  
  
  
 
 
 
  
 


 
 
  
 

 

转换成bean 新建类ShiroConfig

@Configuration
public class ShiroConfig {
 @Bean
 public RetryLimitCredentialsMatcher getRetryLimitCredentialsMatcher(){
  RetryLimitCredentialsMatcher rm=new RetryLimitCredentialsMatcher(getCacheManager(),"5");
  rm.setHashAlgorithmName("md5");
  rm.setHashIterations(4);
  return rm;

 }
 @Bean
 public LoginRealm getLoginRealm(){
  LoginRealm realm= new LoginRealm();
  realm.setCredentialsMatcher(getRetryLimitCredentialsMatcher());
  return realm;
 }

 @Bean
 public EhCacheManager getCacheManager(){
  EhCacheManager ehCacheManager=new EhCacheManager();
  ehCacheManager.setCacheManagerConfigFile("classpath:ehcache/ehcache.xml");
  return ehCacheManager;
 }

 @Bean
 public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
  return new LifecycleBeanPostProcessor();
 }

 @Bean(name="securityManager")
 public DefaultWebSecurityManager getDefaultWebSecurityManager(){
  DefaultWebSecurityManager dwm=new DefaultWebSecurityManager();
  dwm.setRealm(getLoginRealm());
  dwm.setCacheManager(getCacheManager());
  return dwm;
 }

 @Bean
 public PermissionFilter getPermissionFilter(){
  PermissionFilter pf=new PermissionFilter();
  return pf;
 }

 @Bean
 public VerfityCodeFilter getVerfityCodeFilter(){
  VerfityCodeFilter vf= new VerfityCodeFilter();
  vf.setFailureKeyAttribute("shiroLoginFailure");
  vf.setJcaptchaParam("code");
  vf.setVerfitiCode(true);
  return vf;
 }

 @Bean(name = "shiroFilter")
 public ShiroFilterFactoryBean getShiroFilterFactoryBean(){
  ShiroFilterFactoryBean sfb = new ShiroFilterFactoryBean();
  sfb.setSecurityManager(getDefaultWebSecurityManager());
  sfb.setLoginUrl("/login");
  sfb.setUnauthorizedUrl("/goLogin");
  Map filters=new HashMap<>();
  filters.put("per",getPermissionFilter());
  filters.put("verCode",getVerfityCodeFilter());
  sfb.setFilters(filters);
  Map filterMap = new LinkedHashMap<>();
  filterMap.put("/login","verCode,anon");
  //filterMap.put("/login","anon");
  filterMap.put("/getCode","anon");
  filterMap.put("/logout","logout");
  filterMap.put("/plugin/**","anon");
  filterMap.put("/user/**","per");
  filterMap.put("/**","authc");
  sfb.setFilterChainDefinitionMap(filterMap);
  return sfb;
 }

 @Bean
 public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
  DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
  advisorAutoProxyCreator.setProxyTargetClass(true);
  return advisorAutoProxyCreator;
 }

 @Bean
 public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor(){
  AuthorizationAttributeSourceAdvisor as=new AuthorizationAttributeSourceAdvisor();
  as.setSecurityManager(getDefaultWebSecurityManager());
  return as;
 }

 @Bean
 public FilterRegistrationBean delegatingFilterProxy(){
  FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
  DelegatingFilterProxy proxy = new DelegatingFilterProxy();
  proxy.setTargetFilterLifecycle(true);
  proxy.setTargetBeanName("shiroFilter");
  filterRegistrationBean.setFilter(proxy);
  return filterRegistrationBean;
 }

其中有个别类是自定义的拦截器和 realm,此时spring 即能注入shiro 也就是 spring boot集成上了 shiro

如果你因为其他配置引起一些失败,可以参考开源项目 lenos快速开发脚手架 spring boot版本,其中有对shiro的集成。

感谢各位的阅读!关于“springboot集成shiro的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!


网站栏目:springboot集成shiro的示例分析
标题网址:http://kswjz.com/article/igheeg.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流