好的-经过更多研究之后,我终于完成了这项工作。非常感谢@Dave
G和本教程:在Tomcat上配置双向SSL身份验证,其中的大多数说明均由此解释。
通常,获得相互认证功能的步骤如下:
- 为tomcat服务器创建一个证书。客户端必须信任此证书。
- 为tomcat服务器创建一个密钥库,并将服务器证书导入其中。
- 为客户端创建证书。服务器必须信任此证书。
- 将 客户端 证书导入 服务器 密钥库
- 使用正确的连接器XML更新tomcat server.xml文件。
以上步骤在服务器上是必需的。完成后,要设置客户端,请执行以下操作:
- 将客户端证书从服务器复制到客户端。
- 与服务器通信时,请使用客户端证书(此过程因客户端应用程序的性质而异)。
对于证书配置,我在服务器计算机上执行了以下操作:
# For the following commands, set the values in parenthesis to be whatever makes sense for your environment. The parenthesis are not necessary for the command.# This is an all-in-one command that generates a certificate for the server and places it in a keystore file, while setting both the certifcate password and the keystore password.# The net result is a file called "tomcat.keystore".keytool -genkeypair -alias (serveralias) -keyalg RSA -dname "CN=(server-fqdn),OU=(organizationalunit),O=(organization),L=(locality),ST=(state),C=(country)" -keystore tomcat.keystore -keypass (password) -storepass (password)# This is the all-in-one command that generates the certificate for the client and places it in a keystore file, while setting both the certificate password and the keystore password.# The net result is a file called "client.keystore"keytool -genkeypair -alias (clientalias) -keyalg RSA -dname "CN=(client),OU=(organizationalunit),O=(organization),L=(locality),ST=(state),C=(country)" -keypass (password) -keystore client.keystore -storepass (password)# This command exports the client certificate. # The net result is a file called "client.cer" in your home directory.keytool -exportcert -rfc -alias (clientalias) -file client.cer -keypass (password) -keystore client.keystore -storepass (password)# This command imports the client certificate into the "tomcat.keystore" file.keytool -importcert -alias (clientalias) -file client.cer -keystore tomcat.keystore -storepass (password) -noprompt
现在应该适当设置证书。下一步是在tomcat server.xml中配置连接器。添加如下所示的连接器元素:
<Connector port="8443" maxThreads="150" scheme="https" secure="true" SSLEnabled="true" truststoreFile="/full/path/to/tomcat.keystore" truststorePass="(password)" keystoreFile="/full/path/to/tomcat.keystore" keystorePass="(password)" clientAuth="true" keyAlias="serverkey" sslProtocol="TLS"/>
请注意,在上述XML中:
- “端口”属性可以是您想要的任何属性。
- “ keystoreFile”和“ truststoreFile”属性应为完整路径。缺省情况下,Tomcat不在与server.xml相同的目录中。
- “ keystorePass”和“ truststorePass”属性应与您在创建tomcat.keystore文件时使用的(密码)值匹配。
- 必须 将“ clientAuth”属性设置为“ true”。这就是触发相互认证的原因。
此外,在server.xml中,确保 没有 定义AprLifecycleListner。该侦听器的XML如下所示:
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
该元素应删除/注释掉。AprLifecycleListener的配置方式与上述方法不同,因此无法与这些说明一起使用。
重新启动tomcat。服务器配置应完成。
我使用Firefox测试了我的工作,因为添加客户端证书很容易。打开Firefox并尝试在连接器中定义的端口上连接到tomcat服务的端点。
Ex: https://mytomcatdomain.com:8443/test
执行此操作时,您应该从Firefox得到有关不可信连接的标准警报,因为我们为Tomcat服务器创建了自签名证书。为证书添加一个例外,以便我们的客户端(Firefox)信任我们的服务器(Tomcat)。
添加例外后,您应该会收到“安全连接失败”消息。错误代码为“
ssl_error_bad_cert_alert”。这确认我们的Tomcat服务器正在从客户端请求身份验证。由于我们尚未将Firefox配置为发送受信任的客户端证书,因此请求失败。
要配置Firefox,我们需要做更多的魔术:
// Create a file called DumpPrivateKey.java. The contents should look like so:public class DumpPrivateKey {public static void main(String[] args) throws Exception { final String keystoreName = args[0]; final String keystorePassword = args[1]; final String alias = args[2]; java.security.KeyStore ks = java.security.KeyStore.getInstance("jks"); ks.load(new java.io.FileInputStream(keystoreName), keystorePassword.toCharArray()); System.out.println("-----BEGIN PRIVATE KEY-----"); System.out.println(new sun.misc.base64Enprer().enpre(ks.getKey(alias, keystorePassword.toCharArray()).getEnpred())); System.out.println("-----END PRIVATE KEY-----"); }}使用以下命令编译Java文件:
javac DumpPrivateKey.java
现在,我们将使用这个小工具从上面创建的client.keystore文件中提取密钥。将client.keystore和client.cer文件复制到与DumpPrivateKey类相同的目录中。执行以下命令:
# This extracts the client key from the client keystorejava DumpPrivateKey client.keystore (password) clientkey > clientkey.pkcs8# This creates a client.p12 file that can be used by Firefoxopenssl pkcs12 -export -in client.cer -inkey clientkey.pkcs8 -password pass:(password) -out client.p12
请注意,在上面的代码中,(密码)应该是您用来创建client.keystore的密码。
打开Firefox首选项。单击“证书”选项卡。单击“查看证书”按钮。点击“您的证书”标签。
单击“导入”按钮,然后浏览到先前创建的“ client.p12”文件。应该提示您输入客户端证书的密码。
假设已成功导入“ client.p12”,则现在可以刷新Firefox页面,并且应该从Tomcat服务器端点获得成功的响应。



