• SpringBoot Web Https 配置


    不管是游戏服务器开发,还是其它服务开发,越来越多的平台都要求服务端必须支持https的访问。以增加安全性。比如目前火热的小程序,要求服务端必须支持https,苹果商店也有说http请求要修改为https。所以https将会是游戏服务器的普遍需求。

    一,证书生成

       证书可以自己使用jdk生成进行测试。但是在正常使用的时候,需要去第三方机构购买,网上也有免费的。不过有效期有限制。具体获取证书的方法这里不再详细说明了。一般拿到证书之后会得到这几个文件:

       cert.pem chain.pem   fullchain.pem  privkey.pem

    二,将pem文件转化为keystore文件

    如果使用nginx跳转的话,上面的证书文件可以直接使用,但是在tomcat中,证书的配置文件格式必须是.keystore的文件。所以需要做一下转化。

    1、生成pkcs12格式的密钥文件:

    $ openssl pkcs12 -export -in cert.pem -inkey privkey.pem -out my.pk12 -name mykey

    (注:此过程中需要输入密码:123456)

    2、生成keystore:

    $ keytool -importkeystore -deststorepass 123456 -destkeypass 123456 -destkeystore my.keystore -srckeystore my.pk12 -srcstoretype PKCS12 -srcstorepass 123456 -alias shkey

    成功之后会获得my.keystore文件。

    三,在Spring boot web中配置https

    首先是在application.properties中添加配置

    server.port= 8446
    
    server.ssl.key-store=/user/cert/my.keystore
    
    server.ssl.key-store-password=123456
    

      

    这样配置之后,启动服务,就可以https访问了。

    四,同时支持http和https访问

    1,http请求不跳转成https访问

    这种方式是http请求单独走一个端口,https请求单独走一个端口。但是spring boot 的appplication.properties只能配置一个端口,这就需要我们手动再添加一个Connector了。

    @Configuration
    
    public class TomcatConfig {
    
    @Bean
    
    public EmbeddedServletContainerFactory servletContainerFactory(){
    
    TomcatEmbeddedServletContainerFactory tomcatConfig = new TomcatEmbeddedServletContainerFactory();
    
    tomcatConfig.addAdditionalTomcatConnectors(this.newHttpConnector());
    
    return tomcatConfig;
    
    }
    
    private Connector newHttpConnector() {
    
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    
    connector.setScheme("http");
    
    connector.setPort(8080);
    
    connector.setSecure(false);
    
    return connector;
    
    }
    
    }
    

      

    这样普通 的http请求,可以访问8080端口了。

    2,将http请求强制跳转到https

    有时候我们的一些旧业务是使用的http,但是新业务以及将来的框架都必须强制使用https,那就需要做一下跳转,把收到的http请求强制跳转到https上面。

    @Configuration
    
    public class TomcatConfig {
    
    @Bean
    
    public EmbeddedServletContainerFactory servletContainerFactory(){
    
    TomcatEmbeddedServletContainerFactory tomcatConfig = new TomcatEmbeddedServletContainerFactory(){
    
    @Override
    
    protected void postProcessContext(Context context) {
    
    SecurityConstraint securityConstraint = new SecurityConstraint();
    
    securityConstraint.setUserConstraint("CONFIDENTIAL");
    
    SecurityCollection collection = new SecurityCollection();
    
    // 这里不知道为什么,只能配置以/*结尾的path。这样配置表示全部请求使用安全模式,必须走https
    
    collection.addPattern("/*");
    
    //另外还可以配置哪些请求必须走https,这表示以/home/开头的请求必须走https
    
    collection.addPattern("/home/*");
    
    securityConstraint.addCollection(collection);
    
    context.addConstraint(securityConstraint);
    
    }
    
    };
    
    tomcatConfig.addAdditionalTomcatConnectors(this.newHttpConnector());
    
    return tomcatConfig;
    
    }
    
    private Connector newHttpConnector() {
    
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    
    connector.setScheme("http");
    
    connector.setPort(8080);
    
    connector.setSecure(false);
    
    // 如果只需要支持https访问,这里把收到的http请求跳转到https的端口
    
    connector.setRedirectPort(8446);
    
    return connector;
    
    }
    
    }
    

     

    以上跳转也可以使用nginx实现。如果有什么问题可以评论留言或加QQ群:66728073交流

  • 相关阅读:
    Redis 错误摘记篇
    搭建备份到业务迁移---mysql
    业务迁移---web
    业务迁移---redis
    redis 编译安装错误问题
    location 匹配规则 (NGINX)
    nginx虚拟目录实现两个后台使用
    零基础学python-19.10 生成器是单迭代器
    零基础学python-19.9 生成器函数与生成器表达式
    零基础学python-19.8 生成器表达式:当迭代器遇上列表解析
  • 原文地址:https://www.cnblogs.com/wgslucky/p/9092128.html
Copyright © 2020-2023  润新知