如何设置 Spring Boot 以运行 HTTPS/HTTP 端口 [英] How set up Spring Boot to run HTTPS / HTTP ports

查看:53
本文介绍了如何设置 Spring Boot 以运行 HTTPS/HTTP 端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Boot 有一些属性来配置 web 端口和 SSL 设置,但是一旦设置了 SSL 证书,http 端口就会变成 https 端口.

Spring boot have some properties to config web port and SSL settings, but once a SSL certificate is set the http port turns into https port.

那么,如何让两个端口同时运行,例如:80 和 443?

So, how can I keep both ports running on it, for example: 80 an 443 at the same time?

如您所见,只有一个端口的属性,在这种情况下启用了server.ssl",是什么使 http 端口自动禁用.

As you can see, there only properties for one port, in this case "server.ssl" is enabled, what makes http port be disabled automatically.

##############
### Server ###
##############
server.port=9043
server.session-timeout=1800
server.ssl.key-store=file:///C:/Temp/config/localhost.jks
server.ssl.key-store-password=localhost
server.ssl.key-password=localhost
server.ssl.trust-store=file:///C:/Temp/config/localhost.jks
server.ssl.trust-store-password=localhost

我什至尝试使用 Tomcat 或 Undertow.我将不胜感激!

I am trying to use even Tomcat or Undertow. I'd appreciate any help!

推荐答案

Spring Boot 配置使用属性,只允许配置一个连接器.您需要的是多个连接器,为此,您必须编写一个 Configuration 类.按照

Spring Boot configuration using properties, allows configuring only one connector. What you need is multiple connectors and for this, you have to write a Configuration class. Follow instructions in

https://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/html/howto-embedded-servlet-containers.html

您可以在下面找到通过属性配置 HTTPS 然后通过 EmbeddedServletContainerCustomizer 配置 HTTP 的工作示例

You can find a working example of configuring HTTPS through properties and then HTTP though EmbeddedServletContainerCustomizer below

http://izeye.blogspot.com/2015/01/configure-http-and-https-in-spring-boot.html?showComment=1461632100718#c4988529876932015554

server:
  port: 8080
  ssl:
    enabled: true
    keyStoreType: PKCS12
    key-store: /path/to/keystore.p12
    key-store-password: password
  http:
    port: 8079


@Configuration
public class TomcatConfig {

@Value("${server.http.port}")
private int httpPort;

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
    return new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            if (container instanceof TomcatEmbeddedServletContainerFactory) {
                TomcatEmbeddedServletContainerFactory containerFactory =
                        (TomcatEmbeddedServletContainerFactory) container;

                Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
                connector.setPort(httpPort);
                containerFactory.addAdditionalTomcatConnectors(connector);
            }
        }
    };
}
}

这篇关于如何设置 Spring Boot 以运行 HTTPS/HTTP 端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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