简体   繁体   English

如何在HTTP请求中接收客户端证书?

[英]How to receive client certificate in an HTTP request?

I have generated self signed SSL certificate and given to a client. 我已经生成了自签名的SSL证书并提供给客户端。 Every time, the client sends a REST request, I do client certificate authentication on the server. 每次客户端发送REST请求时,我都会在服务器上进行客户端证书认证。 I need to get get data of the certificate in the java code. 我需要在Java代码中获取证书的数据。 How do I do that? 我怎么做? I have used jersey framework. 我用过球衣框架。

You can use @Context Annotation for extracting HttpServletRequest. 您可以使用@Context Annotation提取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. 假定您将在Web服务器上启用客户端身份验证。 For tomcat server 对于tomcat服务器

< 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" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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