SpringBoot实践之---解决不安全的HTTP方法漏洞配置

最近项目开发完毕,在进行安全测试的时候,爆出了一个中级安全漏洞--不安全的HTTP方法,如果对这个安全漏洞有不明白的地方,可以自行问度娘。

1、传统Web项目的解决方案

在不使用spring boot的情况下,有两种解决方案1、在过滤器中进行拦截,对于不是http安全的方法直接给前端返回错误信息;2、在tomcat的web.xml配置,对不安全的方法进行拦截。下面,我们重点说下第二种方案,因为这种方案对所有可能存在此安全漏洞的系统都启作用,不用在每个系统中都进行处理。在tomcat的web.xml配置文件中,加如如下的配置文件:

[html]  view plain  copy
  1. <security-constraint>  
  2.             <web-resource-collection>  
  3.                 <url-pattern>/*</url-pattern>  
  4.                 <http-method>HEAD</http-method>  
  5.                 <http-method>PUT</http-method>  
  6.                 <http-method>DELETE</http-method>  
  7.                 <http-method>OPTIONS</http-method>  
  8.                 <http-method>TRACE</http-method>  
  9.                 <http-method>COPY</http-method>  
  10.                 <http-method>SEARCH</http-method>  
  11.                 <http-method>PROPFIND</http-method>  
  12.             </web-resource-collection>  
  13.             <auth-constraint>  
  14.             </auth-constraint>  
  15.     </security-constraint>  

加入这个配置之后,每当我们请求服务器资源的时候,会对请求进行拦截,然后只允许安全的HTTP方法过去。


2、spring boot的解决方案

方案1:

众所周知,spring boot的容器是内嵌的,是没有web.xml给我们配置的,所有的配置都是在properties文件中进行配置的,所以我们的思路也是在properties文件中增加tomcat的相关配置

[java]  view plain  copy
  1. #解决不安全的HTTP方法漏洞  
  2. server.tomcat.port-header=HEAD,PUT,DELETE,OPTIONS,TRACE,COPY,SEARCH,PROPFIND  

关于tomcat的其他配置,有兴趣的可以自己查阅官网。

方案2:

代码的方式增加tomcat的配置,代码如下:

[java]  view plain  copy
  1. @Configuration  
  2. public class TomcatConfig {  
  3.   
  4.     @Bean  
  5.     public EmbeddedServletContainerFactory servletContainer() {  
  6.         TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {// 1  
  7.             protected void postProcessContext(Context context) {  
  8.                 SecurityConstraint securityConstraint = new SecurityConstraint();  
  9.                 securityConstraint.setUserConstraint("CONFIDENTIAL");  
  10.                 SecurityCollection collection = new SecurityCollection();  
  11.                 collection.addPattern("/*");  
  12.                 collection.addMethod("HEAD");  
  13.                 collection.addMethod("PUT");  
  14.                 collection.addMethod("DELETE");  
  15.                 collection.addMethod("OPTIONS");  
  16.                 collection.addMethod("TRACE");  
  17.                 collection.addMethod("COPY");  
  18.                 collection.addMethod("SEARCH");  
  19.                 collection.addMethod("PROPFIND");  
  20.                 securityConstraint.addCollection(collection);  
  21.                 context.addConstraint(securityConstraint);  
  22.             }  
  23.         };  
  24.         return tomcat;  
  25.     }  
  26. }  

这两种方式其实是等同的。个人认为第一种方式更简单灵活。


猜你喜欢

转载自blog.csdn.net/luckykapok918/article/details/79668509
今日推荐