springboot禁用内置Tomcat的不安全请求方法

本文介绍如何在SpringBoot应用中禁用不安全的HTTP请求方法,如PUT、DELETE等,以增强安全性。通过配置Tomcat的SecurityConstraint,可以阻止这些方法的使用,适用于不同版本的SpringBoot。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

起因:安全组针对接口测试提出的要求,需要关闭不安全的请求方法,例如put、delete等方法,防止服务端资源被恶意篡改。
用过springMvc都知道可以使用@PostMapping@GetMapping等这种注解限定单个接口方法类型,或者是在@RequestMapping中指定method属性。这种方式比较麻烦,那么有没有比较通用的方法,通过查阅相关资料,答案是肯定的。

tomcat传统形式通过配置web.xml达到禁止不安全的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>
Spring boot使用内置tomcat,2.0版本以前使用如下形式
@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);  
        }  
    };

2.0版本使用以下形式

@Bean
public ConfigurableServletWebServerFactory configurableServletWebServerFactory() {
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
    factory.addContextCustomizers(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);
    });
    return factory;
}

关于内嵌tomcat的更多配置,感兴趣可以阅读以下官方文档。
参考链接:https://docs.spring.io/spring-boot/docs/2.0.0.RC1/reference/htmlsingle/#howto-configure-tomcat

### 集成 SpringBootTomcat 和 Vue 的开发环境配置 #### 1. 开发环境概述 IntelliJ IDEA 是一种强大的 Java 开发工具,可以很好地支持 Spring Boot 应用程序的开发。通过适当配置,可以在 IntelliJ IDEA 中实现 Spring Boot、Tomcat 和 Vue.js 的集成开发。 --- #### 2. 配置 Spring Boot 项目 创建一个新的 Spring Boot 项目并导入到 IntelliJ IDEA 中: - 使用 Spring Initializr 初始化一个新项目,选择所需的依赖项(如 `Spring Web` 和 `Thymeleaf` 或其他前端模板引擎)。 - 导入 Maven/Gradle 构建文件以确保所有依赖项都已加载成功[^1]。 ```xml <dependencies> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> ``` --- #### 3. 配置嵌入式 Tomcat Spring Boot 默认集成了嵌入式的 Tomcat 容器,因此无需额外安装 Tomcat。可以通过修改 `application.properties` 文件来调整 Tomcat 的端口和其他设置: ```properties server.port=8080 server.servlet.context-path=/api ``` 如果需要使用外部 Tomcat,则需禁用内置容器并将 WAR 包部署到外部 Tomcat 上[^2]。 --- #### 4. 前端 Vue.js 集成 为了将 Vue.js 集成到 Spring Boot 项目中,通常有以下两种方法: ##### 方法一:静态资源方式 将 Vue.js 打包后的静态文件放置在 Spring Boot 的 `/src/main/resources/static` 目录下。这样可以直接访问这些静态文件。 ###### 步骤: 1. 创建 Vue.js 项目并通过 npm/yarn 构建打包。 2. 将构建生成的 `dist` 文件夹复制到 Spring Boot 的 `static` 路径下。 3. 启动 Spring Boot 应用即可访问前端页面。 ##### 方法二:前后端分离架构 在这种模式下,Vue.js 提供独立的服务接口调用功能,而 Spring Boot 则作为后端 API 接口服务运行。 ###### 步骤: 1. 在 Vue.js 项目中配置 Axios 或 Fetch 来请求 Spring Boot 提供的 RESTful API。 2. 确保 CORS 设置允许跨域请求,在 Spring Boot 中添加如下配置类: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class CorsConfig { @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**").allowedOrigins("*"); } }; } } ``` --- #### 5. 数据库连接配置 由于系统采用了 MySQL 数据库,需要在 `application.properties` 文件中进行相应的数据库连接配置[^4]: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/school_management?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=password spring.jpa.hibernate.ddl-auto=update ``` --- #### 6. 测试与调试 完成上述配置后,启动 Spring Boot 应用程序,并验证其是否正常工作。对于 Vue.js 部分,可通过浏览器打开对应的 URL 地址进行测试。 --- ### 总结 以上是在 IntelliJ IDEA 中集成 Spring Boot、Tomcat 和 Vue.js 的基本流程。这种方法仅提高了项目的可维护性和扩展性,还简化了开发过程中的复杂度[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值