当前位置: 代码迷 >> 综合 >> HTTP TRACE / TRACK Methods Allowed 问题修复
  详细解决方案

HTTP TRACE / TRACK Methods Allowed 问题修复

热度:99   发布时间:2023-11-22 17:19:27.0

HTTP TRACE / TRACK Methods Allowed问题修复

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.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;/*** @author :dzp* @date :Created in 2021/4/26 20:09* @description:*/
@SpringBootApplication
public class DemoApplication{
    public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);}//主要是以下代码:@Beanpublic EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
    // 1protected 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;}
}
  相关解决方案