springboot配置ssl自签名证书

生成自签证书

每一个JDK或者JRE里都有一个工具,叫做:keytool,安装了jdk或jre之后,配置好JAVA环境之后,就可以直接在控制台使用该命令生成自签名证书:

keytool -genkey -alias xzwkey -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore E:\Desktop\sslkey\keystore.p12 -validity 3650

1.-alias 证书别名

2.-storetype 指定密钥仓库类型

3.-keyalg 生证书的算法名称,RSA是一种非对称加密算法

4.-keysize 证书大小

5.-keystore 生成的证书文件的存储路径和文件名

6.-validity 证书的有效期

springboot配置ssl

server.port=8443
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=123456
server.ssl.key-store-type=PKCS12
server.ssl.key-alias=xzwkey

http自动转向https

    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        //Connector监听的http的端口号
        connector.setPort(80);
        connector.setSecure(false);
        //监听到http的端口号后转向到的https的端口号
        connector.setRedirectPort(443);
        return connector;
    }

    /**
     * http重定向到https
     * @return
     */
    @Bean
    public TomcatServletWebServerFactory servletContainer() {

        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(httpConnector());
        return tomcat;
    }

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×