首先實現服務器使用自簽名證書,不要求客戶端使用證書的配置。
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訪問。