简体   繁体   English

使用 SSL 的 Tomcat 客户端身份验证

[英]Tomcat Client Authentication using SSL

I'm at a loss, since I'm not a Tomcat person.我不知所措,因为我不是 Tomcat 人。 I need to use a 3rd party's web service and they require Client Authentication via SSL, so they generated and issued me an SSL certificate.我需要使用第 3 方的 Web 服务,并且他们需要通过 SSL 进行客户端身份验证,因此他们生成并向我颁发了 SSL 证书。 Unfortunately this is as far as they support it and cannot give me any direction on how to actually use it.不幸的是,这是他们支持的范围,不能给我任何关于如何实际使用它的指导。 I'm stuck using this 3rd party so unfortunately I have to put up with their lack of support.我一直在使用这个 3rd 方,所以不幸的是我不得不忍受他们缺乏支持。

So what I have is a Java application that a vendor is supplying for us (who apparently has never had to deal with this), a Tomcat app server running 6.0.20 on CentOS 5.3, and the SSL cert from the 3rd party.所以我拥有的是一个供应商为我们提供的 Java 应用程序(显然他们从来没有处理过这个问题),一个在 CentOS 5.3 上运行 6.0.20 的 Tomcat 应用程序服务器,以及来自 3rd 方的 SSL 证书。

What all do I need to do at this point?此时我需要做什么? All I can find online is how to set up a keystore so that my app can use Client Authentication against things connecting to it, not for when it needs to connect out to someone else, or how to use SSL over port 8443 (which I know how to do already and have set up).我在网上能找到的只是如何设置密钥库,以便我的应用程序可以对连接到它的事物使用客户端身份验证,而不是在它需要连接到其他人时,或者如何通过端口 8443(我知道)使用 SSL如何做已经设置)。

Update:更新:

Try following way to enable the client authentication in Tomcat.尝试以下方法在 Tomcat 中启用客户端身份验证。

To make tomcat take advantages of Client Authentication, we require three certificates.为了让 tomcat 充分利用 Client Authentication,我们需要三个证书。 ie A Server Certificate for Tomcat, Client Certificate for the browser and Certificate of the CA which will sign both the above mentioned certificates.即用于 Tomcat 的服务器证书、用于浏览器的客户端证书和将签署上述两个证书的 CA 证书。 Here, I'll show how to do it in Windows.在这里,我将展示如何在 Windows 中执行此操作。

There are two ways.有两种方法。

  1. You should have a CSR file ie a Certificate Signing Request.您应该有一个 CSR 文件,即证书签名请求。 You can submit it to the Certificate Authority like Verisign or Comodo or many other like them.您可以将其提交给VerisignComodo或许多其他类似的证书颁发机构。 They'll provide you the certificate.他们会给你证书。 Or要么

  2. You can create your own Certificate Authority and sign the certificates.您可以创建自己的证书颁发机构并签署证书。 But it is recommended to do this for personal use only.但建议仅用于个人用途。

You should have Java and OpenSSL installed to perform the below steps.您应该安装 Java 和 OpenSSL 以执行以下步骤。

To generate the Certificate Signing Request, you should have the key.要生成证书签名请求,您应该拥有密钥。 To generate the key type the following command in CMD.要生成密钥,请在 CMD 中键入以下命令。

openssl genrsa -out Serverkey.key 1024 openssl genrsa -out Serverkey.key 1024

This will generate a file "Serverkey.key".这将生成一个文件“Serverkey.key”。 The key size is 1024. You can give it as per your requirement.密钥大小为 1024。您可以根据需要提供它。

Now generate the CSR file with the help of following command.现在借助以下命令生成 CSR 文件。

openssl req -new -key Serverkey.key -out ServerReq.csr -config /path/to/openssl.cnf openssl req -new -key Serverkey.key -out ServerReq.csr -config /path/to/openssl.cnf

Once you execute this command, you'll be asked to give some information.执行此命令后,系统会要求您提供一些信息。 After that, you'll find the CSR file in your directory.之后,您将在目录中找到 CSR 文件。 You can submit this file to the CA.您可以将此文件提交给 CA。 In case, you are doing this for your personal use, and you want to have your own CA, create a key and CSR for your CA with the help of above given two commands.如果您这样做是为了个人使用,并且您想要拥有自己的 CA,请在上面给出的两个命令的帮助下为您的 CA 创建一个密钥和 CSR。 After you have your CSR for CA, you can sign with the CA's key with the help of following command.获得 CA 的 CSR 后,您可以在以下命令的帮助下使用 CA 的密钥进行签名。

openssl x509 -req -days 365 -in CAReq.csr -signkey CAKey.key -out CA.crt openssl x509 -req -days 365 -in CAReq.csr -signkey CAKey.key -out CA.crt

Once you have the CA certificate, you can use it to sign other certificates.拥有 CA 证书后,您可以使用它来签署其他证书。

