Springboot + shiro 整合之Url攔截設置(轉)


 shiro 整合到springboot 還是比較簡單的,只需要新建一個spring-shiro.xml的配置文件:


  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"  
  4.        default-lazy-init="true">  
  5.   
  6.     <description>Shiro Configuration</description>  
  7.   
  8.          <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
  9.             <property name="realm" ref="ShiroRealm" />  
  10.         </bean>  
  11.           
  12.         <!-- 項目自定義的Realm -->  
  13.         <bean id="ShiroRealm" class="org.spring.springboot.interceptor.shiro.ShiroRealm" ></bean>  
  14.           
  15.         <!-- Shiro Filter -->  
  16.         <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
  17.             <property name="securityManager" ref="securityManager" />       
  18.             <property name="loginUrl" value="/login" />         
  19.             <property name="successUrl" value="/backstage/index" />     
  20.             <property name="unauthorizedUrl" value="/login" />  
  21.                 <!-- anon:匿名攔截器,即不需要登錄即可訪問;一般用於靜態資源過濾  
  22.                      authc:如果沒有登錄會跳到相應的登錄頁面登錄  
  23.                      user:用戶攔截器,用戶已經身份驗證/記住我登錄的都可 -->  
  24.             <property name="filterChainDefinitions">  
  25.             <value>  
  26.         </span>     /plugins/** = anon  
  27.                 /images/** = anon  
  28.                 /css/**                     = anon  
  29.                         /**                 = authc<span style="font-size:14px;">  
  30.                 </value>  
  31.             </property>  
  32.         </bean>  
  33.        <!-- 緩存管理器 使用Ehcache實現-->    
  34.     <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">    
  35.         <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>    
  36.     </bean>    
  37.          
  38.       <!-- AOP式方法級權限檢查 -->  
  39.     <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">  
  40.         <property name="proxyTargetClass" value="true" />  
  41.     </bean>  
  42.       
  43.     <!-- 保證實現了Shiro內部lifecycle函數的bean執行 -->  
  44.     <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />  
  45. </span>  

</beans>



自定義的身份驗證就不多說了,主要還是關注本文重點,解釋一下filterChainDefinitions

1、springboot 默認訪問路徑 resources 下的 static(好像最高級別),它就這么定的愛咋咋地。。,所以靜態資源文件(js,css,jpg等)的訪問配置要去掉 /static  (/static/js/**=anon) 

2、/** 必須要放在最下面 ,注意是必須


我個人覺得springboot 的WebMvcConfigurerAdapter bean注解方式不如以前xml方便,只需要導入一下這個xml 文件,並將這個文件放到classpath 下就OK了。

@Configuration   //標注此文件為一個配置項,spring boot才會掃描到該配置。
@ImportResource(locations={"classpath:spring-shiro.xml"})

public class BootConfiguration extends WebMvcConfigurerAdapter {

}

上面這個類可以隨便放src下的任意目錄,springboot 它會找到的。

 


2018-01-22更新

本次的碰上的問題就是用上面第二條解決的,在配置Shiro的時候,給靜態資源加了"/static"沒有造成無法訪問!但是在引用靜態資源的時候,要加上"/static"否則訪問不到,比如:
```xml ```


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM