简体   繁体   English

超薄框架限制网址

[英]Slim framework restrict url

I have my REST Base APi on Slim Framework. 我在Slim Framework上拥有REST Base APi。 I will need a help in restricting some URL which should only be accessible internally and not exposed public. 我将需要帮助来限制某些URL,这些URL仅可在内部访问,而不能公开访问。 How can i achieve this please. 我怎样才能做到这一点。

/REST/api/v1/getusers -- is publicly available / REST / api / v1 / getusers-公开可用

/REST/api/v1/userinfo -- new api which i want only local access ie other systems on the network can access but not exposed publicly / REST / api / v1 / userinfo-我只希望本地访问的新api,即网络上的其他系统可以访问但不公开显示

Firstly add rka-ip-address-middleware to determine the client's IP address. 首先添加rka-ip-address-middleware来确定客户端的IP地址。 You can install this using composer require akrabat/rka-ip-address-middleware : 您可以使用composer require akrabat/rka-ip-address-middleware安装此composer require akrabat/rka-ip-address-middleware

$app->add(new RKA\Middleware\IpAddress());

Now, create a group and put all restricted routes inside it. 现在,创建一个组并将所有受限制的路由放入其中。 You can then add middleware to the group to ensure that the client IP address is allowed before any of the routes in the group are run: 然后,您可以将中间件添加到组中,以确保在运行组中的任何路由之前允许客户端IP地址:

$app->group('', function () {
    $app->get('/REST/api/v1/userinfo', UserInfoAction::class);

    // other $app->get(), $app->post(), $app->put() etc actions here

})->add(function ($request, $response, $next) {
    // Only allow internal IP addresses
    $allowed = ['127.0.0.1', '192.168.0.1']; // or whatever
    $clientIp = $request->getAttribute('ip_address');

    // Is the client's IP address in the allowed list?
    if (!in_array($clientIp, $allowed)) {
        // Not allowed: return a 401 error
        return $response->withStatus(401);
    }

    // Allowed: continue to action
    return $next($request, $response);
});

I've created a middleware for this: its name is Slim-Restrict-Route and you can find it here . 我为此创建了一个中间件:其名称为Slim-Restrict-Route ,您可以在这里找到它。 It uses the Ip Validator of Respect/Validation and rka-ip-address-middleware . 它使用尊重/验证Ip 验证程序rka-ip-address-middleware

You can register it in this way: 您可以通过以下方式注册:

$app->add(new RKA\Middleware\IpAddress());

$options = array(
  'ip' => '192.*.*.*'
);

$app->get('/api/myEndPoint',function ($req, $res, $args) {
  //Your amazing route code
})->add(new \DavidePastore\Slim\RestrictRoute\RestrictRoute($options));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM