Tomcat配置SSL认证

大概步骤

1:通过JAVA自带的keytool生成keystore;

2:TOMCAT启用HTTPS,并配置keystore;

3:根据keystore导出客户端的证书文件;

4:客户端导入证书文件;

5:测试

环镜

jdk:1.7.60 

tomcat:7.0.52

客户端和服务端都在本机

======================================

1:用JDK自带的keytool工具生成证书:

keytool -genkey -alias uums -keyalg RSA -keystore d:/keys/uumskeystore

   注:在填CN时:一定要填你的域名,不能是IP地址。tomcat官网手册也有强调,没有域名可以通过修改hosts文件虚拟一个域名,例如:127.0 .0.1  xxx.xxx.com 

2:配置Tomcat启用 HTTPS

    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

 将以上代码注释去掉并修改成:

    <Connector
           protocol="org.apache.coyote.http11.Http11NioProtocol"
           port="443" maxThreads="150"
           scheme="https" secure="true" SSLEnabled="true"
           keystoreFile="D:\keys\uumskeystore" keystorePass="123com"
           clientAuth="false" sslProtocol="TLS"/>

 注:keystorepass是你的keystore的密码,port默认是8443我改成443了,请把server.xml中其它8443端口也改过来。

 如果想把通过http访问的请求都跳转到https访问,需要在conf/web.xml里的尾部加入以下配置

    <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>/*</url-pattern>  
        </web-resource-collection>  
        <user-data-constraint>  
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>  
        </user-data-constraint>  
    </security-constraint> 

 3:导出客户端证书

keytool -export -file d:/keys/uums.crt -alias uums -keystore d:/keys/uumskeystore

 注意:导出客户证书,是根据刚刚生成的keystore来导出的,所以内容必须要与keystore一至。

成功导出uusm.crt证书,可以分发给客户应用的JDK使用了。

4:客户端导入证书

keytool -import -keystore D:/Java/jdk1.7.0_60/jre/lib/security/cacerts -file D:/keys/uums.crt -alias uums

注:如果提示证书存在可以通过下面的操作删除

keytool -delete -trustcacerts -alias uums -keystore D:\Java\jd
k1.7.0_60\jre\lib\security\cacerts

导入成功后,客户端访问服务端就可以通过认证了。

 5:测试略

总结:

1:如果在导入或导出证书出现以下错误:keytool error: java.io.IOException: Keystore was tampered with, or password was incorrect

请输入密码:changeit

2:这里只是为了演示,所以用了JAVA 自带的keytool来生成证书,这些证书,在各大浏览器中是认识不出来的,很多会提示不是安全的证书。如果是商业用途的话,就要花费银子从第三方的证书机构申请。

猜你喜欢

转载自hepx.iteye.com/blog/2090416