Spring Boot“请求方法'GET'不受支持”同时通过Catalina Connector将POST请求重定向到https端口 [英] Spring Boot "Request method 'GET' not supported" while redirecting POST request to https port through Catalina Connector

查看:1220
本文介绍了Spring Boot“请求方法'GET'不受支持”同时通过Catalina Connector将POST请求重定向到https端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Spring Boot应用程序中通过Catalina Connector将http重定向到https。如果传入的POST请求是https,那么它正在按预期工作。但是,如果我的传入POST请求是http,并且在通过以下代码重定向到https之后,某个地方它将被更改为GET因为我正在获得 -

I'm trying to redirect http to https through Catalina Connector in my Spring Boot application. If the incoming POST request is "https" then it is working as expected. But if my incoming POST request is "http" and after the redirection to "https" through below code, somewhere it is getting changed to GET because of which I'm getting -

WARN 45028 --- [nio-8443-exec-8] osweb.servlet.PageNotFound:不支持请求方法'GET'

WARN 45028 --- [nio-8443-exec-8] o.s.web.servlet.PageNotFound : Request method 'GET' not supported

以下是@SpringBootApplication类中的方法:

Below are the methods in my @SpringBootApplication class:

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected void postProcessContext(org.apache.catalina.Context context) {
          SecurityConstraint securityConstraint = new SecurityConstraint();
          securityConstraint.setUserConstraint("CONFIDENTIAL");
          SecurityCollection collection = new SecurityCollection();
          collection.addPattern("/*");
          securityConstraint.addCollection(collection);
          context.addConstraint(securityConstraint);
        }
    };

    tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
    return tomcat;
 }

private Connector initiateHttpConnector() {
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setScheme("http");
    connector.setPort(10024);
    connector.setSecure(false);
    connector.setRedirectPort(8443);

    return connector;
 }


推荐答案

添加 collection.addMethod(DEFAULT_PROTOCOL); 中的这一行postProcessContext()覆盖代码中的方法。
它适用于所有HTTP请求方法,如 POST,PUT,DELETE,GET 等。

Add collection.addMethod(DEFAULT_PROTOCOL); this line in postProcessContext() override method from code. It working properly with all HTTP request methods like POST,PUT,DELETE,GET etc..

这篇关于Spring Boot“请求方法'GET'不受支持”同时通过Catalina Connector将POST请求重定向到https端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