首先实现服务器使用自签名证书,不要求客户端使用证书的配置。
1、生成自签名证书
%JAVA_HOME%/jdk/bin/keytool -genkey -alias tomcat -keyalg RSA -keysize 1024 -validity 360 -keystore "D:/Program File/tomcat/conf/server.keystore"
注意:这里使用的keystore的密钥和alias(tomcat)的密钥必须相同,否则会出错。虽然在后面的配置中有alias的密钥,但是依然还是出错。
2、修改tomcat/conf/server.xml
<Connector port="8181" protocol="org.apache.coyote.http11.Http11Protocol" connectionTimeout="10000" redirectPort="8181" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="D:/Program Files/apache-tomcat-6.0.30/conf/server.keystore" keystorePass="server" />
即启用ssl,并把相关参数修改正确。
如此即可使用https访问。但是发现也可以使用http访问。
3、配置只允许https访问的目录
修改 tomcat/webapps/.../WEB-INF/web.xml
其中...代表web应用的文件夹
<security-constraint> <display-name>Example Security Constraint</display-name> <web-resource-collection> <web-resource-name>Protected Area</web-resource-name> <url-pattern>/jsp/security/protected/*</url-pattern> <user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
其中url-pattern标签指定目录,标签<user-data-constraint>指定该目录下文件需要https访问。