idtitlesidebar_label
https
HTTPS
HTTPS

为保证网络访问安全,现在大多数企业都会选择使用SSL验证来提高网站的安全性。

所以Forest自然也加入了对HTTPS的处理,现在支持单向认证和双向认证的HTTPS请求。

单向认证

如果访问的目标站点的SSL证书由信任的Root CA发布的,那么您无需做任何事情便可以自动信任

  1. public interface Gitee {
  2. @Request(url = "https://gitee.com")
  3. String index();
  4. }

Forest的单向验证的默认协议为SSLv3,如果一些站点的API不支持该协议,您可以在全局配置中将ssl-protocol属性修改为其它协议,如:TLSv1.1, TLSv1.2, SSLv2等等。

  1. forest:
  2. ...
  3. ssl-protocol: TLSv1.2

全局配置可以配置一个全局统一的SSL协议,但现实情况是有很多不同服务(尤其是第三方)的API会使用不同的SSL协议,这种情况需要针对不同的接口设置不同的SSL协议。

  1. /**
  2. * 在某个请求接口上通过 sslProtocol 属性设置单向SSL协议
  3. */
  4. @Get(
  5. url = "https://localhost:5555/hello/user",
  6. sslProtocol = "SSL"
  7. )
  8. ForestResponse<String> truestSSLGet();

在一个个方法上设置太麻烦,也可以在 @BaseRequest 注解中设置一整个接口类的SSL协议

  1. @BaseRequest(sslProtocol = "TLS")
  2. public interface SSLClient {
  3. @Get("https://localhost:5555/hello/user")
  4. String testSend();
  5. }

双向认证

若是需要在Forest中进行双向验证的HTTPS请求,也很简单。

在全局配置中添加keystore配置:

  1. forest:
  2. ...
  3. ssl-key-stores:
  4. - id: keystore1 # id为该keystore的名称,必填
  5. file: test.keystore # 公钥文件地址
  6. keystore-pass: 123456 # keystore秘钥
  7. cert-pass: 123456 # cert秘钥
  8. protocols: SSLv3 # SSL协议

接着,在@Request中引入该keystoreid即可

  1. @Request(
  2. url = "https://localhost:5555/hello/user",
  3. keyStore = "keystore1"
  4. )
  5. String send();

另外,您也可以在全局配置中配多个keystore

  1. forest:
  2. ...
  3. ssl-key-stores:
  4. - id: keystore1 # 第一个keystore
  5. file: test1.keystore
  6. keystore-pass: 123456
  7. cert-pass: 123456
  8. protocols: SSLv3
  9. - id: keystore2 # 第二个keystore
  10. file: test2.keystore
  11. keystore-pass: abcdef
  12. cert-pass: abcdef
  13. protocols: SSLv3
  14. ...

随后在某个具体@Request中配置其中任意一个keystoreid都可以