zoukankan      html  css  js  c++  java
  • 本地Java程序访问HTTPs遇到的问题

    本地测试程序需要访问HTTPs,

    String urlPath = "https://192.168.10.88/xxx/login?username=" + userName + "&password=" + password;
    URL url = new URL(urlPath);
    connection = (HttpURLConnection)url.openConnection();
    connection.connect();

     

     抛出异常:

    javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present
    
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1884)
    
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276)
    
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270)
    
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1341)
    
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:153)
    
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868)
    
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:804)
    
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016)
    
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
    
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
    
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
    
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
    
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
    

     根据

    1. http://www.mkyong.com/webservices/jax-ws/java-security-cert-certificateexception-no-name-matching-localhost-found/
    2. http://bluefoot.info/howtos/how-to-avoid-java-security-cert-certificateexception-no-name-matching-localhost-found/

     

    在程序中添加

    static {
            //for localhost testing only
            javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
            new javax.net.ssl.HostnameVerifier(){
                public boolean verify(String hostname,
                        javax.net.ssl.SSLSession sslSession) {
                    if (hostname.equals("localhost")) {
                        return true;
                    }
                    return false;
                }
            });
        }

     抛出异常

    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
        at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1884)
        at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276)
        at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270)
        at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1341)
        at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:153)
        at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868)
        at sun.security.ssl.Handshaker.process_record(Handshaker.java:804)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
        at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)

     说明需要导入192.168.10.88的证书到本地%JAVA_HOME%/jre/lib/security/cacerts

    1. > keytool -import -file d:httpsserver.crt -keystore "%JAVA_HOME%jrelibsecuritycacerts" -alias 88server
    2. javacacerts证书库的默认密钥库密码是changeit
    3. 再次验证是否成功导入:
    4. > keytool -list -keystore "%JAVA_HOME%jrelibsecuritycacerts" | findstr /i 88server

    一切OK.

  • 相关阅读:
    SpringBoot自动装配源码
    对称加密、非对称加密、数字签名
    k8s部署mysql数据持久化
    docker部署 springboot 多模块项目+vue
    ES入门及安装软件
    prometheus入门介绍及相关组件、原理讲解
    流水线 Sonar 代码扫描
    postgresql数据库 查询表名、备注及字段、长度、是否可控、是否主键等信息
    Helm中Tiller镜像下载失败的解决办法
    程序员孔乙己
  • 原文地址:https://www.cnblogs.com/wangyinhui/p/3992672.html
Copyright © 2011-2022 走看看