Categories: Zend framework教程

Zend框架-身份验证

身份验证是任何Web应用程序中最重要且必不可少的功能之一。Zend Framework提供了一个单独的组件来处理身份验证,称为zend-authentication

安装身份验证组件

可以使用以下Composer命令安装身份验证组件。

composer require zendframework/zend-authentication

概念

通常,开发人员编写php函数以根据数据源验证用户详细信息。身份验证完成后,身份验证详细信息将保留用于后续请求。Zend Framework概括了这个概念,并提供了两个类,下面对此进行了解释-

1类Zend \ Authentication \ Adaptor \ AdaptorInterface

此类提供一个单一方法,进行身份验证以编写身份验证逻辑。authenticate方法返回Zend \ Authentication \ Result类的实例。

Result对象保存身份验证状态;如果身份验证成功,则返回身份;如果身份验证失败,则返回错误消息。authenticate接口和结果类的签名如下:

适配器接口

namespace Zend\Authentication\Adaptor; 
public function authenticate() { 
   // code 
}

结果类

namespace Zend\Authentication; 
class Result { 
   public function __construct($code, $identity, array $messages = []); 
}

Zend框架提供了一个默认实现,可以针对数据库,ldap,http基本和摘要凭据进行身份验证。一个适配器进行身份验证,但不会持续未来的任何请求的详细信息。

2类Zend \ Authentication \ AuthenticationService

AuthenticationService是主要组件,它使用已配置的适配器进行身份验证。身份验证完成后,它将保留身份验证详细信息并提供方法,hasIdentity()检查身份是否可用,getIdentity()获取身份验证详细信息和clearIdentity()清除身份验证详细信息。

使用此AuthenticationService的部分代码清单如下-

$adap = new Adapter($username, $password);  
$auth = new AuthenticationService(); 
$result = $auth->authenticate($adap);  
if($result->isValid) { 
   $identity = $auth->getIdentity(); 
} else { 
   // process $result->getMessages() 
}  
// clear 
$auth->clearIdentity();

与授权相关的内容打包为两个单独的模块,分别是– zend-permissions-aclzend-permissions-rbac。zend-permissions-acl基于访问控制列表,而zend-permissions-rbac基于基于角色的访问控制列表。它们提供了ACL和RBAC概念的高级抽象,并有助于编写企业级应用程序。

terry

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

聊聊vue3中的defineProps

在Vue 3中,defineP…

7 天 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

4 周 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

1 月 ago