Tomcat combined with HTTPS

Source: Internet
Author: User

    1. Generate a certificate for the server

Run the console, enter the%java_home%/bin directory, and enter the directory using the following command:

CD "C:\Program Files\java\jdk1.7.0_11\bin" (The generation of certificates is compiled with the 1.7+ version of JDK, code authoring and deployment can be jdk1.6 version, to avoid the compilation of public passwords too weak)

Using Keytool to generate a certificate for Tomcat, assuming the domain name of the target machine is "localhost", the KeyStore file is stored in "D:\home\tomcat.keystore" and the password is "password", which is generated using the following command:

Keytool-genkey-v-alias tomcat-keyalg rsa-keystore D:\home\tomcat.keystore-validity 36500 (parameter brief description: "D:\home\tomcat.ke Ystore "means to save the path of the certificate file, the certificate file name is Tomcat.keystore;"-validity 36500 "means the certificate validity period, 36500 means 100 years, the default is 90 days" Tomcat "for the custom certificate name).

Fill in the necessary parameters at the command line:

A, enter KeyStore password: Here you need to enter a string greater than 6 characters.

B, "What is your first and last name?" "This is required and must be the domain name of the Tomcat deployment host or ip[, such as: Gbcom.com or 10.1.25.251] (which is the access address you will enter in the browser), or the browser will pop up a warning window prompting the user certificate to not match the domain in which it resides. When you do the development test locally, you should fill in "localhost".

C, what is the name of your organizational unit? "," What is your organization name? "," What is your city or region name? "," What is the name of your state or province? "," What is the two-letter country code for this unit? "Can be filled in as needed or can not fill in the direct return, the system asked" correct? ", enter the letter" Y "with the keyboard if required, otherwise enter" n "to re-fill the information.

D, enter <tomcat> master password, this is more important, will be used in the Tomcat configuration file, it is recommended to enter the same password as KeyStore, set other passwords can also, after completing the above input, the direct return to the location you defined in the second step to find the generated file.

    1. Generate certificates for clients

Generate a certificate for your browser so that the server can verify it. In order to successfully import the certificate into IE and Firefox, the certificate format should be PKCS12, so use the following command to generate:

Keytool-genkey-v-alias mykey-keyalg rsa-storetype pkcs12-keystore D:\home\mykey.p12-validity 36500 (MyKey Custom).

The corresponding certificate inventory is placed in "D:\HOME\MYKEY.P12", and the client's CN can be any value. Double-click the Mykey.p12 file to import the certificate into the browser (client).

    1. To have the server trust the client certificate

Because it is a two-way SSL authentication, the server must trust the client certificate, so the client certificate must be added as the server's trust authentication. Because you cannot directly import a certificate library in the PKCS12 format, you must first export the client certificate as a separate CER file, using the following command:

Keytool-export-alias mykey-keystore D:\home\mykey.p12-storetype pkcs12-storepass password-rfc-file D:\home\mykey.ce R

(MyKey is custom-defined MyKey to the client, password is the password you set). With the above command, the client certificate is exported to the "D:\home\mykey.cer" file.

The next step is to import the file into the server's certificate library and add the command as a trusted certificate using the following commands:

Keytool-import-v-file D:\home\mykey.cer-keystore D:\home\tomcat.keystore

With the list command to view the server's certificate library, you can see two certificates, one server certificate and one trusted client certificate:

Keytool-list-keystore D:\home\tomcat.keystore (Tomcat sets the server-side certificate name for you).

    1. Let the client trust the server certificate (optional, do not do this thing, to make local access to be trusted, deployed on the server meaningless)

Because it is a two-way SSL authentication, the client also verifies the server certificate, so the server certificate must be added to the Browse Trusted root certification authorities. Because you cannot directly import a certificate library in the KeyStore format, you must first export the server certificate as a separate CER file, using the following command:

Keytool-keystore D:\home\tomcat.keystore-export-alias Tomcat-file D:\home\tomcat.cer (Tomcat sets the server-side certificate name for you).

With the above command, the server certificate is exported to the "D:\home\tomcat.cer" file. Double-click the Tomcat.cer file, follow the prompts to install the certificate, and fill in the certificate to Trusted root certification authorities.

    1. Configuring the Tomcat Server

