SSL简介 SSL, 或者Secure Socket Layer,是一种允许web浏览器和web服务器通过一个安全的连接进行交流的技术。这意味着将被发送的数据在一端被翻译成密码,传送出去,然后在另一端解开密码,再进行处理。这是一个双向的过程,也就是浏览器和服务器都需要在发送数据之前对它们进行加密。 SSL协定的另一个重要方面是认证(Authentication)。这就是说,在你开始试图通过一个安全连接与一个web服务器交流的时候,这个服务器会要求你的浏览器出示一组证件,通过“鉴定”的方式来证明这就是你所声明的网站。 在某些情况下,服务器还会要求你的web浏览器的认证书,证明你就是你所说的那个人。这就是所知的“客户认证”,尽管实际情况中,更多地用在商务-对-商务(B2B)交易,而不是对个人用户。 但大多数有SSL功能的web服务器不要求客户认证(Client Authentication)。 证书 为了能实施SSL,一个web服务器对每个接受安全连接的外部接口(IP 地址)必须要有相应的证书(Certificate)。关于这个设计的理论是一个服务器必须提供某种合理的保证以证明这个服务器的主人就是你所认为的那个人。这个证书要陈述与这个网站相关联的公司,以及这个网站的所有者或系统管理员的一些基本联系信息。 这个证书由所有人以密码方式签字,其他人非常难伪造。对于进行电子商务(e-commerce)的网站,或其他身份认证至关重要的任何商业交易,认证书要向大家所熟知的认证权威(Certificate Authority (CA))如VeriSign或Thawte来购买。这样的证书可用电子技术证明属实。实际上,认证权威单位会担保它发出的认证书的真实性,如果你信任发出认证书的认证权威单位的话,你就可以相信这个认证书是有效的。 在许多情况下,认证并不是真正使人担忧的事。系统管理员或许只想要保证被服务器传送和接收的数据是秘密的,不会被连接线上的偷窃者盗窃到。庆幸的是,Java提供相对简单的被称为keytool的命令行工具,可以简单地产生“自己签名”的证书。自己签名的证书只是用户产生的证书,没有正式在大家所熟知的认证权威那里注册过,因此不能确保它的真实性。但却能保证数据传输的安全性。 认证也许很重要,也许不重要,完全决定于网站的需要。 用Tomcat来配置SSL主要有下面这么两大步骤: 一、生成证书 1、 在命令行下执行: %Java_home%\bin\keytool -genkey -alias tomcat -keyalg RSA 在此命令中,keytool是JDK自带的产生证书的工具。把RSA运算法则作为主要安全运算法则,这保证了与其它服务器和组件的兼容性。 这个命令会在用户的home directory产生一个叫做" .keystore " 的新文件。在执行后,你首先被要求出示keystore密码。Tomcat使用的默认密码是" changeit "(全都是小写字母),如果你愿意,你可以指定你自己的密码。你还需要在server.xml配置文件里指定自己的密码,这在以后会有描述。 2、 你会被要求出示关于这个认证书的一般性信息,如公司,联系人名称,等等。这些信息会显示给那些试图访问你程序里安全网页的用户,以确保这里提供的信息与他们期望的相对应。 3、 你会被要求出示密钥(key)密码,也就是这个认证书所特有的密码(与其它的储存在同一个keystore文件里的认证书不同)。你必须在这里使用与keystore密码相同的密码。(目前,keytool会提示你按ENTER键会自动帮你做这些)。 如果一切顺利,你现在就拥有了一个可以被你的服务器使用的有认证书的keystore文件。 二、配置tomcat 第二个大步骤是把secure socket配置在$CATALINA_HOME/conf/server.xml文件里。$CATALINA_HOME代表安装Tomcat的目录。一个例子是SSL连接器的<Connector>元素被包括在和Tomcat一起安装的缺省server.xml文件里。它看起来象是这样: $CATALINA_HOME/conf/server.xml <-- Define a SSL Coyote HTTP/1.1 Connector on port 8443 --> <!-- <Connector port="8443" minProcessors="5" maxProcessors="75" enableLookups="true" disableUploadTimeout="true" acceptCount="100" debug="0" scheme="https" secure="true"; clientAuth="false" sslProtocol="TLS"/> --> Connector元素本身,其默认形式是被注释掉的(commented out),所以需要把它周围的注释标志删除掉。然后,可以根据需要客户化(自己设置)特定的属性。一般需要增加一下keystoreFile和keystorePass两个属性,指定你存放证书的路径(如:keystoreFile="C:/.keystore")和刚才设置的密码(如:keystorePass="123456")。关于其它各种选项的详细信息,可查阅Server Configuration Reference。 在完成这些配置更改后,必须象重新启动Tomcat,然后你就可以通过SSL访问Tomcat支持的任何web应用程序。只不过指令需要像下面这样: https://localhost:8443




        SSL Configuration HOW-TO




