昨天,在公布了《Spring官宣抵赖网传大破绽,并提供解决方案》之后。群里就有几个小伙伴问了这样的问题:咱们的Spring版本比拟老,该怎么办?这是一个好问题,所以DD明天独自拿出来说说。
这次的RCE破绽发表之后,官网给出的次要解决方案是降级版本,但只有Spring 5.2、5.3和Spring Boot 2.5、2.6提供了对应的降级版本。
那么对于一些还在用Spring 5.0、5.1甚至Spring 4.x、或者Spring Boot 1.x和Spring 2.4及以下版本的用户该怎么办呢?
第一种办法
官网给出过一种通过扩大RequestMappingHandlerAdapter
来实现的办法。同时也给出了一个Spring Boot下应用Spring MVC的实现计划,如果是WebFlux的话略做批改即可。但如果不是Spring Boot的话,则Bean的初始化形式还要再改改。
@SpringBootApplicationpublic class MyApp { public static void main(String[] args) { SpringApplication.run(CarApp.class, args); } @Bean public WebMvcRegistrations mvcRegistrations() { return new WebMvcRegistrations() { @Override public RequestMappingHandlerAdapter getRequestMappingHandlerAdapter() { return new ExtendedRequestMappingHandlerAdapter(); } }; } private static class ExtendedRequestMappingHandlerAdapter extends RequestMappingHandlerAdapter { @Override protected InitBinderDataBinderFactory createDataBinderFactory(List<InvocableHandlerMethod> methods) { return new ServletRequestDataBinderFactory(methods, getWebBindingInitializer()) { @Override protected ServletRequestDataBinder createBinderInstance( Object target, String name, NativeWebRequest request) throws Exception { ServletRequestDataBinder binder = super.createBinderInstance(target, name, request); String[] fields = binder.getDisallowedFields(); List<String> fieldList = new ArrayList<>(fields != null ? Arrays.asList(fields) : Collections.emptyList()); fieldList.addAll(Arrays.asList("class.*", "Class.*", "*.class.*", "*.Class.*")); binder.setDisallowedFields(fieldList.toArray(new String[] {})); return binder; } }; } }}
这种须要咱们去批改代码,其实我感觉还是有点麻烦的。如果对Spring机制不太熟悉的话,可能还会遇到不少麻烦。上面讲讲另外的便捷办法,也是我对老我的项目举荐的办法。
第二种办法
上面要讲的办法次要是躲避的思路。什么是躲避呢?就是针对该破绽的利用条件去做一些调整。
比方,这次破绽的条件是这些:
- JDK 9 +
- 应用Apache Tomcat部署
- 应用WAR形式打包
- 依赖spring-webmvc或spring-webflux
那么我就能够抉择躲避其中的1个条件就能避免破绽的利用了,比方:
- 降级到JDK 8
- 应用Undertow来部署
- 如果是Spring Boot的晚期项的话,还能调整打包形式,采纳JAR的形式打包和运行来躲避。
另外,DD有留神到,这次破绽之后Tomcat的版本也更新了,所以当你用WAR部署的状况下,能够间接下载最新的Tomcat版本来躲避也是一种不错的抉择。
好了,明天的分享就到这里,解决群友(点击加群)的疑难是一方面,另一方面也是给大家讲讲解决问题时候的一种思考形式。有时候碰到硬茬,咱们不肯定要硬刚,换个方向解决可能性价比更高。如果您感觉明天的分享还不错,欢送点赞、在看、转发到朋友圈。
欢送关注我的公众号:程序猿DD。第一工夫理解前沿行业音讯、分享深度技术干货、获取优质学习资源