简体   繁体   English

如何保护Lithium php RESTful API?

[英]How to secure a Lithium php RESTful API?

I have created a RESTful apps using Lithium php framework and now my question is how to secure it? 我使用Lithium php框架创建了一个RESTful应用程序,现在我的问题是如何保护它?

Is there any existing code for OAUTH or HTTP Digest Authentication that uses lithium framework? 是否存在使用锂框架的OAUTH或HTTP摘要式身份验证的现有代码?

While I'm not sure what sort of security you are looking for ... 虽然我不确定你在寻找什么样的安全性......

There is built in security for Lithium, you can see two short tutorials to get you going here: 有一个内置的锂安全,你可以看到两个简短的教程,让你去这里:

The basics are covered in the "Simple Authentication" tutorial ... you'll need: “简单身份验证”教程中介绍了基础知识......您需要:

  • A database to keep track of you users 用于跟踪用户的数据库
  • Bootstrap Auth via config/bootstrap.php Bootstrap Auth通过config/bootstrap.php
  • Setup Sessions & Auth adapters 设置会话和验证适配器

Then it depends on if you are going to do authenticaion via forms, or by some other method. 那么这取决于你是否要通过表格或其他方法进行认证。

The turtorials will show you how to setup a form, but you can also "secure" the route (url) that is being requested via the config/routes.php file like so ... turtorials将向您展示如何设置表单,但您也可以通过config/routes.php文件“保护”正在请求的路由(url),如此...

<?php

use lithium\net\http\Router;
use lithium\core\Environment;

use lithium\security\Auth;

// check if the user is logged in
$user = Auth::check('default'); 

// these routes are not behind a login
Router::connect('/login', 'Sessions::add');
Router::connect('/logout', 'Sessions::delete');

if ($user && $user["user"] == "admin") {

    // these two routes will only work if a user is authenticated.
    Router::connect('/{:controller}/{:action}/{:args}.{:type}');
    Router::connect('/{:controller}/{:action}/{:args}');
}

// redirect the user to a login if no other routes match
Router::connect('/{:args}', array(), function($request) { header('Location: /login/url/'.str_replace('/','*',$request->url)); exit; });

?>

Thanks for editing your question to actually ask something specific. 感谢您编辑问题以实际询问具体内容。 Please see the following: 请参阅以下内容:

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

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