[英]How to redirect “Access denied” to login page in Silex
This is my firewall code 这是我的防火墙代码
$app['security.firewalls']=[
'secured'=>[
'pattern' => '/',
'anonymous' => true,
'http'=>true,
'form' => array('login_path' => '/login', 'check_path' => '/secured/login_check'),
'logout' => array('logout_path' => '/secured/logout', 'invalidate_session' => true),
'users'=>$users
]
];
$app['security.access_rules']=[
["^/admin", "ROLE_ADMIN"]
];
When users access admin page without role admin, how to redirect them to login page? 当用户在没有角色管理员的情况下访问管理员页面时,如何将他们重定向到登录页面?
I have test with no access rules in admin controller code: 我测试了管理员控制器代码中没有访问规则的情况:
if($app['security.authorization_checker']->isGranted('ROLE_ADMIN')){
// ...
// ...
// ...
}
else return $app->redirect($app->url('login'));
But the problem when I use this method is that it will redirect to homepage instead of previous page. 但是,当我使用此方法时,问题在于它将重定向到主页而不是上一页。 How can I make login page to redirect to previous page instead of homepage after successful login check?
成功进行登录检查后,如何使登录页面重定向到上一页而不是首页?
Try to add always_use_default_target_path
and use_referer
parameters to security config: 尝试将
always_use_default_target_path
和use_referer
参数添加到安全配置中:
$app['security.firewalls']=[
'secured'=>[
...
'form' => array(
'login_path' => '/login',
'check_path' => '/secured/login_check',
'always_use_default_target_path' => false,
'use_referer' => true
),
...
]
];
Why do you use 2 entry points for login? 为什么要使用2个入口点进行登录?
http
and form
? http
和form
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.