在cakefilter中的cakephp auth getuser [英] cakephp auth getuser in beforefilter

查看:97
本文介绍了在cakefilter中的cakephp auth getuser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(请参阅

in my cakephp app with custom auth implementation (see here) i would like to check the auth of a user in the beforefilter method and if he/she is not authenticated i would like to manually render an error page and quit. My problem here is that it seems the auth object gets only filled with data AFTER the action call to an action which require auth. i would need to access auth data in my beforefilter function. how to achieve this? if i try to access it auth->user() it returns NULL, loggedIn() returns always false (because there is no data, makes sense)

推荐答案

我偶然发现了这个问题,建议的解决方案对我不起作用.

I stumbled upon this question and the proposed solution did't work for me.

在Cakephp 3中的beforeFilter中获取身份验证信息的正确方法是调用

The proper way to get auth information in beforeFilter in Cakephp 3 is to call

$this->Auth->config('checkAuthIn', 'Controller.initialize');

在加载auth组件之后.这在无状态身份验证方案(例如令牌身份验证)中特别有用.

right after loading the auth component. This is especially useful in stateless authentication scenarios (e.g. token authentication).

然后您可以通过调用

$user = $this->Auth->user();

请参阅文档 http://book.cakephp.org/3.0/en/controllers/components/authentication.html#deciding-when-to-run-authentication

这篇关于在cakefilter中的cakephp auth getuser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