写在前面:利用了本周一周的时间终于完成了基于tomcat的SSL实现,趁着周末来临,特此总结一下!
1 适用环境:
Tomcat 6.0、jdk1.6、基于第三方CA的配置
2 所需环境安装:
2.1 安装openssl
下载包:openssl-1.0.1d.tar.gz
安装:
tar -xzf openssl-1.0.1d.tar.gz ./config shared make && make install 注:shared (创建共享库)
更新运行时库的配置:
编辑/etc/ld.so.conf,当然也可以直接在/etc/ld.so.conf.d文件夹下再创建一个.conf文件,把/usr/local/ssl/lib拷贝到里面,另外也可以在现有的.conf文件中添加这个路径,反正ld.so.conf中包含了这个文件夹下的所有.conf文件
更新PATH:
修改用户下的.bash_profile,添加路径/usr/local/ssl/bin,执行source .bash_profile
2.2 为tomcat安装apr的支持
如果tomcat没有集成apr,tomcat在启动时apr监听会报出错误:
The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path:
值得apr 注意的是,安装前需要先安装openssl。
在此,我使用的包是apr-1.4.6.tar.gz,apr-util-1.5.2.tar.gz
安装apr
tar zxvf apr-1.4.6.tar.gz cd apr-1.4.6 ./configure make make install
apr默认安装在 /usr/local/apr
安装apr-util
tar zxvf apr-util-1.5.2.tar.gz cd apr-util-1.5.2 ./configure --with-apr=/usr/local/apr make make install
安装tomcat-native
设置环境变量cd /usr/local/tomcat-6.0.36/bin tar zxvf tomcat-native.tar.gz cd tomcat-native-1.1.23-src/jni/native ./configure --with-apr=/usr/local/apr --with-java-home=/usr/java/jdk1.6.0_45 --with-ssl=yes make make install
至此,我们已经安装了openssl和apr,启动tomcat,查看日志,我们可以看到vi /etc/profile # 后面添加以下内容 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib # 使profile生效, source /etc/profile
以上可以看出,openssl和apr都已经成功启动。2013-6-21 11:29:30 org.apache.catalina.core.AprLifecycleListener init 信息: Loaded APR based Apache Tomcat Native library 1.1.23 using APR version 1.4.6. 2013-6-21 11:29:30 org.apache.catalina.core.AprLifecycleListener init 信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 2013-6-21 11:29:31 org.apache.catalina.core.AprLifecycleListener initializeSSL 信息: OpenSSL successfully initialized with version OpenSSL 1.0.1d 5 Feb 2013
3、配置tomcat
编辑conf/server.xml
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on"/>
将SSLEngine改为on,即开启状态。
将默认注释掉的<connector port=8443 ...>的注释释放,并按需修改内部的配置。如:<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" sslProtocol="TLS" SSLVerifyClient="[optional/require/optionalNoCA]" SSLPassword="[私钥密码]" SSLCertificateKeyFile="[CA签发的服务器证书的私钥路径]" SSLCertificateFile="[CA签发的服务器证书路径]" />
重启tomcat,在客户端浏览器安装相应的客户端证书,即可实现https类型的访问。<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" sslProtocol="TLS" SSLVerifyClient="optionalNoCA" SSLPassword="12345678" SSLCertificateKeyFile="/home/costapp/webapp/apache-tomcat-6.0.36/key/serverkey.key" SSLCertificateFile="/home/costapp/webapp/apache-tomcat-6.0.36/key/server.cer" />