问题
I have generated self signed SSL certificate and given to a client. Every time, the client sends a REST request, I do client certificate authentication on the server. I need to get get data of the certificate in the java code. How do I do that? I have used jersey framework.
回答1:
You can use @Context Annotation for extracting HttpServletRequest.
@POST
@Path("/getHelloWorld")
@Consumes(MediaType.APPLICATION_JSON)
public String helloWorld(@Context HttpServletRequest httpRequest) {
X509Certificate[] certs = (X509Certificate[]) httpRequest.getAttribute("javax.servlet.request.X509Certificate");
if (null != certs && certs.length > 0) {
return <<YOUR CODE HERE>>;
}
return <<YOUR CODE HERE>>;
}
It is assumed that you would have enabled client authentication on your web server. For tomcat server
< Connector SSLEnabled="true" acceptCount="100" clientAuth="true"
disableUploadTimeout="true" enableLookups="false" maxThreads="25"
port="8443" keystoreFile="conf/keystore.jks" keystorePass="changeit"
truststoreFile="conf/truststore.ts" truststorePass="changeit"
protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="https"
secure="true" sslProtocol="TLS" />
来源:https://stackoverflow.com/questions/38640067/how-to-receive-client-certificate-in-an-http-request