在SpringBoot+Shiro实现安全框架的时候,自定义扩展了一些Filter,并注册到ShiroFilter,但是运行的时候发现总是在ShiroFilter之前就进入了自定义Filter,结果当然是不对的。

<!--自定义登陆拦截器,支持Ajax-->
<bean id="smartfxLoginFilter" class="com.smartdata360.smartfx.shiro.filter.ShiroLoginFilter">
<property name="loginUrl" value="${shiro.login.url:/login}"/>
</bean>
<!--自定义角色codes拦截器,支持Ajax-->
<bean id="smartfxRolesFilter" class="com.smartdata360.smartfx.shiro.filter.ShiroRolesFilter">
</bean>
<!--自定义Perm拦截器,支持Ajax-->
<bean id="smartfxPermsFilter" class="com.smartdata360.smartfx.shiro.filter.ShiroPermsFilter">
</bean>
<!--自定义session踢出拦截器-->
<bean id="smartfxKickoutFilter" class="com.smartdata360.smartfx.shiro.filter.KickoutSessionFilter">
<property name="kickoutUrl" value="${shiro.kickout.url:/login}"/>
<property name="kickoutAfter" value="${shiro.keckout.after:true}"/>
<property name="userSessionCount" value="${shiro.kickout.maxSessionCount:1}"/>
<property name="sessionDao" ref="redisSessionDao"/>
</bean>

经过查看相关文档,发现其实是SpringBoot自动帮我们注册了我们的Filter,典型的好心办坏事。我们要的是希望Shiro来管理我们的自定义Filter,所以我们要想办法取消SpringBoot自动注册我们的Filter。

参考这里

As described above any Servlet or Filter beans will be registered with the servlet container automatically. To disable registration of a particular Filter or Servlet bean create a registration bean for it and mark it as disabled.

所以解决办法是另外多定义一份配置文件告诉SpringBoot不要自作多情:

package com.smartdata360.smartfx.shiro.config;

import com.smartdata360.smartfx.shiro.filter.KickoutSessionFilter;
import com.smartdata360.smartfx.shiro.filter.ShiroLoginFilter;
import com.smartdata360.smartfx.shiro.filter.ShiroPermsFilter;
import com.smartdata360.smartfx.shiro.filter.ShiroRolesFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author liushuishang@gmail.com
* @date 2017/12/13 15:27
**/
@Configuration
public class ShiroFilterRegisterConfig { @Bean
public FilterRegistrationBean shiroLoginFilteRegistration(ShiroLoginFilter filter) {
FilterRegistrationBean registration = new FilterRegistrationBean(filter);
registration.setEnabled(false);
return registration;
} @Bean
public FilterRegistrationBean shiroRolesFilterRegistration(ShiroRolesFilter filter) {
FilterRegistrationBean registration = new FilterRegistrationBean(filter);
registration.setEnabled(false);
return registration;
}
@Bean
public FilterRegistrationBean shiroPermsFilterRegistration(ShiroPermsFilter filter) {
FilterRegistrationBean registration = new FilterRegistrationBean(filter);
registration.setEnabled(false);
return registration;
} @Bean
public FilterRegistrationBean kickoutSessionFilterRegistration(KickoutSessionFilter filter) {
FilterRegistrationBean registration = new FilterRegistrationBean(filter);
registration.setEnabled(false);
return registration;
}
}

