非安全传输协议前提下,Open API安全协议设计

来源:互联网 发布:ios开发 耗电量优化 编辑:程序博客网 时间:2024/05/21 22:24

本文考虑:在不使用安全传输协议的前提下,Open API调用的安全问题。

    • 角色定义
    • 处理流程
      • 调用方消息发送流程
      • 发布者消息接收流程
      • 调用结果返回流程
    • 代码设计
      • 调用方代码设计
      • 发布者代码设计

作者:刘海龙
微博:[http://weibo.com/liuhailong2008]
博客:[http://blog.csdn.net/stationxp]

角色定义

  • 发布者:Open API的发布者。
  • 调用方:Open API的调用者。

处理流程

调用方消息发送流程

  1. 生成一个UUID,称为_seed
  2. _seed用自己的私钥签名,得到_sign,提供给发布者认证身份。
  3. _seed用发布者的公钥加密,得到_key
  4. 使用_seed对消息_msg对称加密,得到密文_msgx
  5. _sign``_key``_msgx拼接,进行Base64压缩(可选),得到_body
  6. 发送_body,执行API调用。

发布者消息接收流程

  1. 得到_body,拆分为_sign_key_msgx
  2. 通过调用着uri到注册库中找到调用者公钥_invkerpk
  3. 使用_invkerpk解密_sign,得到_seed1
  4. 使用自己对私钥揭秘_key,得到_seed2
  5. 比对_seed1_seed2,如果一致,确认得到_seed;否则处理过程结束。
  6. 使用_seed_msgx解密,得到明文消息。

调用结果返回流程

  1. 返回结果为预先设定的消息代码。
  2. 采用安全方式传输,则使用调用者的公钥加密。

代码设计

调用方代码设计

interface Invoker{    void setEndPoint(InvokerEndPoint endPoint);    /**    * 通过调用EndPoint实现。    */    Response get(String api,Object…params);    Response post(String api,Object…params); } interface InvokerEndPoint{    Response invoke(Request request); } /** 装饰模式 */ interface SecurityInvokerEndPoint{    Response invoke(Request request); }

发布者代码设计

 interface OpenApiFilter{    void setEndPoint(ExportEndPoint endPoint);    /**    * 通过调用EndPoint实现。    */    HttpServletResponse process(HttpServletRequest request); } interface ExportEndPoint{    Response export(Request request); } /** 装饰模式 */ interface SecurityExportEndPoint{    Response export(Request request); } interface ExportHandler{    Response handle(Object...args); }
0 0
原创粉丝点击