Quick-Start Version

The description below uses the variable name $CATALINA_HOME to refer to the directory into which you have installed Tomcat 4, and is the base directory against which most relative paths are resolved. However, if you have configured Tomcat 4 for multiple instances by setting a CATALINA_BASE directory, you should use $CATALINA_BASE instead of $CATALINA_HOME for each of these references.


To install and configure SSL support on Tomcat 4, you need to follow these simple steps. For more information, read the rest of this HOW-TO.

  1. Download JSSE 1.0.2 (or later) from ​​http://java.sun.com/products/jsse/​​ and either make it an installed extension on the system, or else set an environment variable ​​JSSE_HOME​​ that points at the directory into which you installed JSSE.

  2. Create a certificate keystore by executing the following command:Windows:

Unix:

%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSA

  1. and specify a password value of "changeit".

  1. Uncomment the "SSL HTTP/1.1 Connector" entry in ​​$CATALINA_HOME/conf/server.xml​​ and tweak as necessary.

Introduction to SSL

SSL, or Secure Socket Layer, is a technology which allows web browsers and web servers to communicate over a secured connection. This means that the data being sent is encrypted by one side, transmitted, then decrypted by the other side before processing. This is a two-way process, meaning that both the server AND the browser encrypt all traffic before sending out data.

Another important aspect of the SSL protocol is Authentication. This means that during your initial attempt to communicate with a web server over a secure connection, that server will present your web browser with a set of credentials, in the form of a "Certificate", as proof the site is who and what it claims to be. In certain cases, the server may also request a Certificate from your web browser, asking for proof that you are who you claim to be. This is known as "Client Authentication," although in practice this is used more for business-to-business (B2B) transactions than with individual users. Most SSL-enabled web servers do not request Client Authentication.

SSL and Tomcat

It is important to note that configuring Tomcat to take advantage of secure sockets is usually only necessary when running it as a stand-alone web server. When running Tomcat primarily as a Servlet/JSP container behind another web server, such as Apache or Microsoft IIS, it is usually necessary to configure the primary web server to handle the SSL connections from users. Typically, this server will negotiate all SSL-related functionality, then pass on any requests destined for the Tomcat container only after decrypting those requests. Likewise, Tomcat will return cleartext responses, that will be encrypted before being returned to the user's browser. In this environment, Tomcat knows that communications between the primary web server and the client are taking place over a secure connection (because your application needs to be able to ask about this), but it does not participate in the encryption or decryption itself.

Certificates

In order to implement SSL, a web server must have an associated Certificate for each external interface (IP address) that accepts secure connections. The theory behind this design is that a server should provide some kind of reasonable assurance that its owner is who you think it is, particularly before receiving any sensitive information. While a broader explanation of Certificates is beyond the scope of this document, think of a Certificate as a "digital driver's license" for an Internet address. It states what company the site is associated with, along with some basic contact information about the site owner or administrator.

This "driver's license" is cryptographically signed by its owner, and is therefore extremely difficult for anyone else to forge. For sites involved in e-commerce, or any other business transaction in which authentication of identity is important, a Certificate is typically purchased from a well-known Certificate Authority (CA) such as VeriSign or Thawte. Such certificates can be electronically verified -- in effect, the Certificate Authority will vouch for the authenticity of the certificates that it grants, so you can believe that that Certificate is valid if you trust the Certificate Authority that granted it.

In many cases, however, authentication is not really a concern. An administrator may simply want to ensure that the data being transmitted and received by the server is private and cannot be snooped by anyone who may be eavesdropping on the connection. Fortunately, Java provides a relatively simple command-line tool, called​​keytool​​, which can easily create a "self-signed" Certificate. Self-signed Certificates are simply user generated Certificates which have not been officially registered with any well-known CA, and are therefore not really guaranteed to be authentic at all. Again, this may or may not even be important, depending on your needs.

