基于springboot拦截器HandlerInterceptor的注入问题

时间:2022-03-22 00:55:16

springboot拦截器HandlerInterceptor的注入

一、问题描述

项目启动后,拦截器下的注入为null,影响拦截器的逻辑操作。如下图:

基于springboot拦截器HandlerInterceptor的注入问题

二、解决策略

原因: 拦截器加载是在springcontext创建之前完成,详情可以看spring的拦截器加载过程及IOC的关系

解决方案一:使用@Bean在拦截器初始化之前让类加载,重点在于@Bean的拦截器处理及getCheckFilter()的引入

具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 拦截器处理
public class CheckFilter implements HandlerInterceptor {
    @Autowired
    private SystemService systemService;
}
 
// 配置类处理
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
 
    @Bean
    public CheckFilter getCheckFilter(){
        return  new CheckFilter();
    }
 
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getCheckFilter()).addPathPatterns("/**").
                excludePathPatterns("/admin/auth/**");
        super.addInterceptors(registry);
    }
 
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }
 
    @Override
    protected void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/",".jsp");
        super.configureViewResolvers(registry);
    }
}

解决方案二:给拦截器增加配置注解如:@Configuration 、@Component 。在配置类中注入,具体如下操作:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 拦截器处理
@Configuration   //或者 @Component
public class CheckFilter implements HandlerInterceptor {
    @Autowired
    private SystemService systemService;
}
 
// 配置类处理
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
 
    @Autowired
    private CheckFilter  checkFilter;
 
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(checkFilter).addPathPatterns("/**").
                excludePathPatterns("/admin/auth/**");
        super.addInterceptors(registry);
    }
 
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
 registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }
 
    @Override
    protected void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/",".jsp");
        super.configureViewResolvers(registry);
    }
}

三、知识总结

1、涉及spring的拦截器加载过程及IOC的管理

2、参考资料http://www.zzvips.com/article/218698.html

springboot HandlerInterceptor 拦截器匹配规则写错 导致 拦截器无效

项目中发现拦截器无效,访问接口 /user/xxx/xxx 没有进入到拦截器中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
    
    @Autowired
    private UserInterceptor userInterceptor;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    }
   
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userInterceptor).addPathPatterns("/user/*");
    }
}

原因是 拦截器的匹配规则弄错了。我希望 userInterceptor拦截器 拦截 /user/下所有路径的接口

正确拦截规则是 /user/**

拦截规则粟子

addPathPatterns("/**") 表示拦截所有的请求,

addPathPatterns("/**") 表示拦截所有的请求,

addPathPatterns("/test/**") 表示拦截/test/ 下的所有路径请求,

addPathPatterns("/test/*") 表示拦截/test/abc,拦截/test/aaa , 不拦截 /test/abc/def

addPathPatterns("/test/**").excludePathPatterns("/test/login", “/test/register”) 表示拦截/test/ 下的所有路径请求,但不拦截 /test/login 和 /test/register

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

原文链接:https://blog.csdn.net/baidu_28068985/article/details/106659825