Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 本地主机上的SSL证书不工作_Java_Ssl_Hybris - Fatal编程技术网

Java 本地主机上的SSL证书不工作

Java 本地主机上的SSL证书不工作,java,ssl,hybris,Java,Ssl,Hybris,在通过java代码访问https URL时,我遇到了一个异常。 当我尝试访问时,出现以下异常: Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching localhost found at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at s

在通过java代码访问https URL时,我遇到了一个异常。 当我尝试访问时,出现以下异常:

Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching localhost found
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
        at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
        at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
        at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
        at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)
        at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
        at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
        at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
        at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
        at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:81)
        at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
        at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:569)
        ... 131 more
Caused by: java.security.cert.CertificateException: No name matching localhost found
        at sun.security.util.HostnameChecker.matchDNS(HostnameChecker.java:221)
        at sun.security.util.HostnameChecker.match(HostnameChecker.java:95)
        at sun.security.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:455)
        at sun.security.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:436)
        at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:200)
        at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
        at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)
我尝试使用以下命令生成SSL证书:

./jdk1.8/bin/keytool -genkey -keystore /srv/jakarta/.keystore -alias ALIAS \
    -keyalg RSA -keysize 4096 -validity 720
Enter keystore password: # well, enter something
Re-enter new password: # same as above
What is your first and last name?
  [Unknown]:  localhost # !!! IMPORTANT this is the domain name, NOT YOUR name
What is the name of your organizational unit?
  [Unknown]:  # enter something or leave empty
What is the name of your organization?
  [Unknown]:  # enter something or leave empty
What is the name of your City or Locality?
  [Unknown]:  # enter something or leave empty
What is the name of your State or Province?
  [Unknown]:  # enter something or leave empty
What is the two-letter country code for this unit?
  [Unknown]:  # enter something or leave empty
Is CN=example com, OU=Foo, O=Bar, L=City, ST=AA, C=FB correct?
  [no]:  yes
Enter key password for <lea-prod>
    (RETURN if same as keystore password): # Press RETURN
/jdk1.8/bin/keytool-genkey-keystore/srv/jakarta/.keystore-alias\
-keyalg RSA-密钥大小4096-有效期720
输入密钥库密码:#好吧,输入一些东西
重新输入新密码:#同上
你的名字和姓氏是什么?
[未知]:本地主机#!!!重要信息这是域名,不是你的名字
您的组织单位名称是什么?
[未知]:#输入内容或留空
你的组织名称是什么?
[未知]:#输入内容或留空
你所在的城市或地区叫什么名字?
[未知]:#输入内容或留空
你所在的州或省叫什么名字?
[未知]:#输入内容或留空
这个单位的两个字母的国家代码是什么?
[未知]:#输入内容或留空
CN=com、OU=Foo、O=Bar、L=City、ST=AA、C=FB是否正确?
[否]:是的
输入的密钥密码
(如果与密钥库密码相同,则返回):#按返回
并使用以下tomcat/server.xml配置:

 <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
       maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
       clientAuth="false" sslProtocol="TLS"
       keyAlias="ALIAS" keystoreFile="/srv/jakarta/.keystore"
       keystorePass="PW from step 1" />

然而,它仍然不起作用,我面临着同样的错误

我有办法解决这个问题吗?
谢谢

嗯,这正是错误消息所显示的。您的证书是在通用名
example.com
上颁发的。但是,您在地址栏中输入了
localhost
作为服务器名称。这两个显然不匹配。您还应将您的证书通用名(CN)更改为
localhost
,也就是说,重新生成证书时应使用正确的名称
localhost


在下一步中,您需要将该证书导入浏览器的证书存储。但这是另一个话题。更正证书的公用名后,异常应消失。

我相信您正在尝试访问基于HTTPS协议的服务。在这种情况下,您需要获得网站/服务的证书,并将其添加到java密钥库中。这是在生产代码中使用证书的方式


或者,您可以覆盖HostNameVerifier类以抑制任何错误,并在java SSL上下文中设置它以忽略任何错误。理想情况下,当SSL证书不可用时,用户可以禁止签入测试代码

您是否使用localhost访问URL?如果是,您的证书名是localhost吗?然后您是否已将SSL证书添加到受信任?是的,我可以直接在localhost上访问URL,并且证书名称为localhost。我创建了一个本地密钥库,而不是将其添加到windows密钥库中。这让它值得信任吗?我对此一无所知。假设您是从Java程序访问url,那么您需要将证书作为受信任的证书添加到密钥库中。这是我的错误@Bjorn Zurmaar。我将其命名为localhost only,并将其添加到本地生成的密钥库中,而不是windows默认密钥库中。但我没有访问浏览器上的URL。我是通过代码访问这些内容的(通过浏览器或执行任何其他代码)访问URL的方式不起作用。如果存在SSL握手,则证书的通用名称和主机名必须匹配。如果他们没有,证书必须被拒绝。否则,你可以有一个有效的证书,并把它放在任何你想让它看起来合法的服务器上。我希望使用https访问的URL很少,但仍然会出现错误:sun.security.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:455)。在这种情况下,请尝试超越SSLContext并设置虚拟信任库