基于HTTPS的访问是安全的,原因不详谈了,我们谈下在tomcat下如何使用https
环境:
Server version: Apache Tomcat/6.0.35 Server built: Nov 28 2011 11:20:06 Server number: 6.0.35.0 OS Name: Windows XP OS Version: 5.1 Architecture: x86 JVM Version: 1.6.0_21-b06 JVM Vendor: Sun Microsystems Inc.
1.基于jdk的keytool工具生成key,保存路径:%tomcat_home%\tomcat.key
keytool -genkey -alias tomcat -keyalg RSA -keystore D:\tools\tomcat6.0.35\tomcat.key
步骤中会有一系列输入回车,但是需要记住keystorePass,我的为abc2013
2.修改%tomcat_home%\conf的server.xml增加如下:
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="D:\tools\tomcat6.0.35\tomcat.key" keystorePass="abc2013"/>
上面有生成的key路径和pass直接写入即可
3.使用方式:
有两种使用方式,一个是对tomcat的全局应用,一种是基于单个应用
基于tomcat的全局应用:
修改%tomcat_home%\conf的web.xml,指定想要采用的url,下面为对/login采样https访问
<login-config> <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>/login/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint>
也就是会匹配所有的/login/*的请求
基于单个应用
修改%app_project%\WEB-INF的web.xml,指定想要采用的url,下面为对/ab/*采样https访问
<login-config> <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>/ab/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint>
注意:
采样https的端口默认是8443,如果需要路由需要单独配置