Use keytools to configure ssl bidirectional authentication for tomcat 7

  • 2020-06-03 06:28:52
  • OfStack

SSL brief introduction

SSL(Secure Sockets Layer Secure Sockets Layer) is a protocol (specification) used to secure the communication between the client and the server so that the information transmitted during the communication is not stolen or modified.

1. How to ensure the security of data transmission?

Client and server in a handshake (client and server connection is established, and the exchange of parameter process called a handshake) will produce a "conversation key" (session key), is used to encrypt the data transmission, the decryption is also use this "conversation key," and the "conversation key" only know the client and server. That is, as long as the "conversation key" is not cracked, it can be secured.

2. Client and server certificates

Client certificates and server certificates are used to prove one's identity, just as each person has an ID card, which is the only one. Generally, a server-side certificate is sufficient, but sometimes the client is required to provide its own certificate to prove its identity.


Keytool certificate is 1 Java data management tools, Keytool the key (key) and certificate (certificates) there is a file called keystore in keystore, contains two kinds of data: the key entities (Key entity) - the key (secret key) or a private key and matching the public key (asymmetric encryption) trusted certificate entities (trusted certificate entries) - only contains the public key. Let's take a look at the detailed process of configuring ssl bi-directional authentication for tomcat 7 using keytools.

1. Generation of certificate base, certificate, etc

1. Generate server certificate library

keytool -validity 36500 -genkey -v -alias tomcat_server -keyalg RSA -keystore tomcat_server.keystore -dname "CN=,OU=,O=,L=,ST=,c=" -storepass 123456 -keypass 123456
-validity 36500 valid in days CN 1 must be the domain name or IP address of the server OU organizational unit O organization L area ST State/province C countries

2. Client certificate

keytool -validity 36500 -genkeypair -v -alias testclient -keyalg RSA -storetype PKCS12 -keystore testclient.p12 -dname "CN=testclient,OU=,O=,L=,ST=,c=" -storepass 123456

-ES58en PKCS12 is mainly for importing certificates into IE/firefox.

Import the generated certificate into IE.

3. Import the client certificate into the server certificate library

The server-side certificate does not recognize the CERTIFICATE in p12 format. You need to export the CER format certificate from the client-side certificate and then import the CER format certificate into the server-side certificate.

keytool -export -alias testclient -keystore testclient.p12 -storetype PKCS12 -storepass 123456 -rfc -file testclient.cer

Then import client.cer into the server certificate library (use any one of the following commands)

keytool -import -v -file testclient.cer -keystore tomcat_server.keystore -storepass 123456
keytool -import -alias testclient -v -file testclient.cer -keystore tomcat_server.keystore -storepass 123456

Note: The alias here is mykey. If you don't have an alias, the default is mykey, so don't be surprised to see mykey.

4. Export the server certificate from the server certificate library

keytool -export -alias tomcat_server -keystore tomcat_server.keystore -storepass 123456 -rfc -file tomcat_server.cer

This certificate can be imported into the browser to have the client trust the server certificate. Not importing does not affect usage, but the browser will not trust the server certificate and will prompt an error message.

5. View all certificates in the certificate store

keytool -list -keystore tomcat_server.keystore -storepass 123456

2. Tomcat configuration

Configuration server xml

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
 maxThreads="150" scheme="https" secure="true"
 clientAuth="true" sslProtocol="TLS" />

Just start tomcat.

The problem

If you start times the following error:

SEVERE: Failed to initialize end point associated with ProtocolHandler ["http-apr-8443"]
java.lang.Exception: Connector attribute SSLCertificateFile must be defined when using SSL with APR
 at org.apache.coyote.AbstractProtocol.init(
 at org.apache.catalina.connector.Connector.initInternal(
 at org.apache.catalina.util.LifecycleBase.init(
 at org.apache.catalina.core.StandardService.initInternal(
 at org.apache.catalina.util.LifecycleBase.init(
 at org.apache.catalina.core.StandardServer.initInternal(
 at org.apache.catalina.util.LifecycleBase.init(
 at org.apache.catalina.startup.Catalina.load(
 at org.apache.catalina.startup.Catalina.load(
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(
 at java.lang.reflect.Method.invoke(
 at org.apache.catalina.startup.Bootstrap.load(
 at org.apache.catalina.startup.Bootstrap.main(

As for the implementation of SSL, Tomcat provides two types: JSSE and APR. If APR is installed, APR is preferred as the implementation.

The ssh configuration of APR requires OpenSSH to be configured. This is explained in ES122en. xml:

Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, the connector should be using the OpenSSL style configuration described in the APR documentation

What about APR? There are two ways,

1, amend the HTTP protocol = "/ 1.1" to protocol = "org. apache. coyote. http11. Http11Protocol"

2. Under windows, tcnative-1.dll can be deleted.


The above is the whole content of this article, I hope the content of this article can bring 1 definite help to your study or work, if you have any questions, you can leave a message to communicate.

Related articles: