zoukankan      html  css  js  c++  java
  • laravel路由导出和参数加密

    路由导出

    代码位置: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);
        }
    }
    
  • 相关阅读:
    VMware Workstation的三种网络连接方式
    sql:unix下的sql操作
    linux脚本: makefile以及链接库
    unix shell: ksh fundamental(Korn Shell)
    linux c: core dump
    linux命令:scp
    Eclipse更改默认工作目录的方法
    linux: 可重入函数与不可重入函数
    linux环境 :Linux 共享库LIBRARY_PATH, LD_LIBRARY_PATH 与ld.so.conf
    linux命令:Linux命令大全
  • 原文地址:https://www.cnblogs.com/HappyTeemo/p/15476013.html
Copyright © 2011-2022 走看看