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

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

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

2.0版本使用以下形式

@Bean
public ConfigurableServletWebServerFactory configurableServletWebServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addContextCustomizers(context -> {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
collection.addMethod("HEAD");
collection.addMethod("PUT");
collection.addMethod("DELETE");
collection.addMethod("OPTIONS");
collection.addMethod("TRACE");
collection.addMethod("COPY");
collection.addMethod("SEARCH");
collection.addMethod("PROPFIND");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
});
return factory;
}

关于内嵌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. swoole实验版聊天室

    “swoole实验版聊天室”是依据一堂swoole培训课内容改编的,结合了bootstrap前端框架.redis数据库.jquery框架等实现基本功能,只是体现了swoole的应用,并不是为了专门写个 ...

  2. linux iftop查看流量的方法

    linux iftop查看流量的方法iftop 默认是查看eth0网卡的流量 这个是内网iftop -i eth1 检测eht1网卡的流量 eth1一般都是外网 具体可以iftop查看详细信息 < ...

  3. springboot2.0以后的junit

    只需要引入: <dependency> <groupId>junit</groupId> <artifactId>junit</artifactI ...

  4. C++中几种字符串表示方法

    最近学习C++时,被几种字符串搞的有点乱,这里记录一下. c++中有两种风格字符串,分别是: C++风格字符串 C风格字符串 它们各自的声明方式如下: void main(){ string a = ...

  5. Linux下C++酒店管理系统

    功能要求: ​ 相关源码:码云:传送门,GitHub:传送门 相关图片: 拆分版 make编译 ​ ./hotel运行 ​ 输入2,进入开房模块 ​ 相关源码: class.cpp #include ...

  6. centos7中mysql的rpm包安装

    解决依赖 yum remove mysql-libs 执行命令:yum -y install autoconf 安装依赖 yum -y install autoconf 安装mysql rpm -iv ...

  7. 数据采集,SCADA, 使用MQTT的方式来进行消息单/双向传输,什么场景使用MQTT

    1.先来了解下: 看完得出关键字:发布.订阅模式,事件驱动,主题,生产与消费解耦 2.轻量级 普通的socket连接对服务器的消耗太大了,socket服务端是很消耗资源的,一台服务器能链接的客户端是有 ...

  8. 【题解】Luogu P5340 [TJOI2019]大中锋的游乐场

    原题传送门 没想到省选也会出这种题??! 实际就是一个带有限制的最短路 因为\(k<=10\),所以我们珂以暴力将每个点的权值分为[-k,k],为了方便我们珂以转化成[0,2k],将汉堡的权值记 ...

  9. CF933E A Preponderant Reunion DP

    传送门 题解搬运工 设原问题为问题A.每一次减少\(\min\{p_i , p_{i+1}\}\)难以处理,我们考虑将限制变得宽松一些:每一次可以减少\([1,\min\{p_i , p_{i+1}\ ...

  10. git如何支持doc文档

    这个问题很容易解决,只要添加一个 .gitattributes 内容如下: ////////////////////////////////////////////////////////////// ...