SeretKeyMiddleware.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: TABLE ME
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-30 14:59
  10. // +----------------------------------------------------------------------
  11. namespace app\api\middleware;
  12. use app\Request;
  13. use library\exceptions\AuthException;
  14. use library\interfaces\MiddlewareInterface;
  15. use library\services\UtilService;
  16. class SeretKeyMiddleware implements MiddlewareInterface
  17. {
  18. public function handle(Request $request, \Closure $next)
  19. {
  20. // $this->checkSign($request);//暂时不用
  21. return $next($request);
  22. }
  23. /**
  24. * 检查数据是否正常
  25. * @param $secret_key
  26. */
  27. private function checkSign(Request $request) {
  28. $checkHeader = ['deviceId','deviceType','fromApp','fromPlat','mobileType','version','timestamp'];
  29. $sign = $request->header('sign','');
  30. $config = [
  31. 'token' => $request->header('token',''),
  32. 'deviceId' => $request->header('deviceId',''),
  33. 'deviceType' => $request->header('deviceType',0),
  34. 'fromApp' => $request->header('fromApp',''),
  35. 'fromPlat' => $request->header('fromPlat',''),
  36. 'mobileType' => $request->header('mobileType',''),
  37. 'version' => $request->header('version',''),
  38. 'timestamp' => $request->header('timestamp',''),
  39. ];
  40. foreach ($checkHeader as $v) {
  41. if(empty($config[$v])) {
  42. // throw new AuthException('签名参数出错!', 1001);
  43. }
  44. }
  45. // $calSign = $this->makeSign($config);
  46. if(strtoupper($sign) != $calSign){
  47. // throw new AuthException('签名错误!加密规则:' . $this->makeSignStr($config), 1002);
  48. }
  49. }
  50. private function makeSignStr(array $config)
  51. {
  52. $string = 'token=' . $config['token'] . ',deviceId=' . $config['deviceId'] . ',deviceType=' . $config['deviceType'] . ',fromApp=' . $config['fromApp'] . ',fromPlat=' . $config['fromPlat'] . ',mobileType=' . $config['mobileType'] . ',version=' . $config['version'] . ',timestamp=' . $config['timestamp'];
  53. $salt = config('app.appSalt');
  54. return $salt . $string;
  55. }
  56. private function makeSign(array $config)
  57. {
  58. $string = 'token=' . $config['token'] . ',deviceId=' . $config['deviceId'] . ',deviceType=' . $config['deviceType'] . ',fromApp=' . $config['fromApp'] . ',fromPlat=' . $config['fromPlat'] . ',mobileType=' . $config['mobileType'] . ',version=' . $config['version'] . ',timestamp=' . $config['timestamp'];
  59. $salt = config('app.appSalt');
  60. $sign = strtoupper(md5($salt . $string));
  61. return $sign;
  62. }
  63. }