Java SpringBoot 2.0使用http和https,但在https端口上将http重定向到https

Java SpringBoot 2.0使用http和https,但在https端口上将http重定向到https,java,spring-boot,https,ssl-certificate,keystore,Java,Spring Boot,Https,Ssl Certificate,Keystore,我有一个使用http和https的SpringBoot 2.0应用程序。因此,在端口9080上,它为http协议提供服务,在端口9443上,它可以正常工作。我唯一想要的是重定向,如果用户键入,例如: 总而言之: >>工作正常 >>工作正常 >>带来错误错误请求。主机和端口的这种组合需要TLS。但应重定向到 我的application.properties是 server.port=9443 http.port=9080 server.ssl.enabled=true server.ssl.key

我有一个使用http和https的SpringBoot 2.0应用程序。因此,在端口9080上,它为http协议提供服务,在端口9443上,它可以正常工作。我唯一想要的是重定向,如果用户键入,例如:

总而言之:

>>工作正常

>>工作正常

>>带来错误
错误请求。主机和端口的这种组合需要TLS。
但应重定向到

我的application.properties是

server.port=9443
http.port=9080
server.ssl.enabled=true
server.ssl.key-store-type=PKCS12
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=my_passowrd
server.ssl.key-alias=my_alias

也许有人有办法解决这个问题。谢谢,祝您愉快:-)

如果您使用的是嵌入式Tomcat,您可以修改返回的消息以包含重定向头

例如,对于Tomcat 9.0.x:

TLSClientHelloExtractor.USE_TLS_RESPONSE = ("HTTP/1.1 302 \r\n"
    + "Content-Type: text/plain;charset=UTF-8\r\n" + "Location: https://" + hostnamePort + "\r\n"
    + "Connection: close\r\n" + "\r\n" + "Bad Request\r\n"
    + "This combination of host and port requires TLS.\r\n"
    + "Please access this URL with HTTPS scheme.\r\n").getBytes(StandardCharsets.UTF_8);

请注意,这是一个静态值,只能重定向到一个URL,除非您提供自己的安全通道实现。

您可以通过使用TomcateMbeddedServletContainerFactory对上下文进行后处理来做到这一点。我使用SpringBoot 2.1.1,而该类在SpringBoot>2.0中不可用。是true。使用TomcatServletWebServerFactory。我正在写可能重复的代码Hm,我已经试过了,但是可能重复中提到的解决方案总是重定向到https。我只想重定向,以防用户意外地将http协议与https端口一起使用。如果使用http协议和端口,服务器应通过http进行响应。但是谢谢你的链接,如果成功的话,我会试着让它运行并在这里发布答案!
TLSClientHelloExtractor.USE_TLS_RESPONSE = ("HTTP/1.1 302 \r\n"
    + "Content-Type: text/plain;charset=UTF-8\r\n" + "Location: https://" + hostnamePort + "\r\n"
    + "Connection: close\r\n" + "\r\n" + "Bad Request\r\n"
    + "This combination of host and port requires TLS.\r\n"
    + "Please access this URL with HTTPS scheme.\r\n").getBytes(StandardCharsets.UTF_8);