opb/slim-basic-auth

适用于Slim PHP框架的HTTP基本认证中间件

0.2.2 2015-02-04 19:43 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:16:06 UTC


README

这是Slim的HTTP基本认证中间件插件。主要特性:

  • 保护路径及其所有子路径。例如,将设置保护 /admin,也会保护 /admin/foo/admin/bar/baz,但不包括 /foo

  • 提供一个接口 AuthCheckerInterface,让您实现以检查用户名/密码组合与您自己的用户数据库。以下是一个示例。

安装

通过composer安装

{
    "require": {
        "opb/slim-basic-auth": "dev-master"
    }
}

使用方法

HttpBasicAuth 中间件类通过两个参数实例化:一个必填的 AuthCheckerInterface 实现和一个可选的选项数组。目前支持的两个选项是匹配的 path 和可选的 realm。以下示例展示了如何实现。

// MyAuthClass - implementing the required AuthCheckerInterface

class MyAuthClass implements \Slim\Middleware\AuthCheckerInterface
{
	// only function required by the interface
	public function checkCredentials($username, $password)	{
		// interact with your own auth system
		// do some stuff and return true if authorised, false if not	
	}
}

// the rest of your Slim app, adding in the middleware

$app = new \Slim\Slim();

$authChecker = new MyAuthCLass;

$app->add(new \Slim\Middleware\HttpBasicAuth($authChecker, array(
	'path' => '/api', // optional, defaults to '/'
	'realm' => 'Protected API' // optional, defaults to 'Protected Area'
)));