It is very easy to set up an embedded Jetty server that requests client authentication: One just needs to add the statement SslContextFactory.setNeedClientAuth(true); to the ssl context when configuring the server. Any client that has its certificate in the server's truststore will be able to establish a TLS connection to the server.
However I need to know which client of all the possible trusted clients is currently making a request; in other words I need to know the client certificate used in this connection, in particular in the handler. Does anyone know how to access this certificate or if it is even possible?
The certificates are added to the Request objects (such as HttpServletRequest), by a HttpConfiguration Customizer.
Specifically, the SecureRequestCustomizer.
Your code to use this would be as follows (scroll down)...
With this SecureRequestCustomizer in place you can access various pieces about the SSL connection from the
HttpServletRequest.getAttribute(String)
calls using the following attribute names.javax.servlet.request.X509Certificate
an array of
java.security.cert.X509Certificate
[]javax.servlet.request.cipher_suite
the String name of the cipher suite. (same as what is returned from
javax.net.ssl.SSLSession.getCipherSuite()
)javax.servlet.request.key_size
Integer of the key length in use
javax.servlet.request.ssl_session_id
String representation (hexified) of the active SSL Session ID
There's a standard servlet request property: javax.servlet.request.X509Certificate
It returns an array of X509Certificates.
We use this to get the name and look up the DN from the cert: