📄 e231. validating a certification path.txt
字号:
This example validates a chain of certificates using the most-trusted CAs in the JDK's cacerts file.
try {
// Load the JDK's cacerts keystore file
String filename = System.getProperty("java.home")
+ "/lib/security/cacerts".replace('/', File.separatorChar);
FileInputStream is = new FileInputStream(filename);
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
String password = "changeit";
keystore.load(is, password.toCharArray());
// Create the parameters for the validator
PKIXParameters params = new PKIXParameters(keystore);
// Disable CRL checking since we are not supplying any CRLs
params.setRevocationEnabled(false);
// Create the validator and validate the path
// To create a path, see e229 Creating a Certification Path
CertPathValidator certPathValidator
= CertPathValidator.getInstance(CertPathValidator.getDefaultType());
CertPathValidatorResult result = certPathValidator.validate(certPath, params);
// Get the CA used to validate this path
PKIXCertPathValidatorResult pkixResult = (PKIXCertPathValidatorResult)result;
TrustAnchor ta = pkixResult.getTrustAnchor();
X509Certificate cert = ta.getTrustedCert();
} catch (CertificateException e) {
} catch (KeyStoreException e) {
} catch (NoSuchAlgorithmException e) {
} catch (InvalidAlgorithmParameterException e) {
} catch (CertPathValidatorException e) {
// Validation failed
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -