首页 分享 spring boot使用内嵌的tomcat解决不安全的HTTP方法安全漏洞

spring boot使用内嵌的tomcat解决不安全的HTTP方法安全漏洞

来源:花匠小妙招 时间:2024-12-15 12:39

最新推荐文章于 2024-11-09 18:21:31 发布

caodongfang126 于 2019-06-21 14:02:46 发布

一:传统Web项目的解决方案:
在tomcat的web.xml配置文件中,对不安全的方法进行拦截:
<security-constraint>  
            <web-resource-collection>  
                <url-pattern>/*</url-pattern>  
                <http-method>HEAD</http-method>  
                <http-method>PUT</http-method>  
                <http-method>DELETE</http-method>  
                <http-method>OPTIONS</http-method>  
                <http-method>TRACE</http-method>  
                <http-method>COPY</http-method>  
                <http-method>SEARCH</http-method>  
                <http-method>PROPFIND</http-method>  
            </web-resource-collection>  
            <auth-constraint>  
            </auth-constraint>  
</security-constraint>  

如果需要禁用TRACE请求,还需要修改tomcat的server.xml配置文件(在server.xml中先允许TRACE请求,再在web.xml中禁用TRACE):
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" allowTrace="true"
               redirectPort="8443" />

二:spring boot的解决方案:
1.众所周知,spring boot的容器是内嵌的,是没有web.xml给我们配置的,所有的配置都是在properties文件中进行配置的,所以我们的思路也是在properties文件中增加tomcat的相关配置。
#解决不安全的HTTP方法漏洞  
server.tomcat.port-header=HEAD,PUT,DELETE,OPTIONS,TRACE,COPY,SEARCH,PROPFIND  

2.代码的方式增加tomcat的配置(本人测试上面配置文件方式不生效,这种方式可以,如有不同看法请留言),代码如下:
package com.xzp;

import org.apache.catalina.Context;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;

@SpringBootApplication(exclude = { SessionAutoConfiguration.class })
@EnableZuulProxy
public class ApiGatewayServerApplictation {

    public static void main(String[] args) {
        SpringApplication.run(ApiGatewayServerApplictation.class, args);
    }
    //主要是以下代码:
    @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);  
            }  
        };  
        //如果需要禁用TRACE请求,需添加以下代码:
        tomcat.addConnectorCustomizers(connector -> {
            connector.setAllowTrace(true);
        });
        return tomcat;  
    }  

}

相关知识

Spring Boot开发的植物健康管理系统研究
Spring Boot + Vue的网上商城之客服系统实现
Spring Boot:植物健康监测的智能专家
基于Spring Boot的植物健康系统
植物健康,Spring Boot来守护
Spring Boot植物健康系统:绿色科技的创新
Spring Boot植物健康系统:智慧农业的新篇章
基于Spring Boot的农田智能管理系统
Springboot生态农业信息管理系统81257(程序+源码+数据库+调试部署+开发环境)
基于Java技术的网上花店系统设计与实现(源码+lw+部署文档+讲解等)

网址: spring boot使用内嵌的tomcat解决不安全的HTTP方法安全漏洞 https://www.huajiangbk.com/newsview1109507.html

所属分类:花卉
上一篇: 伪静态配置
下一篇: python爬虫反虫之setco

推荐分享