前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot禁用内置Tomcat的不安全请求方法

springboot禁用内置Tomcat的不安全请求方法

作者头像
河岸飞流
发布2019-08-09 14:48:46
5K0
发布2019-08-09 14:48:46
举报
文章被收录于专栏:开发杂记开发杂记

起因:安全组针对接口测试提出的要求,需要关闭不安全的请求方法,例如put、delete等方法,防止服务端资源被恶意篡改。 用过springMvc都知道可以使用@PostMapping@GetMapping等这种注解限定单个接口方法类型,或者是在@RequestMapping中指定method属性。这种方式比较麻烦,那么有没有比较通用的方法,通过查阅相关资料,答案是肯定的。

tomcat传统形式通过配置web.xml达到禁止不安全的http方法
代码语言:javascript
复制
    <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版本以前使用如下形式
代码语言:javascript
复制
@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版本使用以下形式

代码语言:javascript
复制
@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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • tomcat传统形式通过配置web.xml达到禁止不安全的http方法
  • Spring boot使用内置tomcat,2.0版本以前使用如下形式
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档