回想一下,密钥对的定义包括私钥和公钥。与 SSL 证书验证和保护服务器通信类似,客户端证书的有效性需要由创建它的认证机构验证。
由于我们使用 keytool 命令创建了我们自己的自签名客户端证书,Java VM 不会隐含地信任它,因为它是由受信任的证书颁发机构分配的。
让我们看一下以下步骤:
- We will need to force Tomcat to recognize the certificate as a trusted certificate. We do this by exporting the public key from the key pair and adding it to the Tomcat trust store.
- Again, if you do not wish to perform this step now, you can use the existing trust store in .src/main/resources/keys and skip to where we configure server.xml later in this section.
- We'll export the public key to a standard certificate file named jbcp_clientauth.cer, as follows:
keytool -exportcert -alias jbcpclient -keystore jbcp_clientauth.p12
-storetype PKCS12 -storepass changeit -file jbcp_clientauth.cer
- Next, we'll import the certificate into the trust store (this will create the trust store, but in a typical deployment scenario you'd probably already have some other certificates in the trust store):
keytool -importcert -alias jbcpclient -keystore tomcat.truststore
-file jbcp_clientauth.cer
上述命令将创建名为 tomcat.truststore 的信任库并提示您输入密码(我们选择了密码 changeit)。您还将看到有关证书的一些信息,最终会被要求确认您确实信任该证书,如下所示:
Owner: [email protected], OU=JBCP Calendar, O=JBCP, L=Park City,
ST=UT, C=US
Issuer: [email protected], OU=JBCP Calendar, O=JBCP, L=Park City,
ST=UT, C=US
Serial number: 464fc10c
Valid from: Fri Jun 23 11:10:19 MDT 2017 until: Thu Feb 12 10:10:19
MST 2043
//Certificate fingerprints:
MD5: 8D:27:CE:F7:8B:C3:BD:BD:64:D6:F5:24:D8:A1:8B:50
SHA1: C1:51:4A:47:EC:9D:01:5A:28:BB:59:F5:FC:10:87:EA:68:24:E3:1F
SHA256: 2C:F6:2F:29:ED:09:48:FD:FE:A5:83:67:E0:A0:B9:DA:C5:3B:
FD:CF:4F:95:50:3A:
2C:B8:2B:BD:81:48:BB:EF
Signature algorithm name: SHA256withRSA
Version: 3
//Extensions
#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 29 F3 A7 A1 8F D2 87 4B EA 74 AC 8A 4B BC 4B 5D
)......K.t..K.K]
0010: 7C 9B 44 4A ..DJ
]
]
Trust this certificate? [no]: yes
记住新的位置 tomcat.truststore 文件,因为我们需要在我们的Tomcat配置中引用它。
密钥库和信任库有什么区别?
Java 安全套接字扩展 (
JSSE) 文档将密钥库定义为私钥及其对应公钥的存储机制。密钥库(包含密钥对)用于加密或解密安全消息等。信任库旨在在验证身份时仅为受信任的通信伙伴存储公钥(类似于在证书身份验证中使用信任库的方式)。然而,在许多常见的管理场景中,密钥库和信任库被合并到一个文件中(在 Tomcat 中,这将通过使用
keystoreFile 和
truststoreFile 连接器的属性)。文件本身的格式可以完全相同。实际上,每个文件都可以是任何 JSSE 支持的密钥库格式,包括
Java 密钥库 (
JKS)、PKCS 12 等。
- As previously mentioned, we assume you have already configured the SSL Connector, as outlined in the Appendix, Additional Reference Material. If you do not see the keystoreFile or keystorePass attributes in server.xml, it means you should visit the Appendix, Additional Reference Material to get SSL set up.
- Finally, we'll need to point Tomcat at the trust store and enable client certificate authentication. This is done by adding three additional attributes to the SSL connector in the Tomcat server.xml file, as follows:
//sever.xml
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
sslProtocol="TLS"
keystoreFile="<KEYSTORE_PATH>/tomcat.keystore"
keystorePass="changeit"
truststoreFile="<CERT_PATH>/tomcat.truststore"
truststorePass="changeit"
clientAuth="true"
/>
这
server.xml 文件位于
TOMCAT_HOME/conf/server.xml。如果您使用 Eclipse 或 Spring Tool Suite 与 Tomcat 交互,您会发现一个名为
服务器 包含
server.xml。例如,如果您使用的是 Tomcat 8,则 Eclipse 工作区中的路径可能类似于
/Servers/Tomcat v7.0 服务器 在
localhost-config/server.xml。
- This should be the remaining configuration required to trigger Tomcat to request a client certificate when an SSL connection is made. Of course, you will want to ensure you replace both <CERT_PATH> and <KEYSTORE_PATH> with the full paths. For example, on a Unix-based operating system, the path might look like this: /home/mickknutson/packt/chapter8/keys/tomcat.keystore.
- Go ahead and try to start up Tomcat to ensure that the server starts up without any errors in the logs.
还有一种方法可以配置 Tomcat 以选择性地使用客户端证书身份验证——我们将在本章后面启用它。现在,我们首先需要使用客户端证书来连接到 Tomcat 服务器。这样可以更轻松地诊断您是否已正确设置!