例外信息:
javax . net . pkix path building failed : unable to find valid certification path to requested target原因:
此问题通常发生在以下情况下:
Java调用的第三方服务使用Let's Encrypt颁发的证书,JVM不信任Let's Encrypt证书。这是因为本地JDK版本太低,Let's Encrypt证书不在JVM的信任列表中。
解决方法:
方法1(推荐):
本地JDK升级(Java 7=7 u111 and Java 8=8u 101)
方法2:
在代码级别忽略证书(不建议使用,可临时使用以防止安全风险)
方法3:
使用工具类在本地JVM上安装证书,如下所示:
创建In文件,如下所示:
import Java . io . buffered reader;
import Java . io . file;
import Java . io . fileinputstream;
import Java . io . fileoutputstream;
import Java . io . input stream;
import Java . io . inputstreamreader;
import Java . io . output stream;
Import Java。
Import Java。
Import Java。
Import Java。
Import javax.net
Import javax.net
Import javax.net
Import javax.net
Import javax.net
import javax . net . factory;
Import javax.net
Public class InstallCert {
public static void main(string[]args)throws exception {
字串主机;
Int port
char[]pass phrase;
if==1)| | |==2){
String[] c=args[0]。split(' : ');
主机=c[0];
Port====1)?443 3360 in(c[1]);
字串p====1)?变更it ' : args[1];
pass phrase=p . to chararray();
} else {
sy(' usage : Java install cert host[: port][pass phrase]');
Return
}
File File=new File(' jss ecacerts ');
If ()==false) {
Char SEP=File.separatorChar
filedir=new file(' Java . home ')sep ' lib ' sep ' security ');
File=new File(dir,' js secacerts ');
If ()==false) {
File=new File(dir,' cacerts ');
}
}
Sy('Loading KeyStore' file ' . '));
inputstream in=new file inputstream(文件);
KeyStore ks=KeyS();
Ks.load(in,pass phrase);
in . close();
SSL context context=SSL con(' TLS ');
TrustManagerFactory tmf=Tru();
(ks);
x 509 trust manager defaulttrustmanager=(x 509 trust manager)()[0];
savingtrustmanager TM=new savingtrustmanager(defaulttrustmanager);
Con (null,new trust manager [] {TM},null);
Ssl套接字工厂工厂=CON();
sy(' opening connection to ' host ' : ' port ' .));
Ssl套接字套接字=(SSL套接字) (主机、端口);
(10000);
Try {
Sy('Starting SSL handshake . '));
();
();
sy();
Sy ('no errors,certificate is already trusted ');
} catch (SSLException e) {
sy();
e . printstacktrace);
}
x 509 certificate[]chain=;
If (chain==null) {
sy(' could not obtain server certificate chain ');
Return
}
buffered reader reader=new buffered reader(new input stream reader);
sy();
sy(' serversent ' c ' certificate(s): ');
sy();
message digest SHA1=Me(' SHA1 ');
message digest MD5=Me(' MD5 ');
for(int I=0;I c;I) {
x 509 certificate cert=chain[I];
sy(' '(i1)' subject ' cert . getsubjectdn());
sy(' Issuer ' cert . getissuerdn());
());
sy(' sha1 ' to hexstring());
MD5 . update();
sy(' MD5 ' to hexstring());
sy();
}
sy(' enter certificate to add to trusted keystore or ' q ' to quit :[1]);
Stringline=reader.readline()。trim();
int k;
Try {
K=()===0)?0 :英寸(线)-1;
} catch(numberformatexceptione){
sy(' keystore not changed ');
Return
}
x 509 certificate cert=chain[k];
string alias=host '-'(k 1);
Ks。setcertificateentry (alias、cert);
output stream out=new file output stream(' JSSE cacerts ');
Ks.store(out、pass phrase);
out . close();
sy();
市(CERT);
sy();
sy(' added certificate to keystore ' JSSE cacerts ' using alias ' ' alias ');
}
private static finalchar[]hex digits=' 0123456789 ABC def '。tochararay();
private static string to hexstring(byte[]bytes){
string builder sb=new string builder * 3);
For (int b : bytes) {
B=0xff
(hex digits[b 4]);
(hex digits[b 15]);
(' ');
}
return();
}
private static class savingtrustmanager implements x509 trust manager {
Private final X509TrustManager TM:
private x 509 certificate[]chain;
Savingtrustmanager (x509信任管理器TM) {
=TM
}
@Override
public x509 certificate[]getacceptedissuers(){
return new x 509 certificate[0];
//throw new unsupportedoperationexception();
}
public void checkclienttrusted(x 509 certificate[]chain,string authtype)
Throws CertificateException {
throw new unsupportedoperationexception();
}
public void checkservertrusted(x 509 certificate[]chain,string authtype)
Throws CertificateException {
=chain
(陈、Authtype);
}
}
}
编译此文件:
Javac In
运行安装证书:
Java InstallCert www.domain.com
记录如下。
# Java InstallCert www.domain.com
Loading KeyStore /data/a.
opening connection to www . domain.com :443.
Starting SSL handshake.
javax . net . pkix path building failed : unable to find valid certification path to requested target
At (Alerts.java:192)
At (SSL插槽impl.java :1949)
At (Handshaker.java:302)
At (Handshaker.java:296)
at(clienthandshaker . Java :1509)
at(clienthandshaker . Java :216)
At (Handshaker.java:979)
At (Handshaker.java:914)
At (SSL插槽impl.java :1062)
At (SSL插槽impl.java :1375)
At (SSL插槽impl.java :1403)
At (SSL插槽impl.java :1387)
at In(In 336071)
caused by : pkix path building failed : unable to find valid certification path to requested target
At (PKIXValidator.java:387)
At (PKIXValidator.java:292)
at(validator . Java 3360260)
atsun . security . impl . validate(x 509 trustmanagerimpl . Java :324)
atsun . security . impl . check trusted(x 509 trustmanagerimpl . Java :229)
atsun . security . impl . checkservertrusted(x 509 trustmanagerimpl . Java 3360105)
at install cert $ savingtru(in :169)
At (SSLContextImpl.java:922)
at(clienthandshaker . Java :1491)
.8摩尔
caused by : unable to find valid certification path to requested target
at(suncertpathbuilder . Java :141)
at(suncertpathbuilder . Java :126)
At Java。(certpathbuilder.java :280)
At (PKIXValidator.java:382)
.16莫尔
Server sent 2 certificate(s):
1 Subject CN=www.domain.com
is suer cn=let ' s encrypt authority x 3,o=let's encrypt,c=us
sha 1 0 Abe b 4 66 34 61 0 c 12 a 6 ce 41 23 fa a 6 AC 07 4 e 0d 01 c 5
MD5 03 62 53 B6 64 EB E2 C1 94 B1 78 ca 7f ca ba 09
2 subject cn=let ' s encrypt authority x 3,o=let's encrypt,c=us
Issuer cn=dst root ca x3,o=digital signature trust co .
sha 1 e 6 a 3 b 4 5 b 06 2d 50 9 b 33 82 28 2d 19 6 e Fe 97d 5 95 6 c CB
MD 5 b 1 54 09 27 4 f 54 ad 8 f 02 3d 3 b 85 a 5 EC 5d
enter certificate to add to trusted keystore or ' q ' to quit :[1]
[
[
Version: V3
Subject: CN=www.domain.com
signature algorithm : sha 256 with RSA,oid=1.2.840.113549.1.1.11
密钥: sun RSA public key,2048位
modulus : 200730052437287038905886259434398485612135042303461059853939686786974700735349726894253716749732354692843522 26510552
Public exponent: 65537
validity 3360[from : thu may 03 09:57:46 CST 2018、
to : wed aug 01 09:57336046 CST 2018]
is suer : cn=let ' s encrypt authority x 3,o=let's encrypt,c=us
serial number :[0490 ded 4 91 a 4 b 26 f d 82 AAA 5 a BD 06 FFA 4 4804]
Certificate Extensions: 9
[1]: objectid : 1 . 3 . 6 . 1 . 4 . 1 . 11129 . 2 . 4 . 2 criticality=false
扩展unknown : der encoded octet string=
0003360 04 81 F5 04 81 F2 00 F0 00 76 00 55 81 D4 C2 16.v.u .
00103360 90 36 01 4a ea 0b 9b 57 3c 53 F0 c0 E4 38 78 70.6 . j.ws.8xp
00203360 25 08 17 2f a3 aa 1d 07 13 D3 0c 00 00 01 63 23%./...c #
00303360 ef 2b C9 00 00 04 03 00 47 30 45 02 21 00 E3 52.g0e.r
00403360 37 05 ff 9 f 1 16ce 6 fa 4 86 77 df 3d 4d 82 cf 7……。o.w.=m……。
00503360 92 2d bc04 2f 1c B0 DC 4e ff90 C1 D1 7e 02 20./.n……。
0060: 27 e 7 af 9 e 09 14 32d 0 52 BD b 9 1 f fdb 4 8 b 1 c’……。2.r……。
0070: df a2 FD 6c Fe B2 E6 9b3a 45 47 78 6b37 AE a3.l. egxk7……。
0080: 00 76 00 29 3c 51 96 54 c8 39 65 BAAA 50 FC 58 . V .)q . t . 9E.p.x
00903360 07 D4 B7 6f BF 58 7a 29 72 DC a4 C3 0cf 4 E5 45.o.xz) r.e
00a 0: 47 f 4 78 00 001 63 23 ef 2 b a 4 00 00 04 03 00g . x.c #。……。
00b 0: 47 30 45 02 20 0a 09d 1 3 f 17 b 2d 5 21 f 9d 8 9dg 0 e.
00c 0: D1 15 73 4f 81 dc4c 0a cb1 ff 6 34 47 b8 F3 8f.so.l.4g.
00d 0: 21 2d 8 b 25 01 02 21 00 FD 99 44 63 9 c 65 54 a 4!-%.Dc.eT .
00e 0: 74 fc fbf 6 D1 60 66 25 60 df c0 97 59 9b 08 6c t.f% `.y.l
00f0: de13 58 7f6b5a23e8.x.kz #。
[2]: objectid : 1 . 3 . 6 . 1 . 5 . 5 . 7 . 1 . 1 criticality=false
AuthorityInfoAccess [
[
AccessMethod: ocsp
access location : uriname :
而且,
AccessMethod: caIssuers
access location : uriname :
]。
]。
[3]: objectid : 2 . 5 . 29 . 35 criticality=false
AuthorityKeyIdentifier [
密钥标识符[
000: A8 4a 6a 63 04 7d ddba E6 D1 39 B7 a6 45 65 ef . jjc……。9 .ee。
0010: F3 A8 EC A1.
]。
]。
[4]: objectid : 2 . 5 . 29 . 19 criticality=true
BasicConstraints:[
CA:false
PathLen: undefined
]。
[5]: objectid : 2 . 5 . 29 . 32 criticality=false
CertificatePolicies [
[certificatepolicyid :[2 . 23 . 140 . 1 . 2 . 1]
[]]
[certificatepolicyid :[1 . 3 . 6 . 1 . 4 . 1 . 44947 . 1 . 1]
[PolicyQualifierInfo: [
鉴定id : 1.3.6.1.5.5.7.2.1
qualifier : 0003360 161 a68 74 74 70 3a2f 2f 63 70 73 2e 6c 65 74……。
0010: 73 65 6e 63 72 79 70 74 2e 6f 72 67
]、PolicyQualifierInfo: [
鉴定id : 1.3.6.1.5.5.7.2.2
qualifier : 00003360 30 81 9e0c 81 9b 54 68 69 73 20 43 65 72 74 690.this certi
00103360 66 69 63 61 74 65 20 6d 61 79 20 6f 6e 6c 79 20 fica te may only
00203360 62 65 20 72 65 6c 69 65 64 20 75 70 6f 6e 20 62 be relied upon b
00303360 79 20 52 65 6c 79 69 6e 67 20 50 61 72 74 69 65y relying partie
00403360 73 20 61 6e 64 20 6f 6e 6c 79 20 69 6e 20 61 63s and only in AC
00503360 63 6f 72 64 61 6e 63 65 20 77 69 74 68 20 74 68 cor dance with th
00603360 65 20 43 65 72 74 69 69 63 61 74 65 20 50 6f e certificate po
00703360 6c 69 63 79 20 66 f 75 6e 64 20 61 74 20 68 74 licy found at ht
00803360 74 70 733 a 2 f 2 f 6 c 65 74 73 65 6 e 63 72 79 70 TPS ://let sencryp
00903360 74 2e 6f 72 67 2f 72 65 70 6f 73 69 74 6f 72 79t . org/存储库
00A0: 2F/
]]]
]。
[6]: objectid : 2 . 5 . 29 . 37 criticality=false
ExtendedKeyUsages [
ServerAuth
ClientAuth
]。
[7]: objectid : 2 . 5 . 29 . 15 criticality=true
KeyUsage [
DigitalSignature
Key_Encipherment
]。
[8]: objectid : 2 . 5 . 29 . 17 criticality=false
SubjectAlternativeName [
DNSName: www.domain.com
]。
[9]: objectid : 2 . 5 . 29 . 14 criticality=false
SubjectKeyIdentifier [
密钥标识符[
000: 4d 27 88 53 f 8 2 be 5 57 e 3 93 a 2 39 0 c d 0 8 a d 8m’。s.w.9 .
0010: E5 BC 84 A4.
]。
]。
]。
Algorithm: [SHA256withRSA]
Signature:
000: 5d 16 29 6 DC 4 e 8 76 AC 32 cf 44 AE e 4 27 3 b 44].)m. v.2.d.d
00103360 CCA 7 9 a df 95 1 a a c 73 af 2 b 89 51 14 32 2 c 5d.s.q.2,]
0020: 7c 04 9e 29 47 aa ad C4 cf 9e ef db B3 7b a9 09.)g...
00303360 28 8 B85 60 C1 4B67 31 D5 4C 7 AC 6 54 64 C9 90(.`。kg1.lz.td……。
00403360 7c 31 7a a4 ca 59 CD 9b 0b 78 0c c0 00a 4 F6 ef . 1z.y.x……。
00503360 99 af 60 F7 98 3c db F0 8e 08 38 7f 8b 7f c0 9a.`.8 ……。
00603360 180 b 2 c d 5 CD 01 08 EB 2 c ea aa 69 21 c 7 996 e..I!n
0070: 24 4 e 13 41 94 28 7 a c 2d 2 43 e 9 20 22 c 2 35 c 9 $ n . a .(z . c . ' . 5)。
0080: 9d 05 51 25 db 4d D5 19 583 BD 7 1d 8e DBF 2 2e.q % .m.x……。
00903360 78 73 63 35 9 cb2 51 88 00 00 61 1f db79 87 ea xs C5.q.a.y……。
00a 0: af 60 9f 7 03 cab 7 63 f 4 95 c 2d 1 8 f d 0 1 e 2 f.`........./
00b 0: 21 65 d0 cf ef 7a B9 F7 B7 2a BF F2 04 be 78 F4!e.z.*.x.
00c 0: 6f ea 29 89 00 15 E0 8f 72 77 79 8a 9c 6e 2e 98o.)……。rwy.n……。
00d 0: c 9 ee BF 71 18 ab 90 7 a 65 c 8 FD 03 1 c 7 c 0 b 32.q.ze.2
00e 0: dd00 06 D4 67 61 af BF 83 3f F3 D8 9d ee 3b 2e.正式发行.
00 f 03360 e 4 94 bb ad 9 b 48 47 f 3 c 2 CEC 2 36 8 ced 74 3 e.Hg.6 .t
]。
added certificate to keystore ' JSSE cacerts ' using alias ' www . domain.com-1 '
成功运行后,在正在运行的目录中创建名为jssecacerts的文件,并将其复制到JDK安装目录中。
mv JSSE cacerts $ Java _ home/JRE/lib/security/最后重新启动Tomcat。
1.《04f5怎么样2017看这里!JDK版本低导致对Let’s Encrypt证书不信任的问题》援引自互联网,旨在传递更多网络信息知识,仅代表作者本人观点,与本网站无关,侵删请联系页脚下方联系方式。
2.《04f5怎么样2017看这里!JDK版本低导致对Let’s Encrypt证书不信任的问题》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。
3.文章转载时请保留本站内容来源地址,https://www.lu-xu.com/gl/2167620.html