实例讲解Apache+tomcat+ssl配置

1/5/2008来源:Java教程人气:7417

    我的运行环境:Windows2003 Server SP4 + J2SDK1.5.0 + Apache2.0.54 + Tomcat5.5.9

    **预备软件:
    1、安装 Apache 2.0.54
    2、Tomcat 5.5.14
    3、Jk2连接器(mod_jk2.so)
 
    一:配置Apache和tomcat

    Apache安装在d:\Apache2 下,监听端口 80;
    Tomcat在D:\Tomcat51 下,监听端口 8080;
    两者都以windows 2000服务进行安装。

    将mod_jk2.so复制到d:\Apache2\modules目录下:
 
    1.编辑d:\Apache2\conf\httpd.conf文件,在LoadModule那一段代码中添加:
    LoadModule jk2_module modules/mod_jk2.so
 
    2.将AddDefaultCharset ISO-8859-1 改为 AddDefaultCharset GB2312 使Apache自动支持中文显示;
 
    3.在“虚拟主机配置代码段”中添加:(此段配置可以根据你的实际情况进行修改)
  <VirtualHost *:80>
    ServerAdmin linvsfen◎163.com
    DocumentRoot "d:/ Tomcat51/webapps/jsp-examples
    ServerName localhost
    ErrorLog logs/error_log
    CustomLog logs/access_log common
  </VirtualHost>

    **编写workers2.PRoperties文件并加入d:\Apache2\conf目录,内容如下:
   
  # Usually commented out on prodUCtion environments
[logger.apache2]
file=”D:/Apache2/logs/error.log”
level=ERROR
# Provide the basic config needed
[config]
file=D:/Apache2/conf/workers2.properties
debug=1
# Provide the location of shm file on the Apache web server
[shm]
file=D:/Apache2/conf/jk2.shm
size=1000000

[channel.socket:localhost:8009]
port=8009
host=localhost
#define the worker
[ajp13:localhost:8009]
channel=channel.socket:localhost:8009
# Map the Tomcat examples webapp to the Web server uri space
[uri:/jsp-examples/*]
[uri:/*.jsp]

    **编写一个jsp文件(sessiontest.jsp),放在d:\Tomcat51\webapps\jsp-examples 下;

    重新启动以后,启动apache和tomcat,访问http://localhost/jsp-examples/sessiontest.jsp可以访问。

    二.Tomcat配置上ssl步骤:

    1.    生成服务器证书server02.jks和客户端证书dwp.p12

    2、把server02.jks放到conf目录下,

    打开d:\ tomcat51\conf\server.xml 文件,

    然后修改server.xml文件,将注释掉的内容的注释去掉和增加证书存放的位置:如下:
<Connector     port="8443" maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" disableUploadTimeout="true"
               acceptCount="100" scheme="https" secure="true"
               clientAuth="true" sslProtocol="TLS"
               keystoreFile="conf\server02.jks"     keystorePass="123456"
              truststoreFile="conf\server02.jks" truststorePass="123456"/>
    注:server02.jks是证书存贮文件

    3. 安装客户端证书dwp.p12

    3. 修改Tomcat配置,使其自动启用SSL
   
    打开d:\test\tomcat51\webapps\jsp-examples\WEB_INF\web.xml 文件,在security-constraint 标签下,增加如下内容:
    <user-data-constraint>
      <description>
         Constrain the user data transport for the whole application
      </description>
      <transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
 注释掉<auth-constraint>标签下的内容
<auth-constraint>
         <!-- Anyone with one of the listed roles may access this area -->
         <role-name>tomcat</role-name>
     <role-name>role1</role-name>
      </auth-constraint>
在web-resource-collection 标签下,增加如下内容:
      <url-pattern>/*</url-pattern>
    重新启动Apache和tomcat,
 
    访问 http://localhost/jsp-examples/sessiontest.jsp是可是访问的,这是系统提示用户出示证书,启用SSL加密通道,SSL建立后,返回结果。URL变为https://localhost:8443/jsp-examples/sessiontest.jsp