1、阿里云申请免费证书
登陆之后,直接在产品和服务器中搜索【证书】
2、购买证书
3、配置域名
4、下载证书
上面txt文件内容是密码
5、配置网关ssl
server:
port: 8080
ssl:
key-store: classpath:4510662_domain.pfx
key-store-type: PKCS12
enabled: true
key-store-password: 4HBaK4t0
- 把证书复制到resources目录下
- bootstrap.yml配置ssl,如上配置
6、如果出现如下错误
则要在pom.xml添加一个插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<!-- 过滤后缀为pkcs12、jks的证书文件 -->
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pfx</nonFilteredFileExtension>
<nonFilteredFileExtension>jks</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
7、此时访问网关会报如下错误
io.netty.handler.codec.DecoderException: io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: 485454502f312e3120343030200d0a5472616e736665722d456e636f64696e673a206368756e6b65640d0a446174653a205468752c2031372053657020323032302030383a32333a323320474d540d0a436f6e6e656374696f6e3a20636c6f73650d0a0d0a300d0a0d0a
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:459)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:265)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:628)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:563)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:480)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:442)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
at java.lang.Thread.run(Thread.java:748)
Caused by: io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: 485454502f312e3120343030200d0a5472616e736665722d456e636f64696e673a206368756e6b65640d0a446174653a205468752c2031372053657020323032302030383a32333a323320474d540d0a436f6e6e656374696f6e3a20636c6f73650d0a0d0a300d0a0d0a
at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1178)
at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1243)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:489)
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:428)
... 15 common frames omitted
是因为用户使用https访问网关,网关也会使用https访问各个微服务,解决方法是在bootstrap.yml配置把https转成http:
spring:
cloud:
gateway:
routes:
- id: router1
predicates:
- Host=sample.com
- Path=/**
uri: lb:http://sample-web
lb:[overwite scheme]://sample-web
这行加上http就行了,原理查看LoadBalancerClientFilter.class
源码
8、配置tomcat
-
解压已下载保存到本地的Tomcat证书文件。
-
解压后您将看到文件夹中有2个文件,您可为两个证书文件重命名。
-
- 证书文件(domain name.pfx):以.pfx为后缀或文件类型。
- 密码文件(pfx-password.txt):以.txt为后缀或文件类型。
-
在Tomcat安装目录下新建cert目录,将解压的证书和密码文件拷贝到cert目录下。
-
修改配置文件server.xml,并保存。
-
文件路径:Tomcat安装目录/conf/server.xml
-
在标签内添加:
<Connector port="443"
protocol="HTTP/1.1"
SSLEnabled="true"
scheme="https"
secure="true"
keystoreFile="D:/web-server/apache-tomcat-common/cert/domain.pfx"
keystoreType="PKCS12"
keystorePass="password"
clientAuth="false"
SSLProtocol="TLSv1+TLSv1.1+TLSv1.2"
ciphers="TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA256"/>
- 可选: 配置web.xml文件,开启HTTP强制跳转HTTPS。在文件</welcome-file-list>后添加以下内容:
<login-config>
<!-- Authorization setting for SSL -->
<auth-method>CLIENT-CERT</auth-method>
<realm-name>Client Cert Users-only Area</realm-name>
</login-config>
<security-constraint>
<!-- Authorization setting for SSL -->
<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>