温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Springboot怎么使用内置tomcat禁止不安全HTTP

发布时间:2022-04-02 16:00:21 来源:亿速云 阅读:502 作者:iii 栏目:移动开发

本文小编为大家详细介绍“Springboot怎么使用内置tomcat禁止不安全HTTP”,内容详细,步骤清晰,细节处理妥当,希望这篇“Springboot怎么使用内置tomcat禁止不安全HTTP”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

Springboot 内置tomcat禁止不安全HTTP方法

1、在tomcat的web.xml中可以配置如下内容

让tomcat禁止不安全的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>

2、Spring boot使用内置tomcat

没有web.xml配置文件,可以通过以下配置进行,简单来说就是要注入到Spring容器中

@Configuration
public class TomcatConfig { 
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcatServletContainerFactory = new TomcatEmbeddedServletContainerFactory();
        tomcatServletContainerFactory.addContextCustomizers(new TomcatContextCustomizer(){
 
   @Override
   public void customize(Context context) {
    SecurityConstraint constraint = new SecurityConstraint();
    SecurityCollection collection = new SecurityCollection();
    //http方法
    collection.addMethod("PUT");
    collection.addMethod("DELETE");
    collection.addMethod("HEAD");
    collection.addMethod("OPTIONS");
    collection.addMethod("TRACE");
    //url匹配表达式
    collection.addPattern("/*");
    constraint.addCollection(collection);
    constraint.setAuthConstraint(true);
    context.addConstraint(constraint );
    
    //设置使用httpOnly
    context.setUseHttpOnly(true);    
   }
        });
        return tomcatServletContainerFactory;
    } 
}

启用不安全的HTTP方法

问题描述:

可能会在Web服务器上上载、修改或删除Web页面、脚本和文件。

"启用了不安全的HTTP方法:OPTIONS /system HTTP/1.1Allow: HEAD, PUT, DELETE, TRACE, OPTIONS, PATCH

上述方法的用途:

  • Options、Head、Trace:主要由应用程序来发现和跟踪服务器支持和网络行为;

  • Get:检索文档;

  • Put和Post:将文档提交到服务器;

  • Delete:销毁资源或集合;

  • Mkcol:创建集合

  • PropFind和PropPatch:针对资源和集合检索和设置属性;

  • Copy和Move:管理命名空间上下文中的集合和资源;

  • Lock和Unlock:改写保护

很显然上述操作明细可以对web服务器进行上传、修改、删除等操作,对服务造成威胁。虽然WebDAV有权限控制但是网上一搜还是一大堆的攻击方法,所以如果不需要这些方法还是建议直接屏蔽就好了。

解决方案:

在web应用中的web.xml加上如下内容

<security-constraint>
        <web-resource-collection>
            <web-resource-name>disp</web-resource-name>
            <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>
            <http-method>PATCH</http-method>
        </web-resource-collection>
        <auth-constraint></auth-constraint>
    </security-constraint>

标签介绍:

  • <security-constraint>用于限制对资源的访问;

  • <auth-constraint>用于限制那些角色可以访问资源,这里设置为空就是禁止所有角色用户访问;

  • <url-pattern>指定需要验证的资源

  • <http-method>指定那些方法需要验证

读到这里,这篇“Springboot怎么使用内置tomcat禁止不安全HTTP”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI