【问题标题】:How to open port 80 (HTTP) and 433 (HTTPS) in Java spring boot?如何在 Java Spring Boot 中打开端口 80(HTTP)和 433(HTTPS)?
【发布时间】:2021-08-27 13:43:09
【问题描述】:

我想为 http 打开端口 80,为 https 打开端口 433。 80端口应该重定向到433。我将application.properties中的服务器端口设置为80端口:

server.port=80

为了重定向,我遵循了官方的 spring 文档并创建了这个小类:

public class HTTPSRedirector {

    @Bean
    SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        http.redirectToHttps();
        return http.build();
    }
}

但是现在服务器超时了,当我尝试通过以下命令使用 curl 时:

curl -v http://www.example.com/

我收到Bad request 响应,因为启用了 SSL/TLS。如果我尝试像这样 ping / 联系 https 端口:

curl -v https://www.example.com/

然后我收到端口未打开且连接被拒绝的消息。如何在我的 Java spring 项目中同时打开 80 和 433 端口?

【问题讨论】:

  • stackoverflow.com/questions/30896234/… 另外,请确保您先在本地进行测试。如果您实际上正在使用甚至路由到网络外部的 FQDN 进行测试,那么请确保流量通过防火墙/路由器转发到您的 springboot 应用程序。如果您在本地运行 springboot 应用程序,请尝试 telnet 到端口 443 并查看它是否已打开(我认为这不是因为您只启用了端口 80,但在您显示的代码中没有 443 的连接器)

标签: java spring http https port


【解决方案1】:

这就是我最终修复它的方式:

我使用了问题中列出的 bean。此 bean 将所有 http 请求重定向到 https。

另外,我将服务器端口设置为:

server.port=443

这是https 的默认端口。通过这两个更改,服务器可以正常工作,并且一切都是 https。

【讨论】:

    猜你喜欢
    • 2018-08-23
    • 2015-09-02
    • 2016-05-12
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 2016-09-27
    相关资源
    最近更新 更多