SignAuthMiddleware.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace app\admin\middleware;
  3. use Webman\Http\Request;
  4. use Webman\Http\Response;
  5. use Webman\MiddlewareInterface;
  6. /**
  7. * 接口签名中间件
  8. * Class SignAuthMiddleware
  9. * @package app\admin\middleware
  10. */
  11. class SignAuthMiddleware implements MiddlewareInterface
  12. {
  13. public function process(Request $request, callable $handler) : Response
  14. {
  15. try {
  16. $sign = $request->header('sign');
  17. if(!$sign){
  18. throw new \Exception('签名不能为空!', 500);
  19. }
  20. $token = $request->header('Authorization', '');
  21. $data = $request->all();
  22. $string = KsortAscll($data);
  23. $mySign = strtoupper(md5($string . $token . config('app')['sign_key']));
  24. if($mySign != $sign)
  25. {
  26. throw new \Exception('签名错误!' . $string . $token . config('app')['sign_key'], 500);
  27. }
  28. return $handler($request);
  29. }catch (\Exception $exception){
  30. throw new \Exception($exception->getMessage(), $exception->getCode());
  31. }
  32. }
  33. }