好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring Boot之内嵌tomcat版本升级操作示例

Spring Boot之如何升级内嵌tomcat版本

1. 背景

根据信息安全运营团队发布的Tomcat-AJP协议漏洞风险预警,Tomcat的AJP协议存在高危漏洞(默认8009端口)由于存在实现缺陷导致相关参数可控,攻击者利用该漏洞可通过构造特定参数,读取服务器webapp 下的任意文件。若服务器端同时存在文件上传功能,攻击者可进一步实现远程代码的执行。漏洞CVE编号:CVE-2020-1938,此漏洞风险等级为高危。附件中为全行开发、生产涉及的系统,请大家尽快确认是否使用了AJP协议并按照临时方案进行修复。

修复方案如下:

1、未使用AJP协议方案:直接关闭AJP协议

(1)编辑 <CATALINA_BASE>/conf/server.xml,找到如下行(<CATALINA_BASE> 为 Tomcat 的工作目录):

<Connector port=[8009]protocol=[AJP/1.3] redirectPort=[8443] />

(2)将此行注释掉(也可删掉该行):

<!—<Connectorport=[8009] protocol=[AJP/1.3]redirectPort=[8443] />—>

(3)保存后需重新启动,规则方可生效。

(4)重启后执行netstat -an|grep 8009 检查8009端口已经不在监听状态

2、使用AJP协议:建议将Tomcat立即升级到9.0.31、8.5.51或7.0.100版本进行修复

虽然我们的产品使用内嵌tomcat,只是使用其中的http协议,未用到AJP协议,且已将AJP协议关闭。但是鉴于客户的安全意识很高,对此不认同,强烈要求升级tomcat版本。于是开始踩升级内嵌tomcat的坑啦。

2. 过程

2.1 升级单模块项目的tomcat版本

写了个demo测试内嵌tomcat版本,很容易就升级了。在pom文件里写上tomcat想升级的版本,打包出来,依赖的就是tomcat对应的版本了。

?

1

2

3

< properties >

     < tomcat.version >8.5.51</ tomcat.version >

</ properties >

但是这种升级需要pom里依赖父项目为org.springframework.boot,也就类似于面向对象里的继承父类,并重写父类对应的方法,这个意思你懂的吧?也就是说pom里有如下类似标注,否则直接写tomcat版本升级是不升效的。

?

1

2

3

4

5

< parent >

     < groupId >org.springframework.boot</ groupId >

     < artifactId >spring-boot-starter-parent</ artifactId >

     < version >2.0.8.RELEASE</ version >

</ parent >

2.2 升级包含多个模块的项目

升级多模块的项目的tomcat版本,子模块依赖tomcat,但是子模块的父项目不可能是org.springframework.boot,而是项目对应的父模块,此时第一种方法就不见效了。那我们粗暴的,先将tomcat依赖剔除,再引入对应版本的tomcat版本不就行了嘛。因为tomcat相关的依赖再spring-boot-starter-web依赖模块下面,所以先将它内部包含的tomcat依赖剔除,再引入对应的tomcat版本,具体如下:

?

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

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

< dependency >

     < groupId >org.springframework.boot</ groupId >

     < artifactId >spring-boot-starter-web</ artifactId >

     < exclusions >

         < exclusion >

             < groupId >org.springframework.boot</ groupId >

             < artifactId >spring-boot-starter-tomcat</ artifactId >

         </ exclusion >

         < exclusion >

             < groupId >org.apache.tomcat.embed</ groupId >

             < artifactId >tomcat-embed-core</ artifactId >

         </ exclusion >

         < exclusion >

             < groupId >org.apache.tomcat.embed</ groupId >

             < artifactId >tomcat-embed-el</ artifactId >

         </ exclusion >

         < exclusion >

             < groupId >org.apache.tomcat.embed</ groupId >

             < artifactId >tomcat-embed-websocket</ artifactId >

         </ exclusion >

         < exclusion >

             < groupId >org.apache.tomcat</ groupId >

             < artifactId >tomcat-annotations-api</ artifactId >

         </ exclusion >

     </ exclusions >

</ dependency >

< dependency >

     < groupId >org.apache.tomcat.embed</ groupId >

     < artifactId >tomcat-embed-core</ artifactId >

     < version >${tomcat.version}</ version >

     < exclusions >

         < exclusion >

             < groupId >org.apache.tomcat</ groupId >

             < artifactId >tomcat-annotations-api</ artifactId >

         </ exclusion >

     </ exclusions >

</ dependency >

< dependency >

     < groupId >org.apache.tomcat</ groupId >

     < artifactId >tomcat-annotations-api</ artifactId >

     < version >${tomcat.version}</ version >

</ dependency >

< dependency >

     < groupId >org.apache.tomcat.embed</ groupId >

     < artifactId >tomcat-embed-el</ artifactId >

     < version >${tomcat.version}</ version >

</ dependency >

< dependency >

     < groupId >org.apache.tomcat.embed</ groupId >

     < artifactId >tomcat-embed-websocket</ artifactId >

     < version >${tomcat.version}</ version >

     < exclusions >

         < exclusion >

             < groupId >org.apache.tomcat.embed</ groupId >

             < artifactId >tomcat-embed-core</ artifactId >

         </ exclusion >

     </ exclusions >

</ dependency >

3. 项目有打包子模块如何升级

如果有自己打包子模块,上述就会失效,具体原因还不清楚,但是也很好解决:将如上2步骤的依赖复制黏贴到打包子模块的pom文件里,这样就搞定。

尾声

现在总结起来还是比较简单,但是前一段时间踩坑也是很脑壳疼的。一直升级不生效,只能各种尝试,总算赶在项目发布前解决了,cheers !

以上就是Spring Boot之内嵌tomcat版本升级操作示例的详细内容,更多关于Spring Boot内嵌tomcat版本升级的资料请关注其它相关文章!

原文链接:https://blog.csdn.net/qq_35033270/article/details/104734756

查看更多关于Spring Boot之内嵌tomcat版本升级操作示例的详细内容...

  阅读:56次