From 2ed1199d48f7207f4a012c04f61e13ac1a8d5154 Mon Sep 17 00:00:00 2001
From: chenjiahe <763432473@qq.com>
Date: 星期四, 16 六月 2022 10:27:44 +0800
Subject: [PATCH] 新增请求安全工具

---
 src/main/java/com/hx/security/request/RequestRestriction.java |   39 +++++++++++++++++++++++++++++++++++++++
 pom.xml                                                       |    6 ++++++
 2 files changed, 45 insertions(+), 0 deletions(-)

diff --git a/pom.xml b/pom.xml
index 8cc4bb0..03e7429 100644
--- a/pom.xml
+++ b/pom.xml
@@ -141,6 +141,12 @@
             <artifactId>jstl</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-tomcat</artifactId>
+            <!--<scope>provided</scope>-->
+        </dependency>
+
         <!-- mybatis鑷姩鐢熸垚 -->
         <dependency>
             <groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
diff --git a/src/main/java/com/hx/security/request/RequestRestriction.java b/src/main/java/com/hx/security/request/RequestRestriction.java
new file mode 100644
index 0000000..825855c
--- /dev/null
+++ b/src/main/java/com/hx/security/request/RequestRestriction.java
@@ -0,0 +1,39 @@
+package com.hx.security.request;
+
+import org.apache.tomcat.util.descriptor.web.SecurityCollection;
+import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
+import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
+import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**璁剧疆闄愬埗璇锋眰*/
+@Configuration
+public class RequestRestriction {
+
+    @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");
+            //collection.addMethod("PATCH");
+            securityConstraint.addCollection(collection);
+            context.addConstraint(securityConstraint);
+        });
+        return factory;
+    }
+
+
+}

--
Gitblit v1.8.0