CheckSign.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Consts\SysConsts;
  4. use Closure;
  5. class CheckSign
  6. {
  7. /**
  8. * Handle an incoming request.
  9. * changeLog: 2022-07-13
  10. * - 新版签名不使用device_info参与
  11. * - 为了和已经上架的快应用保持兼容,现在,两种签名方法通过一种就可以认为是签名通过
  12. *
  13. * @param \Illuminate\Http\Request $request
  14. * @param \Closure $next
  15. * @return mixed
  16. */
  17. public function handle($request, Closure $next)
  18. {
  19. $key = 'a!A&AFRWT65Nb3NlklezUiqHyQAA@Z8M';
  20. \Log::info('[CheckSign]请求的request参数:', $request->all());
  21. if($this->oldSignPass($request, $key) || $this->newSignPass($request, $key)) {
  22. return $next($request);
  23. } else {
  24. return response()->error('QAPP_SIGN_ERROR');
  25. }
  26. }
  27. private function oldSignPass($request, $key) {
  28. $params = $request->except(['_url']);
  29. $timestamp = $request->post('timestamp', 0);
  30. $sign = $request->post('sign', '');
  31. $backendSign = _sign($params, $key);
  32. \Log::info('[CheckSign]旧版校验sign:', [
  33. 'front_sign' => $sign,
  34. 'backent_sign' => $backendSign,
  35. 'device_no' => $request->input('device_no', '')
  36. ]);
  37. if ($timestamp && time() - $timestamp <= (SysConsts::ONE_HOUR_SECONDS * 10) && $sign == $backendSign) {
  38. \Log::info('[CheckSign]旧版校验通过');
  39. return true;
  40. } else {
  41. \Log::info('[CheckSign]旧版校验没有通过');
  42. return false;
  43. }
  44. }
  45. private function newSignPass($request, $key) {
  46. $params = $request->except(['_url', 'device_info']);
  47. $timestamp = $request->post('timestamp', 0);
  48. $sign = $request->post('sign', '');
  49. $backendSign = _sign($params, $key);
  50. \Log::info('[CheckSign]新版校验sign:', [
  51. 'front_sign' => $sign,
  52. 'backent_sign' => $backendSign,
  53. 'device_no' => $request->input('device_no', '')
  54. ]);
  55. if ($timestamp && time() - $timestamp <= (SysConsts::ONE_HOUR_SECONDS * 10) && $sign == $backendSign) {
  56. \Log::info('[CheckSign]新版校验通过');
  57. return true;
  58. } else {
  59. \Log::info('[CheckSign]新版校验没有通过');
  60. return false;
  61. }
  62. }
  63. }