ExternalSignCheck.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Middleware;
  3. use Webman\MiddlewareInterface;
  4. use Webman\Http\Response;
  5. use Webman\Http\Request;
  6. class ExternalSignCheck implements MiddlewareInterface
  7. {
  8. public function process(Request $request, callable $next): Response
  9. {
  10. // Access to files beginning with. Is prohibited
  11. if (strpos($request->path(), '/.') !== false) {
  12. return response('<h1>403 forbidden</h1>', 403);
  13. }
  14. /** @var Response $response */
  15. $response = $next($request);
  16. // Add cross domain HTTP header
  17. /*$response->withHeaders([
  18. 'Access-Control-Allow-Origin' => '*',
  19. 'Access-Control-Allow-Credentials' => 'true',
  20. ]);*/
  21. return $response;
  22. }
  23. /**
  24. * 外部通用 判断签名是否正确.
  25. */
  26. public function handle($request, Closure $next)
  27. {
  28. $timestamp = !empty($request->get('timestamp'))?$request->get('timestamp'):'';
  29. $sign = !empty($request->get('sign'))?$request->get('sign'):'';
  30. $response = $next($request);
  31. return $response;
  32. }
  33. public function sign($params, $key)
  34. {
  35. $data = $params;
  36. //签名步骤一:按字典序排序参数
  37. ksort($data);
  38. $buff = "";
  39. foreach ($data as $k => $v) {
  40. if ($v != null && $k !== "sign" && $v !== "" && !is_array($v)) {
  41. $buff .= $k . "=" . $v . "&";
  42. }
  43. }
  44. $buff = trim($buff, "&");
  45. //签名步骤二:在string后加入KEY
  46. $string = $buff . "&key=" . $key;
  47. // \Log::info('Seconds_string:'.$string.'Seconds_key:'.$key);
  48. //签名步骤三:MD5加密
  49. $string = md5($string);
  50. //签名步骤四:所有字符转为大写
  51. $result = strtoupper($string);
  52. return $result;
  53. }
  54. }