在java 5中验证证书?

时间:2010-04-26 09:46:23

标签: java security ocsp

我正在寻找java 5中客户端证书的OCSP验证示例。另外,为了这个目的,java.security文件中的配置如何使用

1 个答案:

答案 0 :(得分:0)

static {
    Security.setProperty("ocsp.enable", "true");
}

public boolean validate(X509Certificate certificate, CertPath certPath,
        PKIXParameters parameters) throws GeneralSecurityException {
    try {
        CertPathValidator cpv = CertPathValidator.getInstance("PKIX");
        PKIXCertPathValidatorResult result = (PKIXCertPathValidatorResult) cpv
                .validate(certPath, parameters);
        Signature.LOG.debug("Validation result is: " + result);
        return true; // if no exception is thrown
    } catch (CertPathValidatorException cpve) {

        // if the exception is (or is caused by)
        // CertificateRevokedException, return false;
        // otherwise re-throw, because this indicates a failure to perform
        // the validation
        Throwable cause = ExceptionUtils.getRootCause(cpve);
        Class<? extends Throwable> exceptionClass = cause != null ? cause.getClass()
                : cpve.getClass();
        if (exceptionClass.getSimpleName().equals("CertificateRevokedException")) {
            return false;
        }
        throw cpve;
    }
}