操作场景
本文档指导您通过 TSF 的微服务网关配置 HTTPS 访问。
操作步骤
步骤1:准备 SSL 证书
您可以通过以下任一种方式准备一个 SSL 证书:
方式一:购买(通过证书授权机构购买)
方式二:自己生成(通过 keytool 或 openssl 工具生成)
步骤2:配置证书信息
说明
如果网关类型为 Envoy 网关,证书需要放在部署 Envoy 网关应用的时候配置的路径下。
1. 将 SSL 证书放到 resources下 的 https 文件夹(可自定义名称)中:


2. 在配置文件 application.yml 中设置证书的基本信息。如下:
# ssl证书相关配置server:ssl:#启用sslenabled: true#证书地址key-store: classpath:https/keystore.p12#证书密码key-store-password: 123456#证书类型key-store-type: JKS
说明
SpringCloudZuul 与 SpringCloudGateway 配置方式一致,是因为 SpringBoot 服务使用 SSL-HTTPS 的通用方式。
步骤3:访问验证
zuul application.yml 配置如下:
server:port: 18000ssl:enabled: truekey-store: classpath:https/keystore.p12key-store-password: 123456key-alias: tomcathttpskey-store-type: JKSspring:application:name: zuul-democloud:consul:host: 127.0.0.1port: 8500discovery:heartbeat:enabled: truettl-value: 5ttl-unit: spreferIpAddress: trueribbon:ReadTimeout: 10000ConnectTimeout: 5000zuul:max:host:connections: 500host:socket-timeout-millis: 10000connect-timeout-millis: 5000routes:api-v1:path: /v1/**serviceId: provider-demostripPrefix: trueapi-v2:path: /v2/**url: http://127.0.0.1:18081stripPrefix: trueapi-v3:path: /*/v3/**serviceId: provider-demostripPrefix: trueignored-patterns: /**/ignore/**hystrix:command:default:execution:timeout:enabled: trueisolation:thread:timeoutInMilliseconds: 30000
HTTPS 访问网关成功的显示如下:


scg application.yml 配置如下:
server:port: 8080ssl:enabled: truekey-store: classpath:https/keystore.p12key-store-password: 123456key-alias: tomcathttpskey-store-type: JKSerror:include-exception: truespring:application:name: sc-gatewaycloud:gateway:discovery:locator:enabled: truelower-case-service-id: falsehttpclient:connectTimeout: 10responseTimeout: 20sroutes:- id: api-v1uri: lb://PROVIDER-DEMOorder: 0predicates:- Path=/provider/**filters:- StripPrefix=1# consul:# host: 127.0.0.1# port: 8500# enabled: true# scheme: HTTPlogging:file: /var/logs/${spring.application.name}/${spring.application.name}.loglevel:root: INFOcom.tencent.tsf: INFO
HTTPS 访问网关成功的显示如下:


常见问题
问题描述:当使用自签名证书时,Chrome 会拦截请求,并展示 ERR_CERT_INVALID,旧版本可以选择跳过,继续访问,但是新版本 Chrome 不允许继续,且提示:您的连接不是私密连接攻击者可能会试图从 XX.XX.XX.XX 窃取您的信息(例如:密码、通讯内容或信用卡信息)。
解决方案:在 Chrome 该页面上,输入 thisisunsafe,即可进行访问,用来验证 HTTPS 配置是否成功。线上环境不推荐使用自签名证书。