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 ```
原文地址:https://www.cnblogs.com/jpfss/p/8328165.html