https双向认证

作者: 青峰星宇 | 来源:发表于2018-10-18 19:31 被阅读19次

    一、运行环境

    win7_64
    studio3.1.3
    IDEA2017.3
    JDK1.8
    Sprint Boot 2.0.5
    Struct2

    二、证书生成

    1. 生成私钥和证书

    keytool -genkeypair -alias serverkey -keyalg RSA -keysize 2048 -validity 3650 -keystore tomatocc.keystore
    

    2. 查看keystore详情

    keytool -v -list -keystore tomatocc.keystore
    

    3、证书导入导出

    3.1)我们将我们生成的密钥信息导出为.cer格式的证书。我们执行下面命令,输入密钥库口令即可。
    keytool -exportcert -keystore tomatocc.keystore -file tomatocc.cer -alias serverkey
    
    3.2)将证书导入到密钥库,一般为导入信任证书(SSL客户端使用),我们可以执行如下指令,然后输入密钥库口名再回车即可(此证书用于服务器端的双向验证)。
    keytool -importcert -keystore client_trust.keystore -file tomatocc.cer -alias client_trust_server -noprompt
    

    4. 将.cer格式的证书转换为p12证书(服务器端需要验证的客户端证书)

    keytool -importkeystore -srckeystore tomatocc.keystore -destkeystore tomatocc.p12 -srcalias serverkey -destalias serverkey -srcstoretype jks -deststoretype pkcs12 -noprompt
    

    5、生成客户端信任证书库mip_client.truststore( 客户端信任的服务器端证书)

    keytool -import -v -alias serverkey -file tomatocc.cer -keystore tomatocc.truststore -storepass 123456 -storetype BKS -provider org.bouncycastle.jce.provider.BouncyCastleProvider
    

    到此Https客户端和服务端都需要的证书都生成完毕了。准备工作完毕,下面开始上代码了。

    三、服务器端springboot 配置

    1、在application.properties文件中添加如下配置:

    server.http.port=80
    ###单向认证:
    server.port=443
    server.ssl.key-store=classpath:tomatocc.keystore
    server.ssl.key-store-password=123456
    server.ssl.key-password=123456
    server.ssl.key-type=JKS
    server.ssl.keyAlias=serverkey
     ###双向认证:
    server.ssl.client-auth=need
    server.ssl.trust-store=classpath:client_trust.keystore
    server.ssl.trust-store-password=123456
    server.trust.key-type=JKS
    server.trust-store-provider=serverkey
    

    2、https跳转java类和证书存放位置如下图1所示:

    图1

    3、https跳转设置代码如下所示

    package com.example.demo.configs;
    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.beans.factory.annotation.Value;
    import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    @Configuration
    public class HttpsConfiguration {
    // 监听的http请求的端口,需要在application配置中添加http.port=端口号  如80
    @Value("${server.http.port}")
    int serverHttpPort;
    //正常启用的https端口 如443
    @Value("${server.port}")
    Integer httpsPort;
    
    // struct2 写法
    @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;
    }
    
    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        //Connector监听的http的端口号
        connector.setPort(serverHttpPort);
        connector.setSecure(false);
        //监听到http的端口号后转向到的https的端口号
        connector.setRedirectPort(httpsPort);
        return connector;
    }
    }
    

    相关文章

      网友评论

        本文标题:https双向认证

        本文链接:https://www.haomeiwen.com/subject/umiszftx.html