SpringBoot解决不安全的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. }  


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

Spring Boot 中集成 Swagger UI 遇到的安全漏洞主要是由于 Swagger 提供的 UI 没有默认启用安全保护机制,这可能导致未经授权的访问敏感信息。要解决这个问题,你可以采取以下几个步骤: 1. 添加 Swagger 和 Springfox依赖:首先,在你的 Maven 或 Gradle 项目中添加 Swagger 和 springfox-swagger2 的依赖。 **Maven**: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.x.y</version> <!-- 更新到最新版本 --> </dependency> ``` **Gradle**: ```groovy implementation 'io.springfox:springfox-boot-starter:3.x.y' // 更新到最新版本 ``` 2. 安全配置 Swagger:在 `application.properties` 或 `application.yml` 文件中,启用Swagger的 JSON 文档支持,并设置基本认证: ```yaml swagger: enabled: true info: version: "1.0" title: "API文档" path: /api-docs base-package: com.example.demo.controller security: basic: enabled: true user-info-uri: /users/me ``` 这里设置了基本认证,你可以替换为你项目的实际用户名密码路径。 3. 创建用户验证服务:如果需要更复杂的验证机制,可以创建一个自定义的 SecurityConfiguration 类,覆盖默认的 BasicAuthenticationFilter,实现 JWT、OAuth2 等其他认证方式。 4. 使用全局拦截器(Global Interceptor):如果你希望所有 API 调用都需要验证,可以在 WebMvcConfigurerAdapter 或其对应的注解处理器中添加全局过滤器。 5. 配置 Swagger UI 的安全:将 Swagger UI 的资源映射到安全控制下的路径,例如通过`http://localhost:8080/swagger-ui.html?configUrl=/v2/api-docs` 来指向已授权的 API 文档。 ```java @Configuration public class SwaggerConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/") .setCachePeriod(3600); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } } ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值