前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring cloud/spring boot同时支持http和https访问

spring cloud/spring boot同时支持http和https访问

作者头像
lyb-geek
发布2018-12-20 16:54:11
1.1K0
发布2018-12-20 16:54:11
举报
文章被收录于专栏:Linyb极客之路Linyb极客之路

前言

       关于spring boot同时支持http和https访问,在spring boot官网73.9已经有说明文档了,同样在github上也有官网的例子。官网链接如下

https://github.com/spring-projects/spring-boot/tree/v1.5.9.RELEASE/spring-boot-samples/spring-boot-sample-tomcat-multi-connectors

  在这里,我向大家讲述一下,我是怎么实现的。

方式一

    一、相关配置

代码语言:javascript
复制
server:
  port: 4000


https:
  port: 8443
  ssl:
    key-store: classpath:sample.jks
    key-store-password: secret
    key-password: password

   可以看到,只是简简单单添加端口的信息,sample.jks可以自己生成(记得对应密码),也可以在官网例子里面下载。

https://raw.githubusercontent.com/spring-projects/spring-boot/v1.5.9.RELEASE/spring-boot-samples/spring-boot-sample-tomcat-multi-connectors/src/main/resources/sample.jks

二、spring boot启动文件读取配置信息(注:请添加必要的jar)

代码语言:javascript
复制
@SpringBootApplication
public class GatewayApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(GatewayApplication.class, args);
    }

    @Value("${https.port}")
    private Integer port;

    @Value("${https.ssl.key-store-password}")
    private String key_store_password;

    @Value("${https.ssl.key-password}")
    private String key_password;
  
  /* --------------------请按照自己spring boot版本选择 start--------------------- */

  // 这是spring boot 1.5.X以下版本的 添加了这个,下一个就不用添加了
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
        tomcat.addAdditionalTomcatConnectors(createSslConnector()); // 添加http
        return tomcat;
    }
  
  // 这是spring boot 2.0.X版本的 添加这个,上一个就不用添加了
  @Bean
  public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createSslConnector()); // 添加http
    return tomcat;
  }

  /* -------------------请按照自己spring boot版本选择 end---------------------- */


  // 配置https
    private Connector createSslConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
        try {
            File keystore = new ClassPathResource("sample.jks").getFile();
            /*File truststore = new ClassPathResource("sample.jks").getFile();*/
            connector.setScheme("https");
            connector.setSecure(true);
            connector.setPort(port);
            protocol.setSSLEnabled(true);
            protocol.setKeystoreFile(keystore.getAbsolutePath());
            protocol.setKeystorePass(key_store_password);
            protocol.setKeyPass(key_password);
            return connector;
        }
        catch (IOException ex) {
            throw new IllegalStateException("can't access keystore: [" + "keystore"
                    + "] or truststore: [" + "keystore" + "]", ex);
        }
    }
}

启动项目后,可以看到两个端口,说明已经成功

方式二

 一、相关配置

代码语言:javascript
复制
server:
  port: 8443
  ssl:
    key-store: classpath:sample.jks
    key-store-password: secret
    key-password: password

http:
  port: 8080

 二、spring boot启动文件读取配置信息(注:请添加必要的jar)

代码语言:javascript
复制
@SpringBootApplication
public class SampleTomcatTwoConnectorsApplication {

    @Value("${http.port}")
    private Integer port;


  /* --------------------请按照自己spring boot版本选择 start--------------------- */
  
  // 这是spring boot 1.5.X以下版本的 添加了这个,下一个就不用添加了
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
        tomcat.addAdditionalTomcatConnectors(createStandardConnector()); // 添加http
        return tomcat;
    }
  
  // 这是spring boot 2.0.X版本的 添加这个,上一个就不用添加了
  @Bean
  public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createStandardConnector()); // 添加http
    return tomcat;
  }


/* --------------------请按照自己spring boot版本选择 end--------------------- */


  // 配置http
    private Connector createStandardConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(port);
        return connector;
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleTomcatTwoConnectorsApplication.class, args);
    }

}

启动项目后,可以看到两个端口,说明已经成功

总结

 对比两种方法可以看出方式二比方式一简单一点,主要是因为方式二用的代码比较少,我也不知道这两种方式有什么区别,我自己测试过,无论是spring boot还是spring cloud,这两个方式都没问题,就算是方式二,同样可以帮服务注册到eureka上。不一样的是方式一注册到eureka的端口是4000,方式二注册到eureka的端口是8443。作为一个强迫的人士,在我自己的项目上,用的方式二,因为我的eureka用的http注册服务。如果你只是spring boot,当然选择少一点代码的方式二啦。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-11-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Linyb极客之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档