Convert a PEM-formatted String to a java.security.cert.X509Certificate

前端 未结 5 667
再見小時候
再見小時候 2020-12-24 12:57

How does one create a java.security.cert.X509Certificate instance from a PEM-formatted String? The PEM-formatted String is a HTTP request \"SSL_CLIENT_CERT\" h

5条回答
  •  有刺的猬
    2020-12-24 13:27

    I have a similar problem, I'm pasting also here the java code that worked for me in case anyone neaded it :

    import java.util.Base64;
    
    public static X509Certificate parseCertificate(String _headerName, HttpServletRequest _request) throws CertificateException {
        String certStr = _request.getHeader("x-clientcert");
        //before decoding we need to get rod off the prefix and suffix
        byte [] decoded = Base64.getDecoder().decode(certStr.replaceAll(X509Factory.BEGIN_CERT, "").replaceAll(X509Factory.END_CERT, ""));
    
        return (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(decoded));
    }
    

提交回复
热议问题