以下文章提供了 CakePHP 授权的概述。 CakePHP 是一个开源工具,它以可插拔的方式提供 Auth 组件来执行我们的任务。 Auth组件用于提供身份验证和授权对象。换句话说,我们可以说它是两者的组合,用于根据我们的要求确定用户的授权和身份验证。身份验证意味着确定用户凭据并验证这些凭据,例如用户名和密码。另一方面,授权意味着根据用户凭据和用户提供的其他信息对用户进行验证。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
什么是CakePHP授权?
如您所知,添加了两个“最近”(不是最近)的新模块来管理 CakePHP 应用程序中的身份验证和授权的思想。从长远来看,身份验证和授权是在控制器层使用 AuthComponent 进行监督的。随着任务的发展,这两件事通常会变得错综复杂,使 AuthComponent 成为同时管理许多元素的令人困惑的类。
这些新模块背后的第一个想法是重构 AuthComponent 并创建显式层来处理:
确认:你是谁?
批准:你说你被允许了吗?
我们将利用特定模型研究本文中的授权想法:我们应该设想一些游戏应用程序,用户将在其中监督锦标赛。用户希望举办新的锦标赛并通过具有众多附属关系的锦标赛会员资格加入锦标赛。除非欢迎客户参加比赛,否则客户不会参加锦标赛。锦标赛的玩家可以欢迎不同的用户来玩。
如何检查CakePHP授权?
现在让我们看看如何检查 CakePHP 授权,如下所示:
在我们各自的应用程序中实现授权中间件后,我们可以检查授权。这是因为中间件包装了每个请求的身份。
现在让我们看看如何检查单个资源的授权,如下所示:
他们可以帮助您真正查看对单个资产的批准。通常这是一个 ORM 物质或应用领域对象。
您的政策给出了决定批准选择的理由:
代码:
// Fetch identity from each and every request $user = $this->request->getAttribute('identity'); // Checking authorization on $sample if ($user->can('delete', $sample)) { // Do delete operation }
现在让我们看看如何应用范围条件,如下所示:
每当您需要对各种项目进行批准检查(例如分页查询)时,您通常需要获取当前客户接触的记录。这个模块将这个想法作为“范围”来实现。
范围方法允许您“范围”查询或结果集并返回刷新的纲要或问题对象:
代码:
// Fetch the identity from each and every request $specified user = $this->request->getAttribute('identity'); $Sql_query = $specified fuser->ApplyScopeTo('index', $Sql_query);
授权组件可用于监管活动中以顺利批准,从而提高失望的豁免率。
创建 CakePHP 授权
现在让我们看看如何在 CakePHP 中创建授权,示例如下:
首先,我们需要了解需要考虑哪些参数,如下:
确认是区分合适客户的最常见方式。 CakePHP 支持三种验证。
- FormAuthenticate:它允许您确认给定结构化 POST 信息的客户端。通常,这是客户端输入数据的登录结构。这是默认的验证策略。
- BasicAuthenticate:它允许您确认客户端正在使用基本 HTTP 验证。
- DigestAuthenticate:它允许您确认客户端正在使用摘要 HTTP 验证。
首先,我们需要配置routes.php文件,如下:
代码:
<?php use Cake\Core\Plugin; use Cake\Routing\RouteBuilder; use Cake\Routing\Router; Router::defaultRouteClass('DRoute'); Router::scope('/', function (RouteBuilder $routes) { $routes->connect('/auth',['controller'=>'Auth','action'=>'index']); $routes->connect('/login',['controller'=>'Auth','action'=>'login']); $routes->connect('/logout',['controller'=>'Auth','action'=>'logout']); $routes->fallbacks('DRoute'); }); Plugin::routes();
之后,我们需要创建一个controller.php文件,并编写如下代码:
代码:
<?php namespace App\Controller; use Cake\Controller\Controller; use Cake\Event\Event; use Cake\Controller\Component\AuthComponent; class DemoController extends Controller { public function initialize() { parent::initialize(); $this->loadComponent('RequestHandler'); $this->loadComponent('Flash'); $this->loadComponent('Auth', [ 'authenticate' => [ 'Form' => [ 'fields' => [ 'username' => 'userid', 'password' => 'userpass' ] ] ], 'loginAction' => [ 'controller' => 'Authexs', 'action' => 'login' ], 'loginRedirect' => [ 'controller' => 'Authexs', 'action' => 'index' ], 'logoutRedirect' => [ 'controller' => 'Authexs', 'action' => 'login' ] ]); } public function BFilter(Event $eventt) { $this->Auth->allow(['index','view']); $this->set('loggedIn', $this->Auth->specified user()); } }
现在创建 authcontrollr.php 文件并编写以下代码:
代码:
<?php namespace App\Controller; use App\Controller\AppController; use Cake\ORM\TableRegistry; use Cake\Datasource\ConnectionManager; use Cake\Event\Eventt; use Cake\Auth\DefaultPasswordHasher; class AuthController extends AppController { var $component = array('Auth'); public function index(){ } public function login(){ if($this->request->is('post')) { $specified_user = $this->Auth->identify(); if($user){ $this->Auth->setUser($specified_user); return $this->redirect($this->Auth->redirectUrl()); } else $this->Flash->errormsg('Entered username and password is wrong'); } } public function logout(){ return $this->redirect($this->Auth->logout()); } }
最后,我们需要创建一个登录模板来查看结果,如下。
<?php echo $this->Form->create(); echo $this->Form->control('UserID'); echo $this->Form->control('Userpass'); echo $this->Form->button('Submit'); echo $this->Form->end(); ?>
说明:
这里我们创建一个模板来查看结果。执行上述代码后,我们将得到以下屏幕。
在这里我们可以提供用于登录的用户凭据。
我们必须创建另一个用于注销的 PHP 文件并编写以下代码。
代码:
<?php echo $this->Html->link('logout',[ "controller" => "Auth","action" => "logout" ]); ?>
After executing the above code, we will get the following screen.
CakePHP Authorization Installing
Now let’s see how we can install authorization in CakePHP as follows:
First, we need to load the plugin by using the following statement as follows:
Code:
$this-> addPlugin('Authorization');
After that, we need to enable all authorization plugins by importing the following class as follows:
Code:
use Authorization\AuthorizationService; use Authorization\AuthorizationServiceInterface; use Authorization\AuthorizationServiceProviderInterface; use Authorization\Middleware\AuthorizationMiddleware; use Authorization\Policy\OrmResolver;
After creating a policy as per our requirement, we also need to fix add and edit action as per our requirement. The requirement mentioned above we can achieve through coding.
Conclusion
From the above article, we have taken in the essential idea of the CakePHP authorization and see the representation and example of the CakePHP authorization. Finally, we saw how and when we use the CakePHP authorization from this article.
以上是CakePHP授权的详细内容。更多信息请关注PHP中文网其他相关文章!

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考虑使用AttActAcks.s.s.4)

会话再生是指在用户进行敏感操作时生成新会话ID并使旧ID失效,以防会话固定攻击。实现步骤包括:1.检测敏感操作,2.生成新会话ID,3.销毁旧会话ID,4.更新用户端会话信息。

PHP会话对应用性能有显着影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

phpientifiesauser'ssessionusessessionSessionCookiesAndSessionIds.1)whiwSession_start()被称为,phpgeneratesainiquesesesessionIdStoredInacookInAcookInamedInAcienamedphpsessidontheuser'sbrowser'sbrowser.2)thisIdAllowSphptptpptpptpptpptortoreTessessionDataAfromtheserverMtheserver。

PHP会话的安全可以通过以下措施实现:1.使用session_regenerate_id()在用户登录或重要操作时重新生成会话ID。2.通过HTTPS协议加密传输会话ID。3.使用session_save_path()指定安全目录存储会话数据,并正确设置权限。

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver CS6
视觉化网页开发工具

Dreamweaver Mac版
视觉化网页开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版