spring boot集成shiro详细教程(小结)

 更新时间:2018年01月05日 13:55:36   作者:bweird  
这篇文章主要介绍了spring boot 集成shiro详细教程,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

引入依赖:

  <dependency>
   <artifactId>ehcache-core</artifactId>
   <groupId>net.sf.ehcache</groupId>
   <version>2.5.0</version>
  </dependency>
  <dependency>
   <groupId>org.apache.shiro</groupId>
   <artifactId>shiro-ehcache</artifactId>
   <version>1.2.2</version>
  </dependency>

  <dependency>
     <groupId>org.slf4j</groupId>
     <artifactId>slf4j-api</artifactId>
     <version>1.7.25</version>
  </dependency>  
  <dependency>
   <groupId>org.apache.shiro</groupId>
   <artifactId>shiro-core</artifactId>
   <version>1.2.3</version>
  </dependency>
  <dependency>
   <groupId>org.apache.shiro</groupId>
   <artifactId>shiro-web</artifactId>
   <version>1.2.3</version>
  </dependency>
  <dependency>
   <groupId>org.apache.shiro</groupId>
   <artifactId>shiro-spring</artifactId>
   <version>1.2.3</version>
  </dependency>

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

<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>1.7.25</version>
</dependency>

传统xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx.xsd">
 <context:component-scan base-package="com.len"/>
 <!--定义realm-->
 <bean id="myRealm" class="com.len.core.shiro.LoginRealm">
  <property name="credentialsMatcher" ref="credentialsMatcher"/>
 </bean>
 <bean id="permissionFilter" class="com.len.core.filter.PermissionFilter"/>
 <!--目前去掉自定义拦截验证 由个人处理登录业务-->
 <!--<bean id="customAdvicFilter" class="com.len.core.filter.CustomAdvicFilter">
  <property name="failureKeyAttribute" value="shiroLoginFailure"/>
 </bean>-->
 <bean id="verfityCodeFilter" class="com.len.core.filter.VerfityCodeFilter">
  <property name="failureKeyAttribute" value="shiroLoginFailure"/>
  <property name="jcaptchaParam" value="code"/>
  <property name="verfitiCode" value="true"/>
 </bean>
 <!-- Shiro过滤器 -->
 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  <property name="securityManager" ref="securityManager"/>
 <property name="loginUrl" value="/login"/>
  <property name="unauthorizedUrl" value="/goLogin" />
  <property name="filters">
   <map>
    <entry key="per" value-ref="permissionFilter"/>
    <entry key="verCode" value-ref="verfityCodeFilter"/>
    <!--<entry key="main" value-ref="customAdvicFilter"/>-->
   </map>
  </property>
  <property name="filterChainDefinitions">
   <value>
    <!-- /** = anon所有url都可以匿名访问 -->
    /login = verCode,anon
    /getCode = anon
    /logout = logout
    /plugin/** = anon
    <!-- /** = authc 所有url都必须认证通过才可以访问-->
    /user/**=per
    <!-- /login = main-->
    /** = authc
   </value>
  </property>
 </bean>

 <!--安全管理器-->
 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  <property name="realm" ref="myRealm"/>
  <property name="cacheManager" ref="cacheManager" />
  <!--<property name="rememberMeManager" ref="rememberMeManager" />-->
 </bean>

 <!-- 凭证匹配器 -->
 <bean id="credentialsMatcher"
  class="com.len.core.shiro.RetryLimitCredentialsMatcher">
  <constructor-arg index="0" ref="cacheManager"/>
  <constructor-arg index="1" value="5"/>
  <property name="hashAlgorithmName" value="md5"/>
  <property name="hashIterations" value="4"/>
 </bean>
 <!--缓存-->
 <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
  <property name="cacheManagerConfigFile" value="classpath:ehcache/ehcache.xml"/>
 </bean>


 <!--开启注解-->
 <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
  <property name="securityManager" ref="securityManager" />
 </bean>

 <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
</beans>

转换成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<String, Filter> filters=new HashMap<>();
  filters.put("per",getPermissionFilter());
  filters.put("verCode",getVerfityCodeFilter());
  sfb.setFilters(filters);
  Map<String, String> 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的集成,可以用来学习

地址:https://gitee.com/bweird/lenosp

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 通过MyBatis读取数据库数据并提供rest接口访问

    通过MyBatis读取数据库数据并提供rest接口访问

    这篇文章主要介绍了通过MyBatis读取数据库数据并提供rest接口访问 的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-08-08
  • java格式化数值成货币格式示例

    java格式化数值成货币格式示例

    这篇文章主要介绍了java格式化数值成货币格式示例,格式化一个数值,比如123456789.123,希望显示成"$123,456,789.123",需要的朋友可以参考下
    2014-04-04
  • java类加载机制、类加载器、自定义类加载器的案例

    java类加载机制、类加载器、自定义类加载器的案例

    这篇文章主要介绍了java类加载机制、类加载器、自定义类加载器的案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • springBoot使用openfeign来远程调用的实现

    springBoot使用openfeign来远程调用的实现

    这篇文章主要介绍了springBoot使用openfeign来远程调用的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • SpringBoot Application核心注解详解

    SpringBoot Application核心注解详解

    进入到@SpringBootApplication的源码,可以看到里面组合了三个我们感兴趣的注解@ComponentScan、@EnableAutoConfiguration、@SpringBootConfiguration,我们一一分析这三个注解
    2022-10-10
  • AntDesign封装全局异常处理全局拦截器

    AntDesign封装全局异常处理全局拦截器

    这篇文章主要为大家介绍了AntDesign封装全局异常处理全局拦截器,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • 集合框架及背后的数据结构

    集合框架及背后的数据结构

    本文主要介绍了Java的集合框架Java Collection Framework,接口interface以及背后的数据结构,感兴趣的同学可以阅读参考
    2023-03-03
  • Java DOM4J方式生成XML的方法

    Java DOM4J方式生成XML的方法

    今天小编就为大家分享一篇Java DOM4J方式生成XML的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • 利用Java编写个"不贪吃蛇"小游戏

    利用Java编写个"不贪吃蛇"小游戏

    贪吃蛇大家一定有玩过了吧,今天小编给大家带来点不一样的。本文将用Java编写一个"不贪吃蛇"小游戏,感兴趣的小伙伴可以动手尝试一下
    2022-08-08
  • springboot实现发送邮件(QQ邮箱为例)

    springboot实现发送邮件(QQ邮箱为例)

    这篇文章主要为大家详细介绍了springboot实现发送邮件,qq邮箱代码实现邮件发送,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06

最新评论