zoukankan      html  css  js  c++  java
  • SpringBoot配置Https

    HTTPS (全称:Hyper Text Transfer Protocol over SecureSocket Layer),是以安全为目标的 HTTP 通道,在HTTP的基础上通过传输加密和身份认证保证了传输过程的安全性 [1]  。HTTPS 在HTTP 的基础下加入SSL 层,HTTPS 的安全基础是 SSL,因此加密的详细内容就需要 SSL。 HTTPS 存在不同于 HTTP 的默认端口及一个加密/身份验证层(在 HTTP与 TCP 之间)。这个系统提供了身份验证与加密通讯方法。它被广泛用于万维网上安全敏感的通讯,例如交易支付等方面

    生成证书

    使用Java JDK自带生成SSL证书的工具keytool

    生成证命令

    C:UsersAdministrator>keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keysize 2048 -keystore "D:/tomcat.keystore"

    通过此命令运行,则CMD命令窗口会提示输入密钥库口令

    -alias 别名
    -keypass 指定生成密钥的密码
    -keyalg 指定密钥使用的加密算法(如 RSA)
    -keysize 密钥大小
    -validity 过期时间,单位天
    -keystore 指定存储密钥的密钥库的生成路径、名称
    -storepass 指定访问密钥库的密码

    域名证书,可以通过阿里 或 腾讯云 来进行申请

    参考,腾讯云域名证书申请流程:https://cloud.tencent.com/document/product/400/6814

    项目配置证书

    导入证书,把生成的tomcat.keystore放在resources里面

    application.properties 或 application.yml 配置文件中配置相关https内容

    server.port=8443
    
    # 开启https,配置跟证书对应
    server.ssl.enabled=true
    server.ssl.key-store=classpath:tomcat.keystore
    # server.ssl.key-store-type=JKS
    server.ssl.key-store-type=JKS
    # 密码
    server.ssl.key-store-password=123456
    # springboot2.x不需要配置
    server.ssl.key-password=123456
    # 别名
    server.ssl.key-alias=tomcat

    配置http协议跳转https

    package com.dingsheng;
    
    import org.apache.catalina.Context;
    import org.apache.catalina.connector.Connector;
    import org.apache.tomcat.util.descriptor.web.SecurityCollection;
    import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
    import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
    import org.springframework.context.annotation.Bean;
    
    @SpringBootApplication
    public class DingshengApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DingshengApplication.class, args);
        }
    
        // SpringBoot2.x配置HTTPS,并实现HTTP访问自动转向HTTPS
        @Bean
        public ServletWebServerFactory servletContainer() {
            TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(){
                @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(httpConnector());
            return tomcat;
        }
    
        @Bean
        public Connector httpConnector() {
            Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
            connector.setScheme("http");
            connector.setPort(8080); // 监听Http的端口
            connector.setSecure(false);
            connector.setRedirectPort(8443); // 监听Http端口后转向Https端口
            return connector;
        }
    }

    相关其他博主文字,推荐大家可以参考

    https://www.cnblogs.com/huanzi-qch/p/12133872.html

     

  • 相关阅读:
    获取UltraWebTree指定节点的所有父节点内容
    OnClientClick
    ListControl控件经典用法
    DataTable添加列时容易被忽略的问题!
    利用数据库来填充UltraWebTree
    Web导出Word需要添加头文件
    程序中添加动态用户密码
    重构oninit,应用在模式窗口的方法
    防SQL注入
    获得connect string简单方法
  • 原文地址:https://www.cnblogs.com/liuyangjava/p/13488751.html
Copyright © 2011-2022 走看看