简体   繁体   English

default_target_path如何在Silex防火墙中运行?

[英]How does the default_target_path work in a Silex Firewall?

I have setup a firewall in Silex as follows: 我在Silex中设置了防火墙,如下所示:

$this -> register(new SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'login' => array(
            'pattern' => '^/admin/login$'
        ),
        'admin' => array(
            'pattern' => '^/admin.*$',
            'form' => array(
                'login_path' => '/admin/login',
                'check_path' => '/admin/security/validate',
                'default_target_path' => "/admin",
                'always_use_default_target_path' => true
            ),
            'logout' => array(
                'logout_path' => '/admin/security/logout'
            ),
            'users' => $app -> share(function () use ($app) {
                return new \Turtle\Providers\UserProvider($app);
            })
        )
    ),
    'security.access_rules' => array(
        array('^/admin.*$', 'ROLE_ADMIN')
    )
));

This works in that when I hit a page in the 'admin' area I get redirected to my login page. 这样做是因为当我点击'admin'区域中的页面时,我被重定向到我的登录页面。 However I have started to do some authorization in my custom AuththenticationSuccess handler. 但是我已经开始在我的自定义AuththenticationSuccess处理程序中进行一些授权。 I want to use the built in method determineTargeUrl to redirect on success but it keeps redirecting to '/'. 我想使用内置方法determineTargeUrl重定向成功但它仍然重定向到'/'。

After some debugging I have found that the options in the object that the method uses has the following: 经过一些调试后,我发现该方法使用的对象中的选项具有以下内容:

array (size=5)
  'always_use_default_target_path' => boolean false
  'default_target_path' => string '/' (length=1)
  'login_path' => string '/login' (length=6)
  'target_path_parameter' => string '_target_path' (length=12)
  'use_referer' => boolean false

Clearly this is not what I have set in my firewall. 显然,这不是我在防火墙中设置的。 It is my understanding that this should match what it is in the firewall that I have used when accessing the system. 据我所知,这应该与我在访问系统时使用的防火墙中的内容相匹配。 The URL I used was ' http://localhost/admin '. 我使用的URL是' http:// localhost / admin '。

So how do I make it so that the options I have set in my firewall appear in the object so that I can use the determineTargetUrl? 那么我如何制作它以便我在防火墙中设置的选项出现在对象中,以便我可以使用determineTargetUrl?

Thanks lots, Russell 谢谢,拉塞尔

It looks like your problem is that your login route is inside your secured area. 看起来您的问题是您的登录路线位于您的安全区域内。 You've defined your access rule as ^/admin.*$ . 您已将访问规则定义为^/admin.*$ This means that any route starting with /admin requires ROLE_ADMIN including your login route. 这意味着以/admin开头的任何路由都需要ROLE_ADMIN包括您的登录路由。 To fix this you need to remove security from your login route. 要解决此问题,您需要从登录路由中删除安全性。

Add a new access rule above your admin rule. 在管理规则上方添加新的访问规则。

'security.access_rules' => array(
    array('^/admin/login$', 'IS_AUTHENTICATED_ANONYMOUSLY'),
    array('^/admin.*$', 'ROLE_ADMIN')
)

Edit : After reading your question again, I may have misunderstood you. 编辑 :再次阅读你的问题后,我可能误解了你。 it sounds like you can successfully log in but are redirected to the wrong place after a successful login. 听起来您可以成功登录,但在成功登录后会被重定向到错误的位置。 Is that correct? 那是对的吗? If so I will remove this answer. 如果是这样,我会删除这个答案。

This was my mistake. 这是我的错。 I am using custom AuthenticationSuccess and AuthenticationFailure handlers and i neglected to pass any options into them when I was declaring them: 我正在使用自定义AuthenticationSuccess和AuthenticationFailure处理程序,当我声明它时,我忽略了将任何选项传递给它们:

    $app['security.authentication.success_handler.admin'] = $app -> share(function() use ($app) {
        return new AuthenticationSuccessHandler($app['security.http_utils'], array(), $app);
    });

    $app['security.authentication.failure_handler.admin'] = $app -> share(function() use ($app) {
        return new AuthenticationFailureHandler($app['kernel'], $app['security.http_utils'], array(), $app);
    });

So the options array that is used in determineTargetUrl on authentication success was empty and thus had default values. 这样在使用的选项阵列determineTargetUrl在认证成功是空的,因此具有默认值。

By adding an array of options to the AuthenticationSuccessHandler it works. 通过向AuthenticationSuccessHandler添加一组选项,它可以正常工作。 This is OK as each custom handler is linked to a different firewall. 这没关系,因为每个自定义处理程序都链接到不同的防火墙。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM