起因:安全组针对接口测试提出的要求,需要关闭不安全的请求方法,例如put、delete等方法,防止服务端资源被恶意篡改。

用过springMvc都知道可以使用@PostMapping@GetMapping等这种注解限定单个接口方法类型,或者是在@RequestMapping中指定method属性。这种方式比较麻烦,那么有没有比较通用的方法,通过查阅相关资料,答案是肯定的。

tomcat传统形式通过配置web.xml达到禁止不安全的http方法
  1. <security-constraint>
  2. <web-resource-collection>
  3. <url-pattern>/*</url-pattern>
  4. <http-method>PUT</http-method>
  5. <http-method>DELETE</http-method>
  6. <http-method>HEAD</http-method>
  7. <http-method>OPTIONS</http-method>
  8. <http-method>TRACE</http-method>
  9. </web-resource-collection>
  10. <auth-constraint>
  11. </auth-constraint>
  12. </security-constraint>
  13. <login-config>
  14. <auth-method>BASIC</auth-method>
  15. </login-config>
Spring boot使用内置tomcat,2.0版本以前使用如下形式
  1. @Bean
  2. public EmbeddedServletContainerFactory servletContainer() {
  3. TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {// 1
  4. protected void postProcessContext(Context context) {
  5. SecurityConstraint securityConstraint = new SecurityConstraint();
  6. securityConstraint.setUserConstraint("CONFIDENTIAL");
  7. SecurityCollection collection = new SecurityCollection();
  8. collection.addPattern("/*");
  9. collection.addMethod("HEAD");
  10. collection.addMethod("PUT");
  11. collection.addMethod("DELETE");
  12. collection.addMethod("OPTIONS");
  13. collection.addMethod("TRACE");
  14. collection.addMethod("COPY");
  15. collection.addMethod("SEARCH");
  16. collection.addMethod("PROPFIND");
  17. securityConstraint.addCollection(collection);
  18. context.addConstraint(securityConstraint);
  19. }
  20. };

2.0版本使用以下形式

  1. @Bean
  2. public ConfigurableServletWebServerFactory configurableServletWebServerFactory() {
  3. TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
  4. factory.addContextCustomizers(context -> {
  5. SecurityConstraint securityConstraint = new SecurityConstraint();
  6. securityConstraint.setUserConstraint("CONFIDENTIAL");
  7. SecurityCollection collection = new SecurityCollection();
  8. collection.addPattern("/*");
  9. collection.addMethod("HEAD");
  10. collection.addMethod("PUT");
  11. collection.addMethod("DELETE");
  12. collection.addMethod("OPTIONS");
  13. collection.addMethod("TRACE");
  14. collection.addMethod("COPY");
  15. collection.addMethod("SEARCH");
  16. collection.addMethod("PROPFIND");
  17. securityConstraint.addCollection(collection);
  18. context.addConstraint(securityConstraint);
  19. });
  20. return factory;
  21. }

关于内嵌tomcat的更多配置,感兴趣可以阅读以下官方文档。

参考链接:https://docs.spring.io/spring-boot/docs/2.0.0.RC1/reference/htmlsingle/#howto-configure-tomcat

本文首发于个人公众号:河岸飞流,欢迎订阅

原文链接:https://mp.weixin.qq.com/s/bqUwkqZyHQEkWDR9fqEqJA

springboot禁用内置Tomcat的不安全请求方法的更多相关文章

  1. Spring boot 内置tomcat禁止不安全HTTP方法

    Spring boot 内置tomcat禁止不安全HTTP方法 在tomcat的web.xml中可以配置如下内容,让tomcat禁止不安全的HTTP方法 <security-constraint ...

  2. 模拟Springboot二:内置tomcat

    既然要将tomcat内置到项目中,并且能够成功的启动项目就要知道 tomcat  做了哪些事情 ,那么就必须先搞明白 一个 普通的web项目是如何被我们本地配置的tomcat启动并运行的 (1). 先 ...

  3. springboot让内置tomcat失效

    一.POM(去除内嵌tomcat后,需要添加servlet依赖) <dependency> <groupId>org.springframework.boot</grou ...

  4. 如何优雅的使用springboot项目内置tomcat

    问题:以前,我们在使用SSM框架的时候,都是通过外置的tomcat进行部署,如果想访问文件,直接拖到项目的根目录下面即可.假如我们需要放一个apk文件,然后让别人下载,只需将apk放到项目根目录下面, ...

  5. Spring Boot 添加jersey-mvc-freemarker依赖后内置tomcat启动不了解决方案

    我在我的Spring Boot 项目的pom.xml中添加了jersey-mvc-freemarker依赖后,内置tomcat启动不了. 报错信息如下: org.springframework.con ...

  6. Spring Boot2.0之 原理—创建内置Tomcat容器

    前面所述的https://www.cnblogs.com/toov5/p/9823728.html 中的第一条先不赘述了,就是玩了maven 重点介绍后两条 首先内置Tomcat: SpringBoo ...

  7. springboot内置tomcat验证授权回调页面域名

    springboot内置tomcat验证公众号授权回调页面域名 解决方法: 网上下载一个tomcat,在server.xml文件中修改端口为springboot内置tomcat的端口号,复制验证文件到 ...

  8. SpringBoot内置tomcat启动原理

    前言          不得不说SpringBoot的开发者是在为大众程序猿谋福利,把大家都惯成了懒汉,xml不配置了,连tomcat也懒的配置了,典型的一键启动系统,那么tomcat在springb ...

  9. SpringBoot 常用配置 静态资源访问配置/内置tomcat虚拟文件映射路径

    Springboot 再模板引擎中引入Js等文件,出现服务器拒绝访问的错误,需要配置过滤器 静态资源访问配置 @Configuration @EnableWebMvc public class Sta ...

随机推荐

  1. 量化编程技术—pandas与数据分析

    # -*- coding: utf-8 -*- # @Date: 2017-08-26 # @Original: import numpy as np stock_cnt = 200 view_day ...

  2. Javascript / Nodejs call 和 apply

    call: 改变了函数运行的作用域,即改变函数里面this的指向apply:同call,apply第二个参数是数组结构 例如: this.name = 'Ab'var obj = {name: 'BB ...

  3. 【视频开发】Gstreamer框架中使用gst-launch进行流媒体播放

    Gstreamer框架中使用gst-launch进行流媒体播放 Gstreamer是一套开源的流媒体框架,用其也可以进行流媒体开发,Gstreamer是基于glib库编写的,需要将多个不同功能的元件( ...

  4. Fiddler抓包工具的简单使用

    Fiddler的官方网站:http://www.fiddler2.com Fiddler的官方帮助:http://docs.telerik.com/fiddler/knowledgebase/quic ...

  5. centos7安装rsync及两台机器进行文件同步

    安装及配置 yum -y install rsync #启动rsync服务 systemctl start rsyncd.service systemctl enable rsyncd.service ...

  6. QT5学习记录(一)

    学习环境:Windows10 + QT5.13 + QT Creater4.9.1(2019-08-10 22:02:30) 1.基本工程创建操作 常规操作创建画面,可选择QDialog.MainWi ...

  7. go上传图片微信服务器<<临时素材

    type WxImage struct { Type string `json:"type"` MediaId string `json:"media_id"` ...

  8. Luogu2481 SDOI2010 代码拍卖会 DP、组合

    传送门 神仙DP 注意到\(N \leq 10^{18}\),不能够直接数位DP,于是考虑形成的\(N\)位数的性质. 因为低位一定不会比高位小,所以所有满足条件的\(N\)位数一定是不超过\(9\) ...

  9. Spring AOP 创建Advice 定义pointcut、advisor

    前面定义的advice都是直接植入到代理接口的执行之前和之后,或者在异常发生时,事实上,还可以对植入的时机定义的更细. Pointcut定义了advice的应用时机,在Spring中pointcutA ...

  10. C#泛型集合之——队列与堆栈

    队列与堆栈基础 队列 1.操作: (1)创建及初始化: Queue<类型> 队列名 =new Queue<类型>()://空队列,无元素 Queue<类型> 队列名 ...