openssl x509 -req -days 365 -CA CA.crt -CAkey CAKey.key -CAcreateserial -in ServerReq.csr -out Server.crt openssl x509 -req -days 365 -CA CA.crt -CAkey CAKey.key -CAcreateserial -in ServerReq.csr -out Server.crt

You can use the same command for client certificate as well.您也可以对客户端证书使用相同的命令。

The browser which is our client here, will accept the P12 format certificate.我们这里的客户端浏览器将接受 P12 格式的证书。 P12 format is a file which contains your certificate as well as the key. P12 格式是一个包含您的证书和密钥的文件。

To conver the CRT to P12 use the following command.要将 CRT 转换为 P12,请使用以下命令。

openssl pkcs12 -export -in Server.crt -inkey ServerKey.key -chain -CAfile CA.crt -out ServerCert.p12 openssl pkcs12 -export -in Server.crt -inkey ServerKey.key -chain -CAfile CA.crt -out ServerCert.p12

In tomcat, there is one truststore which will have the CA's certificate and another is a keystore which will have server's key and certificate (p12 file).在 tomcat 中,有一个信任库将拥有 CA 的证书,另一个信任库将拥有服务器的密钥和证书(p12 文件)。

For importing CA's certificate to truststore use the following command.要将 CA 的证书导入信任库,请使用以下命令。

keytool -import -alias CertAuth -keystore caCerts.jks -file CA.crt keytool -import -alias CertAuth -keystore caCerts.jks -file CA.crt

You can give alias as whatever you want.您可以随意指定别名。 Note the password that you give when asked after executing the above command.请注意您在执行上述命令后询问时提供的密码。 We'll use that password in the server.xml file.我们将在 server.xml 文件中使用该密码。 Same applies for the below command.同样适用于以下命令。

For importing the p12 format certificate to the keystore use the following command.要将 p12 格式证书导入密钥库,请使用以下命令。

keytool -importkeystore -destkeystore tomcat.keystore -srckeystore -ServerCert.p12 -srcstoretype PKCS12 -alias 1 keytool -importkeystore -destkeystore tomcat.keystore -srckeystore -ServerCert.p12 -srcstoretype PKCS12 -alias 1

Now, change the tomcat's server.xml as following.现在,将 tomcat 的 server.xml 更改如下。

<Connector port="8443" 
           protocol="org.apache.coyote.http11.Http11NioProtocol"
           SSLEnabled="true" scheme="https" secure="true"
           truststoreFile="path/to/truststorefile" truststorePass="password" 
           keystoreFile="path/to/keystorefile" keystorePass="password"
           clientAuth="true" sslProtocol="TLS"
           />

Now, import the Client's P12 format certificate to the browser.现在,将客户端的 P12 格式证书导入浏览器。 Then, start the tomcat server and try to access https://localhost:8443.然后,启动tomcat服务器,尝试访问https://localhost:8443。 You can visit the blog for the detailed version of this answer.您可以访问博客以获取此答案的详细版本。 Hope this helps.希望这可以帮助。

Here's the really long answer: http://java.sun.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html这是很长的答案: http : //java.sun.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html

Don't take my word for it, but I believe that, as a client, client auth will automatically be performed when the server requests it.不要相信我的话,但我相信,作为客户端,客户端身份验证将在服务器请求时自动执行。

If configuring tomcat is the question, have you read http://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html ?如果配置 tomcat 是个问题,您是否阅读过http://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html In particular, note the clientAuth attribute of the Connector element.特别要注意连接器元素的 clientAuth 属性。

I don't know that this is about configuring Tomcat, other than to be able to pass in system properties to a web application running in Tomcat.除了能够将系统属性传递给在 Tomcat 中运行的 Web 应用程序之外,我不知道这是关于配置 Tomcat 的。

The vendor that supplies the web application really should be able to tell you how to get the client connection from their software to use a specific client certificate when making an SSL connection to a remote web service.提供 Web 应用程序的供应商确实应该能够告诉您在与远程 Web 服务建立 SSL 连接时如何从他们的软件获取客户端连接以使用特定的客户端证书。

For instance, they could have their application implement a custom KeyManager for SSL connections that is able to look up the client certificate and private key from a configurable location.例如,他们可以让他们的应用程序为 SSL 连接实现一个自定义的KeyManager ,它能够从一个可配置的位置查找客户端证书和私钥。

If they haven't done that, they are probably using the default SunX509 KeyManager.如果他们还没有这样做,他们可能正在使用默认的 SunX509 KeyManager。

For the default KeyManager, you can apparently use keytool to create a keystore containing the client certificate and private key the certificate describes.对于默认的 KeyManager,您显然可以使用 keytool 创建一个包含客户端证书和证书描述的私钥的密钥库。 Then you can specify that key store using the following system parameters:然后您可以使用以下系统参数指定该密钥库:

-Djavax.net.ssl.keyStore="/path/to/keystore"
-Djavax.net.ssl.keyStorePassword="<password>"

You will need to configure Tomcat to pass in these properties.您需要配置 Tomcat 以传入这些属性。

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

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