路由导出
代码位置:vendorlaravelframeworksrcIlluminateFoundationConsoleRouteListCommand.php
protected function getRouteInformation(Route $route) {
$data = $this->filterRoute([
'host' => $route->domain(),
'method' => implode('|', $route->methods()),
'uri' => $route->uri(),
'name' => $route->getName(),
'action' => $route->getActionName(),
'middleware' => $this->getMiddleware($route),
]);
$path = "./routeRewrite.log";
$fp = fopen($path, "a+");
$before = $route->uri(); //加密前
$after = encrypt_aes($before, AES_KEY); //加密后
$middleware = $this->getMiddleware($route);
$array = explode(",", $middleware);
$str = "";
foreach ($array as $v) {
$str = $str . "'{$v}',";
}
$str = substr($str, 0, strlen($str) - 1);
$route = "Route::post('{$after}', '\{$route->getActionName()}')->middleware({$str});";
fwrite($fp, $route . "
");
fclose($fp);
return $data;
}
中间件
class AfterMiddleware {
public function handle($request, Closure $next) {
if ( $request->isMethod('post') ) {
$str = file_get_contents("php://input");
$key = AES_KEY;
$data = decrypt_aes($str, $key);
$body = json_decode($data, true);
$request->merge($body);
$response = $next($request);
if ($response->status() == 200) {
$data = encrypt_aes($response->content(), $key);
$response->setContent($data);
}
return $response;
}
return $next($request);
}
}
加密解密
if (!function_exists('encrypt_aes')) {
function encrypt_aes($data, $key) {
$data = openssl_encrypt($data, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
return base64_encode($data);
}
}
if (!function_exists('decrypt_aes')) {
function decrypt_aes($data, $key) {
$encrypted = base64_decode($data);
return openssl_decrypt($encrypted, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
}
}