解决Shiro+SpringBoot自定义Filter不生效问题
在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不生效问题的更多相关文章
- SpringBoot自定义Filter
SpringBoot自定义Filter SpringBoot自动添加了OrderedCharacterEncodingFilter和HiddenHttpMethodFilter,当然我们可以自定 义F ...
- 解决Spring Security自定义filter重复执行问题
今天做项目的时候,发现每次拦截器日志都会打两遍,很纳闷,怀疑是Filter被执行了两遍.结果debug之后发现还真是!记录一下这个神奇的BUG! 问题描述 项目中使用的是Spring-security ...
- 细说shiro之自定义filter
写在前面 我们知道,shiro框架在Java Web应用中使用时,本质上是通过filter方式集成的. 也就是说,它是遵循过滤器链规则的:filter的执行顺序与在web.xml中定义的顺序一致,如下 ...
- springboot自定义filter获取spring容器bean对象
今天在自己定义的filter中,想要直接注入spring容器的其它bean进行操作,发现不能正常的注入: 原因:web容器加载顺序导致, 加载顺序是listener——filter——servlet, ...
- Shiro权限管理框架(五):自定义Filter实现及其问题排查记录
明确需求 在使用Shiro的时候,鉴权失败一般都是返回一个错误页或者登录页给前端,特别是后台系统,这种模式用的特别多.但是现在的项目越来越多的趋向于使用前后端分离的方式开发,这时候就需要响应Json数 ...
- 解决shiro自定义filter后,ajax登录无法登录,并且无法显示静态资源的问题
这个问题困扰了我一天,看了下面两个文章,豁然开朗: https://www.cnblogs.com/gj1990/p/8057348.html https://412887952-qq-com.ite ...
- 【SpringBoot】SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener
=================6.SpringBoot拦截器实战和 Servlet3.0自定义Filter.Listener ============ 1.深入SpringBoot2.x过滤器Fi ...
- Spring-Boot使用嵌入式容器,那怎么配置自定义Filter呢
Listener.Filter和Servlet是Java Web开发过程中常用的三个组件,其中Filter组件的使用频率最高,经常被用来做简单的权限处理.请求头过滤和防止XSS攻击等.如果我们使用的是 ...
- springboot(八)自定义Filter、自定义Property
自定义Filter 我们常常在项目中会使用filters用于录调用日志.排除有XSS威胁的字符.执行权限验证等等. Spring Boot自动添加了OrderedCharacterEncodingFi ...
随机推荐
- Verilog 位拼接运算符的优先级
最近研究FIFO的时候,在开源工程中看到这样一段代码 ; always @(posedge rd_clk) {'b0}}; else {'b0}}; else if(re) rp_bin <= ...
- 20155211课下测试ch10补交
20155211课下测试ch10补交 1.假设下面代码中的foobar.txt中有6个ASCII字母,程序的输出是() A.c = f B.c = o C.c = b D.c = 随机数 答案:A 解 ...
- 20155236 2016-2017-2 《Java程序设计》第九周学习总结
20155236 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 JDBC入门 1.JDBC简介 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标 ...
- 【转】QT事件传递与事件过滤器
[概览] 1.重载特定事件函数. 比如: mousePressEvent(),keyPressEvent(), paintEvent() . 2.重新实现QObject::ev ...
- MTCNN(Multi-task convolutional neural networks)人脸对齐
MTCNN(Multi-task convolutional neural networks)人脸对齐 .
- 10 ORM 多表操作 查询
1.子查询:基于对象的跨表查询 def query(request): """ 跨表查询: 1.基于对象查询 2.基于双下划线查询 3.聚合.分组查询 4. F Q 查询 ...
- 【HNOI2015】实验比较
题面 题解 首先将所有相等的用并查集缩点,然后会发现题目有一个很有用的性质: 对每张图片\(i\),小D都最多只记住了某一张质量不比\(i\)差的另一张图片\(K_i\). 于是将\(K_i\)作为\ ...
- 【HNOI2014】抄卡组
题面 题解 如果所有的字符串都有通配符,那么只要比较不含通配符的前缀和后缀就可以了. 否则一定有一个串没有通配符.找出这个字符串,然后将所有串与这个串匹配,通配符将\(B\)分成一段一段在\(A\)上 ...
- Wannafly挑战赛24 B 222333
小水题???但是时间限制异常鬼畜,跑了2min \(P | (2^m)*(3^n)-1\)的意思就是\(2^m 3^n = 1 (\text{mod }P)\) 设f[i]表示3^k=i的最小的k 然 ...
- Python爬虫之HTTP和HTTPS
一:HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的方法,以明文的形式传输,效率高,但是不安全 HTTPS ...