您需要执行几个步骤
1)配置tomcat进行客户端证书认证(在server.xml中):
<Connector port="8443"
protocol="HTTP/1.1"
SSLEnabled="true"
maxThreads="150"
minSpareThreads="25"
maxSpareThreads="75"
enableLookups="false"
disableUploadTimeout="true"
acceptCount="100"
keyAlias="tomcat"
debug="0"
scheme="https"
secure="true"
clientAuth="want"
sslProtocol="TLS"
keystoreFile="server.keystore" keystorePass="changeit"
truststoreFile="trust.keystore" truststorePass="changeit"/>
http://tomcat.apache.org/tomcat-7.0-doc/config/http.html关于clientAuth属性的澄清:
如果您希望 SSL 堆栈请求客户端证书,则设置为需要,但如果没有提供,则不会失败。
请参阅 Tomcat 身份验证以更好地解释如何为 SSL 配置 Tomcat:http: //tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html
2) 使用以下代码实现您的 hasValidClientCertificate():
X509Certificate[] crts = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate");
if (crts!= null && crts.length > 0) {
return true;
}
3)如果需要访问用户证书请访问
X509Certificate userCert = crts [0];