How to receive client certificate in an HTTP request?

吃可爱长大的小学妹 提交于 2019-12-12 03:41:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!