解决Shiro+SpringBoot自定义Filter不生效问题的更多相关文章

  1. SpringBoot自定义Filter

    SpringBoot自定义Filter SpringBoot自动添加了OrderedCharacterEncodingFilter和HiddenHttpMethodFilter,当然我们可以自定 义F ...

  2. 解决Spring Security自定义filter重复执行问题

    今天做项目的时候,发现每次拦截器日志都会打两遍,很纳闷,怀疑是Filter被执行了两遍.结果debug之后发现还真是!记录一下这个神奇的BUG! 问题描述 项目中使用的是Spring-security ...

  3. 细说shiro之自定义filter

    写在前面 我们知道,shiro框架在Java Web应用中使用时,本质上是通过filter方式集成的. 也就是说,它是遵循过滤器链规则的:filter的执行顺序与在web.xml中定义的顺序一致,如下 ...

  4. springboot自定义filter获取spring容器bean对象

    今天在自己定义的filter中,想要直接注入spring容器的其它bean进行操作,发现不能正常的注入: 原因:web容器加载顺序导致, 加载顺序是listener——filter——servlet, ...

  5. Shiro权限管理框架(五):自定义Filter实现及其问题排查记录

    明确需求 在使用Shiro的时候,鉴权失败一般都是返回一个错误页或者登录页给前端,特别是后台系统,这种模式用的特别多.但是现在的项目越来越多的趋向于使用前后端分离的方式开发,这时候就需要响应Json数 ...

  6. 解决shiro自定义filter后,ajax登录无法登录,并且无法显示静态资源的问题

    这个问题困扰了我一天,看了下面两个文章,豁然开朗: https://www.cnblogs.com/gj1990/p/8057348.html https://412887952-qq-com.ite ...

  7. 【SpringBoot】SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener

    =================6.SpringBoot拦截器实战和 Servlet3.0自定义Filter.Listener ============ 1.深入SpringBoot2.x过滤器Fi ...

  8. Spring-Boot使用嵌入式容器,那怎么配置自定义Filter呢

    Listener.Filter和Servlet是Java Web开发过程中常用的三个组件,其中Filter组件的使用频率最高,经常被用来做简单的权限处理.请求头过滤和防止XSS攻击等.如果我们使用的是 ...

  9. springboot(八)自定义Filter、自定义Property

    自定义Filter 我们常常在项目中会使用filters用于录调用日志.排除有XSS威胁的字符.执行权限验证等等. Spring Boot自动添加了OrderedCharacterEncodingFi ...

随机推荐

  1. 洛谷 P3795 钟氏映射

    洛谷 P3795 钟氏映射 题目背景 2233年,CSSYZ学校的数学老师兼数学竞赛顾问钟JG已经2200+岁啦! 为了庆生,他或她给广大人民群众出了道题. 题目描述 设集合N=M={x∣x∈N+​, ...

  2. NOIp2018 pj 滚粗记

    NOIp2018 pj 滚粗记 考前 一个午觉睡完就到了考场 考中 \(T1\)水题切了 \(T2\)水题切了 \(T3\)好像是\(dp\),不会,先跳 \(T4\)像树上莫队一样,然后再欧拉序上面 ...

  3. hive streaming 使用的时候的一些心得

    hive streaming 报错的解决方案: 1.把使用到hive streaming 的sql 分解,例如:select transform a,b,c,d using 'python cc.py ...

  4. C#数组 修改

    今天咱们了解下C#中的数组 后面会讲到集合.泛型集合 咱们分开来讲,免得出现混乱 讲完这三个,咱们再汇总一下,看看有什共同点和不同点 定义一个数组: ]; , , , , , , , , , }; 两 ...

  5. 常见的浏览器端的存储技术:cookie

    工作原理: cookie是存在用户硬盘中,用户每次访问站点时,Web应用程序都可以读取Cookie包含的信息.当用户再次访问这个站点时,浏览器就会在本地硬盘上查找与该 URL 相关联的 Cookie. ...

  6. WordPress函数wp_page_menu详解

    说明 该标签显示带有链接的WordPress页面列表,并且可以选择将 Home(主页)自动显示为列表中的一员.该标签是自定义侧边栏和标题栏的好帮手,同时还可以用在其它模板中. WordPress教程 ...

  7. c语言数字图像处理(十):阈值处理

    定义 全局阈值处理 假设某一副灰度图有如下的直方图,该图像由暗色背景下的较亮物体组成,从背景中提取这一物体时,将阈值T作为分割点,分割后的图像g(x, y)由下述公式给出,称为全局阈值处理 多阈值处理 ...

  8. 我的第一个上线小程序,案例实战篇二——LayaAir游戏开始界面开发

    不知不觉我的第一个小程序已经上线一周了,uv也稳定的上升着. 很多人说我的小程序没啥用,我默默一笑,心里说:“它一直敦促我学习,敦促我进步”.我的以一个小程序初衷是经验分享,目前先把经验分享到博客园, ...

  9. vue 使用ref获取DOM元素和组件引用

    在vue中可以通过ref获取dom元素,并操作它,还可以获取组件里面的数据和方法. HTML部分: <div id="app"> <input type=&quo ...

  10. 获400 万美元 A 轮融资,ShipBob 想帮助小微企业享受Amazon Prime 级配送服务 2016-06-18

    Weiss认为,无论零售市场的发展走向如何波动,ShipBob公司都能够获得坚实的成长表现. 在线销售实体商品的小型企业当然希望利用种种方式取悦客户,但面对着Amazon Prime迅如闪电且价格实惠 ...