Springboot + shiro 整合之Url拦截设置

论坛 期权论坛 脚本     
匿名技术用户   2020-12-21 19:53   11   0

shiro 整合到springboot 还是比较简单的,只需要新建一个spring-shiro.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"
       default-lazy-init="true">

    <description>Shiro Configuration</description>

      <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
   <property name="realm" ref="ShiroRealm" />
  </bean>
  
  <!-- 項目自定义的Realm -->
     <bean id="ShiroRealm" class="org.spring.springboot.interceptor.shiro.ShiroRealm" ></bean>
  
  <!-- Shiro Filter -->
  <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
   <property name="securityManager" ref="securityManager" />  
   <property name="loginUrl" value="/login" />  
   <property name="successUrl" value="/backstage/index" /> 
   <property name="unauthorizedUrl" value="/login" />
    <!-- anon:匿名拦截器,即不需要登录即可访问;一般用于静态资源过滤
               authc:如果没有登录会跳到相应的登录页面登录
               user:用户拦截器,用户已经身份验证/记住我登录的都可 -->
   <property name="filterChainDefinitions">
   <value>
    /plugins/** = anon
     /images/** = anon
    /css/**         = anon
                     /**     = authc
    </value>
   </property>
  </bean>
    <!-- 缓存管理器 使用Ehcache实现-->  
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">  
        <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>  
    </bean>  
       
      <!-- AOP式方法级权限检查 -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
        <property name="proxyTargetClass" value="true" />
    </bean>
    
    <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

</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 它会找到的。








分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP