tomcat实现https安全访问

来源:互联网 发布:张正隆雪白血红知乎 编辑:程序博客网 时间:2024/05/22 13:30

1、生成安全访问的证书
命令行下执行:keytool -genkey -alias tomcat -keyalg RSA -keystore c:\tomcat.keystore
然后按提示输入一些密码等信息:

输入keystore密码:再次输入新密码:您的名字与姓氏是什么?  [Unknown]:  tomcat您的组织单位名称是什么?  [Unknown]:  tomcat您的组织名称是什么?  [Unknown]:  tomcat您所在的城市或区域名称是什么?  [Unknown]:  bj您所在的州或省份名称是什么?  [Unknown]:  bj该单位的两字母国家代码是什么  [Unknown]:  cnCN=tomcat, OU=tomcat, O=tomcat, L=bj, ST=bj, C=cn 正确吗?  [否]:  y输入<tomcat>的主密码        (如果和 keystore 密码相同,按回车):

输入完成后,会在C盘下生成tomcat.keystore文件,这个文件的位置不做要求。


2、配置tomcat,使其支持https连接。
打开tomcat目录下conf/server.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 -->    <!--    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"               maxThreads="150" scheme="https" secure="true"               clientAuth="false" sslProtocol="TLS" />    -->

<connectore>节点的注释去掉,8443修改为你要使用的端口。节点中添加两个属性:keystoreFile=”C:/tomcat.keystore” keystorePass=”123456”
其中,keystoreFile指的是你第一步生成的证书文件位置,keystorePass是你在生成证书时所输入的密码。

通过以上两步操作,基本上就可以使用https访问tomcat连接了,不过我在使用tomcat6配置的时候,启动tomcat报错:

2011-9-8 10:11:20 org.apache.catalina.core.AprLifecycleListener init信息: Loaded APR based Apache Tomcat Native library 1.1.20.2011-9-8 10:11:20 org.apache.catalina.core.AprLifecycleListener init信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].2011-9-8 10:11:20 org.apache.catalina.startup.SetAllPropertiesRule begin警告: [SetAllPropertiesRule]{Server/Service/Connector} Setting property 'keystoreFile' to 'C:\tomcat.keystore' did not find a matching property.2011-9-8 10:11:20 org.apache.catalina.startup.SetAllPropertiesRule begin警告: [SetAllPropertiesRule]{Server/Service/Connector} Setting property 'keystorePass' to 'mvtech' did not find a matching property.2011-9-8 10:11:20 org.apache.catalina.startup.SetAllPropertiesRule begin警告: [SetAllPropertiesRule]{Server/Service/Connector} Setting property 'clientAuth' to 'false' did not find a matching property.2011-9-8 10:11:20 org.apache.coyote.http11.Http11AprProtocol init严重: Error initializing endpointjava.lang.Exception: No Certificate file specified or invalid file format at org.apache.tomcat.jni.SSLContext.setCertificate(Native Method) at org.apache.tomcat.util.net.AprEndpoint.init(AprEndpoint.java:720) at org.apache.coyote.http11.Http11AprProtocol.init(Http11AprProtocol.java:107) at org.apache.catalina.connector.Connector.initialize(Connector.java:1014) at org.apache.catalina.core.StandardService.initialize(StandardService.java:680) at org.apache.catalina.core.StandardServer.initialize(StandardServer.java:795) at org.apache.catalina.startup.Catalina.load(Catalina.java:524) at org.apache.catalina.startup.Catalina.load(Catalina.java:548) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:261) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)2011-9-8 10:11:20 org.apache.catalina.startup.Catalina load严重: Catalina.startLifecycleException:  Protocol handler initialization failed: java.lang.Exception: No Certificate file specified or invalid file format at org.apache.catalina.connector.Connector.initialize(Connector.java:1016) at org.apache.catalina.core.StandardService.initialize(StandardService.java:680) at org.apache.catalina.core.StandardServer.initialize(StandardServer.java:795) at org.apache.catalina.startup.Catalina.load(Catalina.java:524) at org.apache.catalina.startup.Catalina.load(Catalina.java:548) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:261) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)

在网上看到有朋友的解决办法,是找到tomcat的bin目录下有个tcnative-1.dll文件,然后将其删除,重新启动tomcat一切正常。
至此,可以通过https方式访问你的应用了。


但是此时如果你用Http方式访问应用,页面会返回乱码或者空,但是也不报错,为了更好的使用,我们需要在客户输入http时能够自动跳转到Https方式,这就需要第三步,让你的应用强制使用https方式访问了。
打开tomcat下conf/web.xml文件,在最下方的<welcome-file-list>节点之后添加:

    <login-config>       <!-- Authorization setting for SSL -->       <auth-method>CLIENT-CERT</auth-method>       <realm-name>Client Cert Users-only Area</realm-name>       </login-config>      <security-constraint>           <!-- Authorization setting for SSL -->           <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>  

配置完成。

0 0
原创粉丝点击