用jdk自带的keytool命令创建一个证书
keytool -genkeypair -alias tomcat -keyalg "RSA" -keystore "coffee.keystore"
功能:
创建一个别名为tomcat的证书,该证书存放在名为coffee.keystore的密钥库中,若coffee.keystore密钥库不存在则创建。
参数说明:
-genkeypair:生成一对非对称密钥;
-alias:指定密钥对的别名,该别名是公开的;
-keyalg:指定加密算法,本例中的采用通用的RAS加密算法;
-keystore:密钥库的路径及名称,不指定的话,默认在操作系统的用户目录下生成一个".keystore"的文件
我的win10 默认会在 C:\Users\coffee\.keystore目录下生成,
指定了以后就会在当前目录下生产一个keystore文件 C:\Users\coffee\Desktop\coffee.keystore
然后配置tomcat的server.xml
<Connector
port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="C:/Users/coffee/Desktop/coffee.keystore" keystorePass="coffee"/>
——————————————————————————
需要注意三个问题
一、alias的密码和密钥库的口令必须一致,否则在tomcat中会报错
Tomcat : java.io.IOException: Keystore was tampered with, or password was incorrect
The password in Tomcat’s “<Connector>” is NOT matched with the password you assigned to your keystore.
File : $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" //...
keystorePass="password here must same with your keystore's password" />
//...
To fix this, make sure both are using same password, and passwords are case sensitive as well :)
二、keytool设置的alias不是"tomcat" 回导致启动tomcat失败12-Nov-2016 13:06:18.187 WARNING [main] org.apache.tomcat.util.net.openssl.OpenSSLContext.init Error initializ ing SSL context
java.lang.NullPointerException
at org.apache.tomcat.util.net.openssl.OpenSSLContext.init(OpenSSLContext.java:276)
at org.apache.tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.java:100)
at org.apache.tomcat.util.net.AbstractJsseEndpoint.initialiseSsl(AbstractJsseEndpoint.java:80)
at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:245)
这个错误是由于keytool设置的alias不是"tomcat"导致的三、你的名字和姓氏
必须输入域名 localhost
并且 访问浏览器的时候 需要访问https://localhost:8443 而不是http