溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2022-04-02 16:00:21 來源:億速云 閱讀:529 作者:iii 欄目:移動開發(fā)

本文小編為大家詳細介紹“Springboot怎么使用內置tomcat禁止不安全HTTP”,內容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“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:主要由應用程序來發(fā)現(xiàn)和跟蹤服務器支持和網絡行為;

  • Get:檢索文檔;

  • Put和Post:將文檔提交到服務器;

  • Delete:銷毀資源或集合;

  • Mkcol:創(chuàng)建集合

  • 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”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI