服务器之家

服务器之家 > 正文

springboot 拦截器执行两次的解决方案

时间:2022-01-05 00:40     来源/作者:吕小小布

springboot拦截器执行两次

原因是:

org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error

也是一个controller路径为/error

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Configuration
public class MVCConfig extends WebMvcConfigurationSupport {
     //自定义的拦截器
    @Bean
    public SecurityInterceptor getSecurityInterceptor() {
        return new SecurityInterceptor();
    }
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加拦截器
        InterceptorRegistration registration = registry.addInterceptor(getSecurityInterceptor());
        //排除的路径
        registration.excludePathPatterns("/login");
        registration.excludePathPatterns("/logout");
        //将这个controller放行
        registration.excludePathPatterns("/error");
        //拦截全部
        registration.addPathPatterns("/**");
    }
}

Springboot拦截器原理

根据当前请求,找到**HandlerExecutionChain**** 【可以处理请求的handler以及handler的所有 拦截器】

先来顺序执行 所有拦截器的 preHandle方法

如果当前拦截器prehandler返回为true。则执行下一个拦截器的preHandle

如果当前拦截器返回为false。直接 倒序执行所有已经执行了的拦截器的 afterCompletion;

如果任何一个拦截器返回false。直接跳出不执行目标方法

所有拦截器都返回True。执行目标方法

倒序执行所有拦截器的postHandle方法。

前面的步骤有任何异常都会直接倒序触发 afterCompletion

页面成功渲染完成以后,也会倒序触发 afterCompletion

springboot 拦截器执行两次的解决方案springboot 拦截器执行两次的解决方案

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_37059838/article/details/88040608

标签:

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国 2021-05-08
返回顶部