出于安全考虑,项目中用dubbo发布的rest服务需要增加https安全访问,原本服务是跑在内置tomcat,内置tomcat对于https设置不支持,只能折腾一番,调整各种配置,部署到外置tomcat发布服务,大致过程:生成安全证书、设置tomcat服务参数、设置服务参数,详细过程见下文。
证书生成
- 进入到jdk目录,执行命令:
keytool -genkey -alias mykeystore -keyalg RSA -validity 30000 -keypass mypass -keystore E:/mykeystore.keystore
- 证书查看:
keytool -list -v -keystore E:/mykeystore.keystore
Tomcat配置
配置Tomcat,打开tomcat目录下/conf/server.xml,修改如下:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
修改为:
<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="443" />
<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"/>-->
修改为:
<Connector port="443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" keystoreFile="/opt/tomcat/mykeystore.keystore" keystorePass="mypass"/>
注释:keystoreFile是证书文件的位置,keystorePasss是证书的密码,在证书文件生成过程时设置的。
<!--
<Connector port="8009" enableLookups="false" protocol="AJP/1.3" redirectPort="8443" />
-->
修改为:
<Connector port="8009" enableLookups="false" protocol="AJP/1.3" redirectPort="443" />
dubbo服务配置
- 服务配置
pom.xml文件,修改为war方式发布:
<packaging>war</packaging>
provider.xml文件,修个服务配置:
<dubbo:protocol name="rest" port="443" contextpath="dop" server="tomcat"/>
<dubbo:protocol name="http" port="8889" />
- web.xml配置
打开项目webapp下面的web.xml,在文件末尾增加:
<security-constraint>
<web-resource-collection>
<web-resource-name >TLS</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>