General Tips on Running SSL

The first time a user attempts to access a secured page on your site, he or she is typically presented with a dialog containing the details of the certificate (such as the company and contact name), and asked if he or she wishes to accept the Certificate as valid and continue with the transaction. Some browsers will provide an option for permanently accepting a given Certificate as valid, in which case the user will not be bothered with a prompt each time they visit your site. Other browsers do not provide this option. Once approved by the user, a Certificate will be considered valid for at least the entire browser session.

Also, while the SSL protocol was designed to be as efficient as securely possible, encryption/decryption is a computationally expensive process from a performance standpoint. It is not strictly necessary to run an entire web application over SSL, and indeed a developer can pick and choose which pages require a secure connection and which do not. For a reasonably busy site, it is customary to only run certain pages under SSL, namely those pages where sensitive information could possibly be exchanged. This would include things like login pages, personal information pages, and shopping cart checkouts, where credit card information could possibly be transmitted. Any page within an application can be requested over a secure socket by simply prefixing the address with ​​https:​​​ instead of ​​http:​​. Any pages which absolutely require a secure connection should check the protocol type associated with the page request and take the appropriate action of ​​https​​ is not specified.

Finally, using name-based virtual hosts on a secured connection can be problematic. This is a design limitation of the SSL protocol itself. The SSL handshake, where the client browser accepts the server certificate, must occur before the HTTP request is accessed. As a result, the request information containing the virtual host name cannot be determined prior to authentication, and it is therefore not possible to assign multiple certificates to a single IP address. If all virtual hosts on a single IP address need to authenticate against the same certificate, the addition of multiple virtual hosts should not interfere with normal SSL operations on the server. Be aware, however, that most client browsers will compare the server's domain name against the domain name listed in the certificate, if any (applicable primarily to official, CA-signed certificates). If the domain names do not match, these browsers will display a warning to the client user. In general, only address-based virtual hosts are commonly used with SSL in a production environment.

Configuration

Download and Install JSSE

Download the Java Secure Socket Extensions (JSSE) package, version 1.0.2 or later, from​​http://java.sun.com/products/jsse/​​. If you built Tomcat from source, you have probably already downloaded this package. If you are running JDK 1.4 (currently in beta), these classes have been integrated directly into the JDK, so you can skip this entire step.

After expanding the package, there are two ways to make it available to Tomcat (choose one or the other):

  • Make JSSE an installed extension by copying all three JAR files (​​jcert.jar​​, ​​jnet.jar​​, and ​​jsse.jar​​) into your ​​$JAVA_HOME/jre/lib/ext​​ directory.
  • Create a new environment variable ​​JSSE_HOME​​ that contains the absolute path to the directory into which you unpacked the JSSE binary distribution.

Prepare the Certificate Keystore

Tomcat currently operates only on ​​JKS​​ format keystores. This is Java's standard "Java KeyStore" format, and is the format created by the ​​keytool​​ command-line utility. This tool is included in the JDK.

To import an existing certificate into a JKS keystore, please read the documentation (in your JDK documentation package) about ​​keytool​​.

To create a new keystore from scratch, containing a single self-signed Certificate, execute the following from a terminal command line:

Windows:


Unix:


(The RSA algorithm should be preferred as a secure algorithm, and this also ensures general compatibility with other servers and components.)

This command will create a new file, in the home directory of the user under which you run it, named "​​.keystore​​". To specify a different location or filename, add the ​​-keystore​​ parameter, followed by the complete pathname to your keystore file, to the ​​keytool​​ command shown above. You will also need to reflect this new location in the ​​server.xml​​ configuration file, as described later. For example:

Windows:


Unix:


After executing this command, you will first be prompted for the keystore password. The default password used by Tomcat is "​​changeit​​" (all lower case), although you can specify a custom password if you like. You will also need to specify the custom password in the ​​server.xml​​ configuration file, as described later.

Next, you will be prompted for general information about this Certificate, such as company, contact name, and so on. This information will be displayed to users who attempt to access a secure page in your application, so make sure that the information provided here matches what they will expect.

