如何保护Lithium php RESTful API?

时间:2012-12-07 07:13:42

标签: php rest oauth lithium http-digest

我使用Lithium php框架创建了一个RESTful应用程序,现在我的问题是如何保护它?

是否存在使用锂框架的OAUTH或HTTP摘要式身份验证的现有代码?

2 个答案:

答案 0 :(得分:2)

虽然我不确定你在寻找什么样的安全性......

有锂内置的安全性,你可以看到两个简短的教程,让你去这里:

“简单身份验证”教程中介绍了基础知识......您需要:

  • 跟踪用户的数据库
  • 通过Auth
  • 启动config/bootstrap.php
  • 设置会话&验证适配器

然后,这取决于您是否要通过表格或其他方法进行认证。

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; });

?>

答案 1 :(得分:2)

感谢您编辑您的问题以实际询问具体内容。请参阅以下内容:

相关问题