• 使用HandlerInterceptor 中注入其他service时为null分析及解决


    一情况分析

    1.1 拦截器代码

    public class ServerInterceptor implements HandlerInterceptor {
        private static final Logger _logger = LoggerFactory.getLogger(ServerInterceptor.class);
        PrintWriter out;
        JSONObject res = new JSONObject();
        
        @Autowired
        UserInfoLoginService userInfoLoginService;
        @Autowired
        SysUserService sysUserService;
        
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            _logger.info("ServerInterceptor逻辑处理");
            String header = request.getHeader("Server");
            if (!"Server".equals(header)) {
                _logger.info("ServerInterceptor无权限");
                res.put("code", "Server");
                res.put("msg", "ServerInterceptor无权限");
                res.put("data", null);
                out = response.getWriter();
                out.append(res.toString());
                return false;
            }
            return true;
        }
    
    
    • 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

    1.2 拦截器配置

    在这里插入图片描述

    1.3 原因

    造成 UserInfoLoginService,SysUserService 为null的原因:
    拦截器加载是在springcontext创建之前完成的,在拦截器配置进行加载,进行new ServerInterceptor()时,这里只是创建的ServerInterceptor空对象,并没有将内部属性进行注入;容器启动结束在使用拦截器时注入实体自然就为null。

    二解决方案

    在拦截器配置中将拦截器提前暴露,交给spring容器管理

    @Configuration
    public class InterceptorConfig implements WebMvcConfigurer {
        @Resource
        InterceptorProperties interceptorProperties;
    
        @Resource
        ServerInterceptor serverInterceptor;
    
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
    
    
            //注册ServerInterceptor
            InterceptorRegistration serverRegistration = registry.addInterceptor(serverInterceptor);
    
            if (!CollectionUtils.isEmpty(interceptorProperties.getIncludeFilterServerPaths())) {
                // Server拦截路径
               serverRegistration.addPathPatterns(interceptorProperties.getIncludeFilterServerPaths());
            }
           
    
    
        }
    }
    
    • 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

    2、拦截器添加@Component,交给容器管理

    @Component
    public class ServerInterceptor implements HandlerInterceptor {
        private static final Logger _logger = LoggerFactory.getLogger(ServerInterceptor.class);
        PrintWriter out;
        JSONObject res = new JSONObject();
    
        @Autowired
        UserInfoLoginService userInfoLoginService;
    
        @Autowired
        SysUserService sysUserService;
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            _logger.info("ServerInterceptor逻辑处理");
            String header = request.getHeader("Server");
            if (!"Server".equals(header)) {
                _logger.info("ServerInterceptor无权限");
                res.put("code", "Server");
                res.put("msg", "ServerInterceptor无权限");
                res.put("data", null);
                out = response.getWriter();
                out.append(res.toString());
                return false;
            }
            return true;
        }
    
    }
    
    • 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
  • 相关阅读:
    如何配置React-Router?
    蓝桥杯练习题(3的倍数)
    双十一游戏党必备的数码好物有哪些?2022双11游戏党必备外设清单
    helm部署gitlab-runner问题解决
    点击劫持概念及解决办法
    MidJourney | AI绘画也有艺术
    分公司电脑访问总部服务器突然不通了走的是SSL隧道,如何排查处理?
    靶机: medium_socnet
    小程序中实现获取全部数据
    六、从零实战企业级K8S本地部署ThingsBoard专业版集群
  • 原文地址:https://blog.csdn.net/weixin_43811057/article/details/132872283