eclipse配置https的方法有很多种,这里介绍一种我使用的
第一步,首先准备xxx.keystore。
使用JDK自带的keytool工具来生成证书:
keytool.exe在java的路径下的bin中
例如我的C:\Program Files\Java\jdk-11.0.2\bin
用命令行打开。输入keytool -genkeypair -alias “tomcat” -keyalg “RSA” -keystore “d:\tomcat.keystore” (注意命令行先切换到bin目录,以及最后的路径自己指定)
这里密码是隐藏的,即输入不会显示,但已经输入了
名字那块要写你的域名,本机即是localhost
第二步
配置eclipse中的server
在web.xml中加入以下代码,在</welcome-file-list>
后面
<security-constraint>
<web-resource-collection>
<web-resource-name>securedapp</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee><!-- 如果你希望关闭 SSL ,只需要将 CONFIDENTIAL 改为 NONE 即可。 -->
</user-data-constraint>
</security-constraint>
然后配置server.xml,去掉Connector 的注释,然后在certificateKeystorePassword输入你的密码,在 certificateKeystoreFile中输入路径,一般比较推荐放在tomcat下的conf中。(注意此时我们还没有将tomcat.keystore放入该文件中)因为这个依靠映射的tomcat比较难找,如果找的到的话可以直接放。找不到的话就启动下eclipse中的tomcat,让他报错,依靠报错将tomcat.keystore拷贝文件。
<Connector SSLEnabled="true" maxThreads="150" port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol">
<SSLHostConfig>
<Certificate certificateKeystoreFile="conf/tomcat.keystore" certificateKeystorePassword="123456" type="RSA" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS"/>
</SSLHostConfig>
</Connector>
(像我的C:\Users\xx\Documents\workspace-spring-tool-suite.RELEASE.metadata.plugins\org.eclipse.wst.server.core\tmp0\conf)够难找吧。
配置好以上内容,即可重启tomcat
因为https默认端口是443,所以如果想要修改到默认端口可以把8443改成443
由于我们是本地测试连接,所以会出现不安全的提示,想要去掉这种不安全,还需要自行配置。有时候完成上述配置后不会直接出现这界面,会提示不安全,直接点开继续连接即可