Authentication ZF2-init或在每个模块控制器中调用的东西
我有一个名为Backend的模块,在这个模块中,我想检查除了Backend\u登录页面之外的所有页面上的有效身份验证。我该怎么做?我试图将它添加到Backend/Module.php中的onBootstrap中,但结果表明,在我的其他模块中也会调用它。。。这当然不是我想要的 那我该怎么做呢Authentication ZF2-init或在每个模块控制器中调用的东西,authentication,zend-framework2,Authentication,Zend Framework2,我有一个名为Backend的模块,在这个模块中,我想检查除了Backend\u登录页面之外的所有页面上的有效身份验证。我该怎么做?我试图将它添加到Backend/Module.php中的onBootstrap中,但结果表明,在我的其他模块中也会调用它。。。这当然不是我想要的 那我该怎么做呢 提前谢谢 要获得有关zf2身份验证的明确信息,您可以按照以下步骤操作: 适配器验证 数据库表验证 LDAP验证 摘要身份验证…这些都是不同的方法这里是数据库表身份验证的示例: 在每个控制器的操作中,如果需要
提前谢谢 要获得有关zf2身份验证的明确信息,您可以按照以下步骤操作: 适配器验证 数据库表验证 LDAP验证 摘要身份验证…这些都是不同的方法这里是数据库表身份验证的示例: 在每个控制器的操作中,如果需要用户身份验证,则应如下所示:
use Zend\Authentication\Result;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\AdapterInterface;
use Zend\Db\Adapter\Adapter as DbAdapter;
use Zend\Authentication\Adapter\DbTable as AuthAdapter;
public function login($credential)
{
$bcrypt = new Bcrypt();
$user = new User();
$auth = new AuthenticationService();
$user->exchangeArray($credential);
$password = $user->password;
$data = $this->getUserTable()->selectUser($user->username);
if (!$data){
$message = 'Username or password is not correct!';
}
elseif($auth->getIdentity() == $user->username){
$message = 'You have already logged in';
}
elseif($bcrypt->verify($password, $data->password)){
$sm = $this->getServiceLocator();
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$authAdapter = new AuthAdapter(
$dbAdapter,
'user',
'username',
'password'
);
$authAdapter -> setIdentity($user->username) -> setCredential($data->password);
$result = $auth->authenticate($authAdapter);
$message = "Login succesfull.Welcome ".$result->getIdentity();
} else {
$message = 'Username or password is not correct';
}
return new ViewModel(array("message" =>$message));
}
像这样,在每个操作中,您都可以检查它是否经过身份验证
我曾经遇到过类似的问题,并在onBootstrap函数中的Module.php中解决了这个问题。试试这个,它对我有用:
class Module {
// white list to access with being non-authenticated
//the list may contain action names, controller names as well as route names
protected $whitelist = array('login');
//....
public function onBootstrap($e){
$app = $e->getApplication();
$em = $app->getEventManager();
$sm = $app->getServiceManager();
$list = $this->whitelist;
$auth = new AuthenticationService();
$em->attach(MvcEvent::EVENT_ROUTE, function($e) use ($list, $auth) {
$match = $e->getRouteMatch();
// No route match, this is a 404
if (!$match instanceof RouteMatch) {
return;
}
// Route is whitelisted
$action = $match->getParam('action');
if (in_array($action, $list) ) {
return;
}
// User is authenticated
if ($auth->hasIdentity()){
return;
}
// the user isn't authenticated
// redirect to the user login page, as an example
$router = $e->getRouter();
$url = $router->assemble(array(
'controller' => 'auth',
'action'=>'login'
), array(
'name' => 'route_name',
));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
}, -100);
}
}
或者你可以看到
希望它能帮上忙。几乎满足了您的需求。
class Module {
// white list to access with being non-authenticated
//the list may contain action names, controller names as well as route names
protected $whitelist = array('login');
//....
public function onBootstrap($e){
$app = $e->getApplication();
$em = $app->getEventManager();
$sm = $app->getServiceManager();
$list = $this->whitelist;
$auth = new AuthenticationService();
$em->attach(MvcEvent::EVENT_ROUTE, function($e) use ($list, $auth) {
$match = $e->getRouteMatch();
// No route match, this is a 404
if (!$match instanceof RouteMatch) {
return;
}
// Route is whitelisted
$action = $match->getParam('action');
if (in_array($action, $list) ) {
return;
}
// User is authenticated
if ($auth->hasIdentity()){
return;
}
// the user isn't authenticated
// redirect to the user login page, as an example
$router = $e->getRouter();
$url = $router->assemble(array(
'controller' => 'auth',
'action'=>'login'
), array(
'name' => 'route_name',
));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
}, -100);
}
}