简体   繁体   English

如何将“拒绝访问”重定向到Silex的登录页面

[英]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_pathuse_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 ? httpform

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 会话过期+ Silex时重定向到登录页面 - Redirect to login page when session expires + silex 登录成功后重定向+ Silex - Redirect after login succes + Silex 访问被拒绝后重定向时如何获取引用页面? - How to get referrer page when there's a redirect after access denied? php如何在找不到页面或访问被拒绝时重定向,或者没有权限 - php how to redirect when page not found or access denied, or no permission 当我访问SimpleSAMLphp核心登录页面时,如何重定向到authsource中第一个配置的登录模块? - How to redirect to the first configured login module in authsource when I access the SimpleSAMLphp core login page? 用户无需登录即可直接访问文件时,如何重定向登录页面 - How to redirect login page when the user to access the file directly without login Laravel,如果没有登录,如何重定向到不同的登录页面 - Laravel, How to redirect to different login page if not login 访问被拒绝403重定向 - access denied 403 redirect 如果匿名用户无权访问 URL,如何将匿名用户重定向到自定义页面而不是登录页面? - How do I redirect an anonymous user to custom page instead of the login page if they do not have access to a URL? 通过访问权限被拒绝的Symfony2身份验证用户被重定向到登录页面 - Symfony2 authenticated users are redirected to login page on Access Denied exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM