简体   繁体   中英

How to retrieve the client certificate from a request in a REST service using Quarkus in two way SSL

Following Quarkus Getting Started guide and enabling SSL the next step I wanted to do was to get the client certificate chain.

I would like to do something like this:

    private X509Certificate extractCertificate(HttpServletRequest req) {
        X509Certificate[] certs = (X509Certificate[]) 
        req.getAttribute("javax.servlet.request.X509Certificate");
        if (null != certs && certs.length > 0) {
            return certs[0];
        }
        throw new RuntimeException("No X.509 client certificate found in request");
     }

Following the getting started guide injecting HttpServletRequest is not straight forward as described in this issue

How would be the way to have access to the client certificate chain then?

After doing some research I ended up with the following solution using class io.vertx.core.http.HttpServerRequest

private X509Certificate extractCertificate(HttpServerRequest req) throws SSLPeerUnverifiedException {
    X509Certificate[] certs = req.connection().peerCertificateChain();
    if (null != certs && certs.length > 0) {
        return certs[0];
    }
    throw new RuntimeException("No X.509 client certificate found in request");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM