“No subject alternative names present” 异常解决

前段时间尝试在自己本机访问https 资源,一直困于“No subject alternative names present”这个错误。

Exception in thread "main" javax.xml.ws.WebServiceException: Failed to access the WSDL at: https://IP:8443/security/2.0?wsdl. It failed with:

java.security.cert.CertificateException: No subject alternative names present.

      at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(RuntimeWSDLParser.java:151)

      at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:133)

      at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:254)

      at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:217)

      at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:165)

      at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:93)

      at javax.xml.ws.Service.<init>(Service.java:56)

因为之前项目吃紧,只好在公司已经配置好的平台上开发,把这个问题搁置了下来。随着第一个版本的成功发布,得了些空闲时间再次来研究这个问题。

从这篇文章中得到启示:http://www.coderanch.com/t/523511/sockets/java/Certificate-No-subject-alternative-names

I read that the problem usually appears when the "cn" in the certificate does not match the address of the server. 
The "CN" in my certificate is "PRESS44" 
The server address that was given to me is an ip. 
https://xxx.xxx.xxx.xxx:123

难道不能用IP地址,必须要用主机名?先查查服务器端的配置。找到服务器端的keystore 文件

/usr/java/jdk1.6.0_15/bin/keytool -list -v -keystore -list -v -keystore server.keystore

一查果然CN用的是主机名,复制过来替换原来URL的IP地址。先在测试的Java类里跑跑看,“No subject” 问题是过了,又报如下的错:

Exception in thread "main" javax.xml.ws.WebServiceException: Failed to access the WSDL at: https://hostname:8443/security/2.0?wsdl. It failed with:

      Server returned HTTP response code: 401 for URL: https://hostname:8443/security/2.0?wsdl.

      at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(RuntimeWSDLParser.java:151)

      at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:133)

      at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:254)

      at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:217)

      at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:165)

      at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:93)

      at javax.xml.ws.Service.<init>(Service.java:56)

访问WSDL也需要安全认证,真是麻烦啊!突然想起前段时间做调研时,JBoss里似乎可以设置一个叫 ignoreHttpsHost 的参数。反正最后也要部署到JBoss,不妨就试试看,设置好三个参数:

System.setProperty("javax.net.ssl.trustStore", "C:\\test\\jssecacerts"); 
System.setProperty("javax.net.ssl.trustStorePassword", "changeit"); 
System.setProperty("org.jboss.security.ignoreHttpsHost", "true");

启动JBoss,跑SoapUI测试用例,三秒之后,成功返回结果。

CertificateException: No subject alternative names present是一个SSL握手过程中可能出现的异常。它表示在证书中没有找到主体备用名称(Subject Alternative Names)。主体备用名称是用于指定与证书关联的主体的其他名称,例如域名或IP地址。 这个异常通常发生在以下情况下: 1. 证书中没有包含主体备用名称。 2. 证书中的主体备用名称与请求的主机名不匹配。 要解决这个问题,可以尝试以下方法: 1. 确保证书中包含了正确的主体备用名称。可以使用openssl命令检查证书的主体备用名称: ```shell openssl x509 -in <证书文件> -text -noout ``` 在输出中查找"Subject Alternative Name"字段,确保其中包含了正确的主体备用名称。 2. 如果证书中没有包含主体备用名称,可以尝试使用通配符证书或使用IP地址作为主体备用名称。 3. 如果证书中的主体备用名称与请求的主机名不匹配,可以尝试使用正确的主机名进行请求,或者更新证书以包含正确的主体备用名称。 4. 如果是在开发或测试环境中遇到这个问题,可以尝试忽略主体备用名称的验证。在Java中,可以通过设置SSLParameters的setEndpointIdentificationAlgorithm方法来实现: ```java SSLParameters sslParams = new SSLParameters(); sslParams.setEndpointIdentificationAlgorithm(""); SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(host, port); sslSocket.setSSLParameters(sslParams); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值