springboot , http转https,发送post请求,提示Request method GET not supported

springboot项目 绑定了https协议 但是http的post请求后自动转换成https请求get请求。尝试了baidu能搜到的各种方法,包括添加filter,拦截器,都没啥用。

我的代码如下

    @Bean  
    public TomcatServletWebServerFactory servletContainer() {  
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {  
            @Override  
            protected void postProcessContext(Context context) {  
                SecurityConstraint constraint = new SecurityConstraint();  
                constraint.setUserConstraint("CONFIDENTIAL");  
                SecurityCollection collection = new SecurityCollection();  
                collection.addPattern("/*");  
                constraint.addCollection(collection);  
                context.addConstraint(constraint);  
            }  
        };  
        tomcat.addAdditionalTomcatConnectors(httpConnector());  
        return tomcat;  
    }  
  
    @Bean  
    public Connector httpConnector() {  
        System.out.println("启用http转https协议,http端口:"+this.httpPort+",https端口:"+this.httpsPort);  
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");  
        connector.setScheme("http");  
        //Connector监听的http的端口号  
        connector.setPort(httpPort);  
        connector.setSecure(false);  
        //监听到http的端口号后转向到的https的端口号  
        connector.setRedirectPort(httpsPort);  
        return connector;  
    }

最后在万能的google的帮助下,找到了答案:postProcessContext这个函数中,需要添加collection.addMethod(DEFAULT_PROTOCOL);这一行。

你可能感兴趣的:(springboot , http转https,发送post请求,提示Request method GET not supported)