服务端数据安全
发布于 6 年前 作者 fanglong 6991 次浏览 来自 问答

类似问题:https://developers.weixin.qq.com/blogdetail?action=get_post_info&docid=000e4676ff01c068de16ee13956000&highline=服务端安全

  1. 服务端提供数据api目前我差到可以通过发送用户的 signature和rawData 到服务端进行shd1进行签名对比,参考文档,https://developers.weixin.qq.com/miniprogram/dev/api/signature.html.目前已经实现

  2. 小程序代码中发送request 每次都需要在header里面加上相关数据发送到服务器.例如:

    wepy.request({
            url: 'http://localhost:64606/test/auth',
            header: {
              auth_token: auth.token,
              auth_signature: authInfo.signature,
              auth_rawData: encodeURI(authInfo.rawData)
            },
            data: {},
            success: res => {
              console.log(res);
            }
          });

    但如何保证这几个变量全局有效,难道每次调用之前都调用下wx.getUserInfo,在callback回调执行业务请求么? 目前我使用wepy框架app.js中代码如下,其他页面都是先进行sendChek(function(){}) 回调类似上面的业务代码的

    sendCheck(cb) {
        wepy.checkSession({
          success: function() {
            console.log('session_key 未过期,并且在本生命周期一直有效');
            this.getUserInfo(cb);
          },
          fail: function() {
            // session_key 已经失效,需要重新执行登录流程
            wepy.login({
              success: function(res) {
                if (res.code) {
                  wepy.request({
                    url: 'http://localhost:64606/login/get_session',
                    data: {
                      code: res.code
                    },
                    success: res => {
                      if (res.data.token) {
                        wepy.setStorageSync('user-token', res.data);
                        this.getUserInfo(cb);
                      }
                    }
                  });
                } else {
                  console.log('登录失败!' + res.errMsg);
                }
              }
            }); // 重新登录
          }
        });
      }
      getUserInfo(cb) {
        const that = this;
        if (this.globalData.userInfo) {
          cb && cb(this.globalData.userInfo, this.globalData.authInfo);
        }
        wepy.getUserInfo({
          success(res) {
            that.globalData.authInfo = {
              signature: res.signature,
              rawData: res.rawData
            };
            that.globalData.userInfo = res.userInfo;
            cb && cb(that.globalData.userInfo, that.globalData.authInfo);
          }
        });
      }

目前我的疑问是是否有其他简单的写法优化这种机制.找了几个开源项目看都好像没有涉及到数据安全的处理

回到顶部