Open the/conf/server.xml in the Tomcat root directory, locate the connector port= "8443" configuration section, and place the Tomcat.keystore in the Conf directory and modify it as follows:

<connector protocol= "Org.apache.coyote.http11.Http11NioProtocol"

port= "443" minsparethreads= "5" maxsparethreads= "150"

Enablelookups= "true" disableuploadtimeout= "true"

Acceptcount= "" maxthreads= "400"

Scheme= "https" secure= "true" sslenabled= "true"

Clientauth= "false" sslprotocol= "TLS"

Keystorefile= "Conf/tomcat.keystore"

keystorepass= "Asdfghj"/> (Tomcat to match the generated server-side certificate name)

Property Description:

ClientAuth: Set whether bidirectional authentication, default = False, set to True for bidirectional authentication

Keystorefile: Server certificate file path

Keystorepass: Server certificate Password

Truststorefile: The root certificate used to authenticate the client certificate, in this case the server certificate

Truststorepass: Root certificate Password

To change the port to 443, you can hide the https port, if you want 2 kinds of access, you want to retain the <connector connectiontimeout= "20000" port= "" protocol= "http/1.1" redirectport= "443" executor= "Tomcatthreadpool" uriencoding= "UTF-8"/> if you want HTTP to go directly to HTTPS, open port 80

6

Test

In the browser input: Https://localhost, will pop-up select the client certificate interface, click "OK", will go to the Tomcat home page, the address bar will have a "lock" icon, the next session of the process of transmission of information has been encrypted by SSL information.

Change Global to HTTPS access

<login-config>

<!--Authorization setting for SSL--and

<auth-method>CLIENT-CERT</auth-method>

<realm-name>client Cert users-only area</realm-name>

</login-config>

<security-constraint>

<web-resource-collection >

<web-resource-name>SSL</web-resource-name>

<url-pattern>/*</url-pattern>

</web-resource-collection>

<user-data-constraint>

<transport-guarantee>CONFIDENTIAL</transport-guarantee>

</user-data-constraint>

</security-constraint>

Partially deployed https:

<login-config>

<!--Authorization setting for SSL--and

<auth-method> Client-cert </auth-method>

<!—-specific ways to access see accessories--

<realm-name>client Cert users-only area</realm-name>

</login-config>

<security-constraint>

<web-resource-collection >

<web-resource-name >SSL</web-resource-name>

<url-pattern>/webconfig/device/*</url-pattern>

</web-resource-collection>

<user-data-constraint>

<transport-guarantee>CONFIDENTIAL</transport-guarantee>

</user-data-constraint>

</security-constraint>

This configuration, when the access path includes /webconfig/device/ , will be enforced only with HTTPS access. Even http, automatically adjusts to HTTPS
as needed, web-resource-collection nodes can be configured with multiple, while Url-pattern the configuration requires a mandatory HTTPS of the URL .

and then restart Tomcat , re-access  

Note: Jump to HTTPS after, want to jump to http , you need to write the redirect yourself, attach the address http, It's not going to change that configuration .

This certificate is not a formal certificate and is not trusted by the browser.

Accessories 1 :

Four types of authentication:

Basic:http specification, Base64
<web-app>
......
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
......
</web-app>

DIGEST:HTTP specification, data integrity is strong, but not SSL
<web-app>
......
<login-config>
<auth-method>DIGEST</auth-method>
</login-config>
......
</web-app>

CLIENT-CERT:J2EE specification, strong data integrity, public key (PKC)
<web-app>
......
<login-config>
<auth-method>Client-cert</auth-method>
</login-config>
......
</web-app>

FORM:J2EE specification, data integrity is very weak, no encryption, allowing a custom landing interface.
<web-app>
......
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
......
</web-app>

Here the form method needs to explain is the login page fixed element: login.html

<form name= "LoginForm" method= "post" action= "J_security_check" >

<input name= "J_username" type= "text" >

<input name= "J_password" type= "password" >

<input type= "Submit" value= "Login" >

</form>

The action of the form must be J_security_check, method= "POST", User name Name= "j_username", Password name= "J_password" These are fixed elements

Tomcat combined with HTTPS

Tomcat combined with HTTPS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.