欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring boot集成shiro詳細(xì)教程(小結(jié))

 更新時(shí)間:2018年01月05日 13:55:36   作者:bweird  
這篇文章主要介紹了spring boot 集成shiro詳細(xì)教程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

我們開發(fā)時(shí)候有時(shí)候要把傳統(tǒng)spring shiro轉(zhuǎn)成spring boot項(xiàng)目,或者直接集成,name我們要搞清楚一個(gè)知識(shí),就是 xml配置和spring bean代碼配置的關(guān)系,這一點(diǎn)很重要,因?yàn)閟pring boot是沒有xml配置文件的(也不絕對(duì),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>

如果你出現(xiàn)錯(cuò)誤 找不到slf4j,引入依賴

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

傳統(tǒng)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"/>
 <!--目前去掉自定義攔截驗(yàn)證 由個(gè)人處理登錄業(yè)務(wù)-->
 <!--<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都必須認(rèn)證通過才可以訪問-->
    /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>

轉(zhuǎn)換成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;
 }

其中有個(gè)別類是自定義的攔截器和 realm,此時(shí)spring 即能注入shiro 也就是 spring boot集成上了 shiro

如果你因?yàn)槠渌渲靡鹨恍┦?,可以參考開源項(xiàng)目 lenos快速開發(fā)腳手架 spring boot版本,其中有對(duì)shiro的集成,可以用來學(xué)習(xí)

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot schedule 解決定時(shí)任務(wù)不執(zhí)行的問題

    springboot schedule 解決定時(shí)任務(wù)不執(zhí)行的問題

    這篇文章主要介紹了springboot schedule 解決定時(shí)任務(wù)不執(zhí)行的問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • JAVA基礎(chǔ)-GUI

    JAVA基礎(chǔ)-GUI

    這篇文章主要介紹了JAVA中關(guān)于GUI的相關(guān)知識(shí),文中代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • 詳解SpringMVC中的@RequestMapping注解

    詳解SpringMVC中的@RequestMapping注解

    這篇文章主要介紹了SpringMVC中@RequestMapping注解,@RequestMapping注解是一個(gè)用來處理請(qǐng)求地址映射的注解,可用于映射一個(gè)請(qǐng)求或一個(gè)方法,可以用在類或方法上,需要的朋友可以參考下
    2023-07-07
  • Java并發(fā)Map面試線程安全數(shù)據(jù)結(jié)構(gòu)全面分析

    Java并發(fā)Map面試線程安全數(shù)據(jù)結(jié)構(gòu)全面分析

    本文將探討如何在Java中有效地應(yīng)對(duì)這些挑戰(zhàn),介紹一種強(qiáng)大的工具并發(fā)Map,它能夠幫助您管理多線程環(huán)境下的共享數(shù)據(jù),確保數(shù)據(jù)的一致性和高性能,深入了解Java中的并發(fā)Map實(shí)現(xiàn),包括ConcurrentHashMap和ConcurrentSkipListMap,及相關(guān)知識(shí)點(diǎn)
    2023-09-09
  • struts2標(biāo)簽總結(jié)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    struts2標(biāo)簽總結(jié)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)總結(jié)了struts2標(biāo)簽的使用方法,和學(xué)習(xí)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • servlet之cookie簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    servlet之cookie簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Cookie技術(shù)誕生以來,它就成了廣大網(wǎng)絡(luò)用戶和Web開發(fā)人員爭(zhēng)論的一個(gè)焦點(diǎn)。下面這篇文章主要給大家介紹了關(guān)于servlet之cookie簡(jiǎn)介的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-07-07
  • java實(shí)現(xiàn)二維數(shù)組轉(zhuǎn)json的方法示例

    java實(shí)現(xiàn)二維數(shù)組轉(zhuǎn)json的方法示例

    這篇文章主要介紹了java實(shí)現(xiàn)二維數(shù)組轉(zhuǎn)json的方法,涉及java數(shù)組遍歷及json格式數(shù)據(jù)構(gòu)造相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10
  • Java并發(fā)編程之ReentrantLock解析

    Java并發(fā)編程之ReentrantLock解析

    這篇文章主要介紹了Java并發(fā)編程之ReentrantLock解析,ReentrantLock內(nèi)容定義了一個(gè)抽象類Sync,繼承自AQS,而不是自己去繼承AQS,所有對(duì)ReentrantLock的操作都會(huì)轉(zhuǎn)化為對(duì)Sync的操作,需要的朋友可以參考下
    2023-12-12
  • Java 回調(diào)機(jī)制(CallBack) 詳解及實(shí)例代碼

    Java 回調(diào)機(jī)制(CallBack) 詳解及實(shí)例代碼

    這篇文章主要介紹了 Java 回調(diào)機(jī)制(CallBack) 詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • java 計(jì)算中位數(shù)的實(shí)現(xiàn)方法

    java 計(jì)算中位數(shù)的實(shí)現(xiàn)方法

    這篇文章主要介紹了java 計(jì)算中位數(shù)的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08

最新評(píng)論