• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

springboot 搞代码 4年前 (2022-01-09) 57次浏览 已收录 0个评论
文章目录[隐藏]

springboot拦截器执行两次

原因是:

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

也是一个controller路径为/error

@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("/err<em>本文来源gao.dai.ma.com搞@代*码(网$</em>or");
        //拦截全部
        registration.addPathPatterns("/**");
    }
}

Springboot拦截器原理

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

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

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

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

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

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

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

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

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:springboot 拦截器执行两次的解决方案

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址