AllowCrossDomain.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  7. // +----------------------------------------------------------------------
  8. // | Author: liu21st <liu21st@gmail.com>
  9. // +----------------------------------------------------------------------
  10. declare (strict_types = 1);
  11. namespace app\admin\middleware;
  12. use Closure;
  13. use think\Config;
  14. use think\Request;
  15. use think\Response;
  16. use Firebase\JWT\Key;
  17. use Firebase\JWT\JWT;
  18. /**
  19. * 跨域请求支持
  20. */
  21. class AllowCrossDomain
  22. {
  23. protected $cookieDomain;
  24. protected $jwtOpen;
  25. protected $jwtSalt;
  26. protected $header = [
  27. 'Access-Control-Allow-Origin' =>'*',
  28. // 'Access-Control-Allow-Origin' =>'127.0.0.1',
  29. 'Access-Control-Allow-Credentials' => 'true',
  30. 'Access-Control-Max-Age' => 1800,
  31. 'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, DELETE',
  32. 'Access-Control-Allow-Headers' => 'Accept,token, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With',
  33. ];
  34. public function __construct(Config $config)
  35. {
  36. $this->cookieDomain = $config->get('cookie.domain', '');
  37. $this->jwtOpen = $config->get('develop.jwt_open', '');
  38. $this->jwtSalt = $config->get('develop.jwt_salt', '');
  39. }
  40. /**
  41. * 允许跨域请求
  42. * @access public
  43. * @param array
  44. * @param Closure $next
  45. * @param array $header
  46. * @return Response
  47. */
  48. public function handle($request, Closure $next, ? array $header = [])
  49. {
  50. $header = !empty($header) ? array_merge($this->header, $header) : $this->header;
  51. if (!isset($header['Access-Control-Allow-Origin'])) {
  52. $origin = $request->header('origin');
  53. if ($origin && ('' == $this->cookieDomain || strpos($origin, $this->cookieDomain))) {
  54. $header['Access-Control-Allow-Origin'] = $origin;
  55. } else {
  56. $header['Access-Control-Allow-Origin'] = '*';
  57. }
  58. }
  59. if($this->jwtOpen && $request->isPost()){
  60. $input = $request->getInput();
  61. $input = str_replace("\"",'',$input);
  62. if(count(explode('.',$input))==3){
  63. $input = $request->param('jwtData');
  64. $salt = hash('sha256',$this->jwtSalt);
  65. $data = JWT::decode($input, new Key($salt, 'HS256'),array('HS256'));
  66. $data = json_decode(json_encode($data,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),true);
  67. }
  68. }elseif($request->isGet()){
  69. $input = $request->get();
  70. $data = $input;
  71. }else{
  72. $input = $request->getInput();
  73. $data = !empty($input)?json_decode($input,true):[];
  74. }
  75. if(!empty($data)){
  76. if($request->isPost()){
  77. $request->withPost($data);
  78. }elseif($request->isGet()){
  79. $request->withGet($data);
  80. }
  81. }
  82. return $next($request)->header($header);
  83. }
  84. }