欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot2的tomcatConfig的写法

程序员文章站 2022-07-15 10:24:34
...

springboot 1.4 使用的tomcatConfig中代码如下

 @Bean  
    public TomcatServletWebServerFactory 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("PUT");  
                collection.addMethod("DELETE");  
                collection.addMethod("CONNECT");  
                collection.addMethod("OPTIONS");  
                collection.addMethod("PATCH");  
                collection.addMethod("PROPFIND");  
                collection.addMethod("PROPPATCH");  
                collection.addMethod("MKCOL");
                collection.addMethod("COPY");  
                collection.addMethod("MOVE");  
                collection.addMethod("LOCK");  
                collection.addMethod("UNLOCK ");  
                collection.addMethod("TRACE");  
                collection.addMethod("HEAD");  
                securityConstraint.addCollection(collection);  
                context.addConstraint(securityConstraint);  
            }  
        };  

升级springboot2之后,发现EmbeddedServletContainerFactory 没有了。然后发现是springboot2改写法了,改成如下写法,记录一下

   @Bean  
    public TomcatServletWebServerFactory servletContainer() {  
    	TomcatServletWebServerFactory  tomcat = new TomcatServletWebServerFactory () {// 1  
            protected void postProcessContext(Context context) {  
                SecurityConstraint securityConstraint = new SecurityConstraint();  
                securityConstraint.setUserConstraint("CONFIDENTIAL");  
                SecurityCollection collection = new SecurityCollection();  
                collection.addPattern("/*");  
                collection.addMethod("PUT");  
                collection.addMethod("DELETE");  
                collection.addMethod("CONNECT");  
                collection.addMethod("OPTIONS");  
                collection.addMethod("PATCH");  
                collection.addMethod("PROPFIND");  
                collection.addMethod("PROPPATCH");  
                collection.addMethod("MKCOL");
                collection.addMethod("COPY");  
                collection.addMethod("MOVE");  
                collection.addMethod("LOCK");  
                collection.addMethod("UNLOCK ");  
                collection.addMethod("TRACE");  
                collection.addMethod("HEAD");  
                securityConstraint.addCollection(collection);  
                context.addConstraint(securityConstraint);  
            }  
        };