CheckSign.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. // 'backend_sign' => $backendSign,
  35. // 'device_no' => $request->input('device_no', ''),
  36. // 'X-Version' => $request->header('X-Version', ''),
  37. // 'package' => $request->input('package', ''),
  38. // ]);
  39. if ($timestamp && time() - $timestamp <= (SysConsts::ONE_HOUR_SECONDS * 10) && $sign == $backendSign) {
  40. // \Log::info('[CheckSign]旧版校验通过');
  41. return true;
  42. } else {
  43. // \Log::info('[CheckSign]旧版校验没有通过');
  44. return false;
  45. }
  46. }
  47. private function newSignPass($request, $key) {
  48. $params = $request->except(['_url', 'device_info']);
  49. $timestamp = $request->post('timestamp', 0);
  50. $sign = $request->post('sign', '');
  51. $backendSign = _sign($params, $key);
  52. // \Log::info('[CheckSign]新版校验sign:', [
  53. // 'front_sign' => $sign,
  54. // 'backend_sign' => $backendSign,
  55. // 'device_no' => $request->input('device_no', ''),
  56. // 'X-Version' => $request->header('X-Version', ''),
  57. // 'package' => $request->input('package', ''),
  58. // ]);
  59. if ($timestamp && time() - $timestamp <= (SysConsts::ONE_HOUR_SECONDS * 10) && $sign == $backendSign) {
  60. // \Log::info('[CheckSign]新版校验通过');
  61. return true;
  62. } else {
  63. // \Log::info('[CheckSign]新版校验没有通过');
  64. return false;
  65. }
  66. }
  67. }