Finally, you will be prompted for the key password, which is the password specifically for this Certificate (as opposed to any other Certificates stored in the same keystore file). You MUST use the same password here as was used for the keystore password itself. (Currently, the ​​keytool​​ prompt will tell you that pressing the ENTER key does this for you automatically.)

If everything was successful, you now have a keystore file with a Certificate that can be used by your server.

Edit the Tomcat Configuration File

The final step is to configure your secure socket in the ​​$CATALINA_HOME/conf/server.xml​​ file, where​​$CATALINA_HOME​​ represents the directory into which you installed Tomcat 4. An example ​​<Connector>​​ element for an SSL connector is included in the default ​​server.xml​​ file installed with Tomcat. It will look something like this:


You will note that the Connector element itself is commented out by default, so you will need to remove the comment tags around it. Then, you can customize the specified attributes as necessary. For detailed information about the various options, consult the ​​Server Configuration Reference​​. The following discussion covers only those attributes of most interest when setting up SSL communication.

The ​​port​​ attribute (default value is 8443) is the TCP/IP port number on which Tomcat will listen for secure connections. You can change this to any port number you wish (such as to the default port for​​https​​ communications, which is 443). However, special setup (outside the scope of this document) is necessary to run Tomcat on port numbers lower than 1024 on many operating systems.

If you change the port number here, you should also change the value specified for the​​redirectPort​​ attribute on the non-SSL connector. This allows Tomcat to automatically redirect users who attempt to access a page with a security constraint specifying that SSL is required, as required by the Servlet 2.3 Specification.

You will notice a ​​Factory​​ element nested inside the ​​Connector​​ element. This is where the "socket factory" used by Tomcat, whenever it needs a socket on the corresponding port number, is configured. You may need to add or change the following attribute values, depending on how you configured your keystore earlier:

AttributeDescription​​className​​The fully qualified class name of the Java class that implements this socket factory. Do not change the default value.​​clientAuth​​Set this value to ​​true​​ if you want Tomcat to require all SSL clients to present a client Certificate in order to use this socket.​​keystoreFile​​Add this attribute if the keystore file you created is not in the default place that Tomcat expects (a file named ​​.keystore​​ in the user home directory under which Tomcat is running). You can specify an absolute pathname, or a relative pathname that is resolved against the ​​$CATALINA_BASE​​ environment variable.​​keystorePass​​Add this element if you used a different keystore (and Certificate) password than the one Tomcat expects (​​changeit​​).​​protocol​​The encryption/decryption protocol to be used on this socket. Do not change the default value.

After completing these configuration changes, you must restart Tomcat as you normally do, and you should be in business. You should be able to access any web application supported by Tomcat via SSL. For example, try:

%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSA

and you should see the usual Tomcat splash page (unless you have modified the ROOT web application). If this does not work, the following section contains some troubleshooting tips.

Troubleshooting

Here is a list of common problems that you may encounter when setting up SSL communications, and what to do about them.

  • I get "java.security.NoSuchAlgorithmException" errors in my log files.

The JVM cannot find the JSSE JAR files. Follow all of the directions to ​​download and install JSSE​​.

  • When Tomcat starts up, I get an exception like "java.io.FileNotFoundException: {some-directory}/{some-file} not found".

A likely explanation is that Tomcat cannot find the keystore file where it is looking. By default, Tomcat expects the keystore file to be named ​​.keystore​​​ in the user home directory under which Tomcat is running (which may or may not be the same as yours :-). If the keystore file is anywhere else, you will need to add a ​​keystoreFile​​​ attribute to the ​​<Factory>​​​ element in the ​​Tomcat configuration file​​.

  • When Tomcat starts up, I get an exception like "java.io.FileNotFoundException: Keystore was tampered with, or password was incorrect".

Assuming that someone has not actually tampered with your keystore file, the most likely cause is that Tomcat is using a different password than the one you used when you created the keystore file. To fix this, you can either go back and ​​recreate the keystore file​​​, or you can add or update the ​​keystorePass​​​ attribute on the ​​<Factory>​​​ element in the ​​Tomcat configuration file​​.REMINDER - Passwords are case sensitive!

If you are still having problems, a good source of information is the TOMCAT-USER mailing list. You can find pointers to archives of previous messages on this list, as well as subscription and unsubscription information, at ​​http://jakarta.apache.org/site/mail.html"​​.