springboot由1.5.9升级至2.1.0导致拦截器注入失败

经过百度在此网址得到一些相关说明:https://www.jianshu.com/p/04b75f3108fd

SpringBoot项目配置HandlerInterceptorAdapter继承WebMvcConfigurationSupport导致自动化配置失效的解决方案

相关背景描述
Spring boot 版本:2.1.4.RELEASE
项目开发过程中需要简单校验一下session的有效性,相关用户权限信息。我决定使用Spring mvc 的 HandlerInterceptorAdapter 拦截器在Spring boot 1.X版本中我们只需配置WebMvcConfigurerAdapter即可,Spring boot 2.x 开始,使用Spring 组件的版本都是5.x,WebMvcConfigurerAdapter在Spring5中已经被遗弃,查询资料发现需要使用新的WebMvcConfigurationSupport。使用WebMvcConfigurationSupport新的问题就来了,Spring mvc的自动配置就失效了,静态资源无法正常访问。我在本项目中还想继续使用Spring mvc 的自动配置,找出了解决方案。

验证效果:【并未解决】

最终解决方式:
1、ZhxfInterceptor extends HandlerInterceptorAdapter 方法中 
   去除 @Autowired 
            private ZhxfConf zhxfConf;
   加入 private String uaacType;
        public ZhxfInterceptor(String uaacType){
             this.uaacType = uaacType;
        }

2、CustomMVCConfiguration extends WebMvcConfigurationSupport 方法中
    加入 @Resource
             private ZhxfConf zhxfConf;
    new ZhxfInterceptor() 改为 new ZhxfInterceptor(zhxfConf.getUaacType())

 

部分示例:

@Configuration
@ConfigurationProperties(prefix = "zhxf")
public class ZhxfConf {

    private String uaacType;

    public String getUaacType() {
        return uaacType;
    }
    public void setUaacType(String uaacType) {
        this.uaacType = uaacType;
    }
}

 

public class ZhxfInterceptor extends HandlerInterceptorAdapter {

    private static final Logger logger = LoggerFactory.getLogger(ZhxfInterceptor.class);

    private String uaacType;

    public ZhxfInterceptor(String uaacType){
        this.uaacType = uaacType;
    }

    /**
     * 拦截处理
     * @return true为拦截放行,会继续执行对应接口,false为拦截,不会进入接口api
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        if("1".equals(uaacType)) {
            String requestUrl = request.getRequestURI();
            logger.info("请求path为: {}", request.getRequestURI());
        }
        return true;
    }

}

(省略中间代码):

@Configuration
public class CustomMVCConfiguration extends WebMvcConfigurationSupport {

    @Resource
    private ZhxfConf zhxfConf;
/**
  * 后台拦截,serverlet请求
  **/
@Bean
public ZhxfInterceptor getDemoInterceptor() {
    return new ZhxfInterceptor(zhxfConf.getUaacType());
}
/**
  * 前端拦截,静态资源
  **/
@Bean
public MappedInterceptor getMappedInterceptor() {
    return new MappedInterceptor(new String[] { "/static/html/**" }, new ZhxfInterceptor(zhxfConf.getUaacType()));
}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot 1.5.9中解决跨域问题可以通过实现WebMvcConfigurer接口来配置。首先,创建一个配置类并实现WebMvcConfigurer接口。然后,在该类中重写addCorsMappings方法,使用CorsRegistry对象来添加跨域配置。具体来说,可以通过调用CorsRegistry对象的addMapping方法来指定需要处理跨域的请求路径,并使用allowedOrigins、allowedMethods、allowedHeaders和allowCredentials方法来配置允许的源、方法、头部和凭据。 以下是一个示例配置类的代码片段,用于解决跨域问题: @Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") // 指定需要处理跨域的请求路径,这里是所有路径 .allowedOrigins("*") // 允许所有源 .allowedMethods("*") // 允许所有方法 .allowedHeaders("*") // 允许所有头部 .allowCredentials(true); // 允许发送凭据信息 } } 这段代码中,addMapping("/**")表示将所有请求路径都进行跨域处理,allowedOrigins("*")表示允许所有源,allowedMethods("*")表示允许所有方法,allowedHeaders("*")表示允许所有头部,allowCredentials(true)表示允许发送凭据信息。 通过以上配置,SpringBoot 1.5.9可以成功解决跨域问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SpringBoot教程(七) | SpringBoot解决跨域问题](https://blog.csdn.net/lsqingfeng/article/details/122614630)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [关于SpringBoot 项目跨域问题的几种解决方法](https://blog.csdn.net/2301_76607156/article/details/130485123)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值