thinkPHP5.0开发微信小程序登录接口signature验证失败

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014520745/article/details/54345040

我用TP5开发微信小程序登录接口的时候,在校验签名signature那遇到了个坑,一直校验失败,找了很久才发现

原因是:tp的input方法会自动转移html字符,比如’”’转成了 &quote; ,所以导致了失败:

本来的rawData

{"nickName":"小小黑","gender":1,"language":"en","city":"Zhanjiang","province":"Guangdong","country":"CN","avatarUrl":"http://wx.qlogo.cn/mmopen/vi_32/iaFp2syscJNYgalastnQ3bnYt2l4cURSX7p7CeZ7SkNLa32yqBHsXITPibqrJl7z6mYYIibmCLlwtoOwCp0nMibIgA/0"}WYrl3b8xiPuJlOnY/zCXdw==

TP中input方法过滤过的rawData

{"nickName":"小小黑","gender":1,"language":"en","city":"Zhanjiang","province":"Guangdong","country":"CN","avatarUrl":"http://wx.qlogo.cn/mmopen/vi_32/iaFp2syscJNYgalastnQ3bnYt2l4cURSX7p7CeZ7SkNLa32yqBHsXITPibqrJl7z6mYYIibmCLlwtoOwCp0nMibIgA/0"}WYrl3b8xiPuJlOnY/zCXdw==

所以校验方法需要这样写:

$signature2 = sha1(htmlspecialchars_decode($rawData) . $session_key);

if ($signature2 !== $signature) return ret_message("signNotMatch");

后续会给一个完整的登录流程代码(前端+ThinkPHP5.0)

没有更多推荐了,返回首页