java - 如何避免在 Spring Boot 应用程序中写入显式端口号 (8443)

标签 java spring spring-mvc ssl spring-boot

我有一个在 DigitalOcean Droplet 上运行的 Spring Boot 应用程序 (arbejdsdag.dk)。我已经成功安装了 LetsEncrypt SSL 证书,它也能正常工作。

但是,除非我在 URL 后明确添加 SSL 端口,否则我无法访问该站点。

所以: arbejdsdag.dk:8443 工作正常 arbejdsdag.dk 返回 404(未找到)错误

我还有一个从 HTTP 到 HTTPS 的重定向。

  @Bean
  @Profile("PROD") 
  public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected void postProcessContext(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;
  }


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

    return connector;
  }

在应用程序属性中将端口设置为 8443 作为 pr。 Spring Boot 文档。

server.port=8443

我希望只需键入 URL 即可访问站点,而无需显式添加端口号。我错过了什么?

最佳答案

端口必须是 443 浏览器默认使用 https 没有端口号。改变

server.port=8443

server.port=443

但请注意,您必须以特权用户身份运行应用程序才能在基于 *NIX 的系统上打开低于 1000 的端口。

关于java - 如何避免在 Spring Boot 应用程序中写入显式端口号 (8443),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43889452/

相关文章:

java - 使用递归而不是循环打印嵌套数组中的元素

spring saml - IDP 发起的 SSO

java - @Async 将 bean 范围从一个线程复制到另一个线程

java - ApplicationContextAware 在 Spring 3.0 中不起作用

java - 无法将 HttpURLConnection 方法切换为 POST

java - 在 JAVA 对象中存储和获取值

java - 我所有 Controller 单元测试的 Spring 3.2.5 错误 "java.lang.AssertionError: No ModelAndView found"

spring - 为什么 beans 会被拒绝以及未识别出 URL 路径意味着什么?

java - 无法打开调试器端口 : java.net.ConnectException "Connection refused"

java - 使用类配置的 Spring MVC jsp 页面映射