|
WebLogic Security Service 提供查找和验证 X509 证书链的证书查找和验证(Certificate Lookup and Validation,简称 CLV)API。
证书路径是一种可以将证书链存储到内存的 JDK 类。术语“证书路径”也指用于定位和验证证书链的 JDK 架构和框架。CLV 框架可扩展和完善了 JDK 证书路径功能。证书路径提供程序依赖于 WebLogic 和 JDK 接口的紧密集成。
应用程序代码可以使用 WebLogic Server 提供的默认证书路径提供程序,以生成和验证证书链,或者使用任何自定义证书路径提供程序。
CertPathSelector 接口 (weblogic.security.pk.CertPathSelector) 包含用于定位和验证证书路径的选择条件。因为有许多方法可以查找证书路径,所以为每种类型的选择条件都实现派生类。
每个选择器类都有一个或者多个用于检索选择数据的方法,以及一个构造方法。
weblogic.security.pk中实现 CertPathSelector 接口的类(一个类对应一种受支持类型的证书链查找)如下所示:
| 注意: | 选择器是否受支持取决于已配置的证书路径提供程序。已配置的证书路径提供程序则由管理员决定。 WebLogic 证书路径提供程序仅使用 EndCertificateSelector 选择器。 |
清单 10-1 显示的是一个对选择器进行选择的示例。
// 您已经拥有结束证书
// 并且还希望用它查找和
// 验证相应的证书链
X509Certificate endCertificate = ...
// 生成证书链选择器
CertPathSelector selector = new EndCertificateSelector(endCertificate);
将 CertPathBuilderParameters的实例作为 CertPathParameters对象传递到 JDK 的 CertPathBuilder.build()方法。
public CertPathBuilderParameters(String realmName,
CertPathSelector selector,
X509Certificate[] trustedCAs,
ContextHandler context)
selector。使用 weblogic.security.pk.CertPathSelector接口派生类之一(如实例化 CertPathSelector中所述)指定用于定位和验证证书路径的选择条件。
清单 10-2显示的是一个传递CertPathBuilderParameter 的实例的示例。
// 生成证书链选择器
CertPathSelector selector = new EndCertificateSelector(endCertificate);
String realm = _;
// 根据需要创建并填充上下文处理程序,或为空
ContextHandler context = _;
// 根据需要传入可信 CA 列表,或为空
X509Certificate[] trustedCAs = _;
// 生成参数
CertPathBuilderParams params =
new CertPathBuilderParameters(realm, selector, context, trustedCAs);
java.security.cert.CertPathBuilder 接口是 CertPathBuilder类的 API。要使用 JDK CertPathBuilder 接口,请执行下列操作:
CertPathBuilder.getInstance方法,以检索 CLV 框架的 CertPathBuilder。必须将“WLSCertPathBuilder”指定为传递到调用的算法名称。weblogic.security.pk.CertPathBuilderParameters的实例作为 CertPathParameters 对象传递到 JDK 的 CertPathBuilder.build() 方法,如实例化 CertPathBuilderParameters中所述。import weblogic.security.pk.CertPathBuilderParameters;
import weblogic.security.pk.CertPathSelector;
import weblogic.security.pk.EndCertificateSelector;
import weblogic.security.service.ContextHandler;
import java.security.cert.CertPath;
import java.security.cert.CertPathBuilder;
import java.security.cert.X509Certificate;
// 您已经拥有结束证书
// 并且还希望用它查找和
// 验证相应的证书链
X509Certificate endCertificate = ...
// 生成证书链选择器
CertPathSelector selector = new EndCertificateSelector(endCertificate);
String realm = _;
// 根据需要创建并填充上下文处理程序
ContextHandler context = _;
// 根据需要传入可信 CA 列表
X509Certificate[] trustedCAs = _;
// 生成参数
CertPathBuilderParams params =
new CertPathBuilderParameters(realm, selector, context, trustedCAs);
// 获取 WLS CertPathBuilder
CertPathBuilder builder =
CertPathBuilder.getInstance("WLSCertPathBuilder");
// 用它来查找和验证证书链
CertPath certpath = builder.build(params).getCertPath();
X509Certificate[] chain =
certpath.getCertificates().toArray(new X509Certificate[0]);
将 CertPathValidatorParameters的实例作为 CertPathParameters对象传递到 JDK 的 CertPathValidator.validate()方法。
public CertPathValidatorParameters(String realmName,
X509Certificate[] trustedCAs,
ContextHandler context)
清单 10-4显示的是一个传递CertPathValidatorParameters 的实例的示例。
// 获取 WLS CertPathValidator
CertPathValidator validator =
CertPathValidator.getInstance("WLSCertPathValidator");
String realm = _;
// 根据需要创建并填充上下文处理程序,或为空
ContextHandler context = _;
// 根据需要传入可信 CA 列表,或为空
X509Certificate[] trustedCAs = _;
// 生成参数(为默认安全领域)
CertPathValidatorParams params =
new CertPathValidatorParams(realm, context, trustedCAs);
java.security.cert.CertPathValidator 接口是 CertPathValidator类的 API。要使用 JDK CertPathValidator 接口,请执行下列操作:
CertPathValidator.getInstance 方法,以检索 CLV 框架的 CertPathValidator。必须将“WLSCertPathValidator”指定为传递到调用的算法名称。weblogic.security.pk.CertPathValidatorParameters的实例作为 CertPathParameters 对象传递到 JDK 的 CertPathValidator.validate() 方法,如实例化 CertPathValidatorParameters中所述。CertPathValidator.validate() 方法将引发 InvalidAlgorithmParameterException。import weblogic.security.pk.CertPathValidatorParams;
import weblogic.security.service.ContextHandler;
import java.security.cert.CertPath;
import java.security.cert.CertPathValidator;
import java.security.cert.X509Certificate;
// 您已经拥有未经验证的 X509 证书链
// 并且希望对其进行验证
X509Certificate[] chain = ...
// 将证书链转换为 CertPath
CertPathFactory factory = CertPathFactory.getInstance("X509");
ArrayList list = new ArrayList(chain.length);
for (int i = 0; i < chain.length; i++) {
list.add(chain[i]);
}CertPath certPath = factory.generateCertPath(list);
// 获取 WLS CertPathValidator
CertPathValidator validator =
CertPathValidator.getInstance("WLSCertPathValidator");
String realm = _;
// 根据需要创建并填充上下文处理程序,或为空
ContextHandler context = _;
// 根据需要传入可信 CA 列表,或为空
X509Certificate[] trustedCAs = _;
// 生成参数(为默认安全领域)
CertPathValidatorParams params =
new CertPathValidatorParams(realm, context, trustedCAs);
// 用它来验证证书链
validator.validate(certPath, params);
|