繁体   English   中英

如何通过Symfony Security在login_path / login_check上使用路由参数

[英]How to use Route Parameter on login_path / login_check with Symfony Security

我有一个使用Silex的项目,并且我真的是这个symfony框架的新手。 Silex带有安全性插件,我相信它与symfony libs相同。 因此,问题在于我需要在login_path防火墙配置中使用一些路由参数,如下例所示

我这样设置路由,cname将是路由变量,我正在尝试用来自secure_area模式的cname变量替换{cname},

  $app->match('/{cname}/dashboard/users','Dashboard\Controller\DashboardController::userAction')->method('GET|POST')->bind('dashboard_users');

这个想法是用路由变量替换“ {cname}”。

$app->register(new Silex\Provider\SecurityServiceProvider());

$app['security.firewalls'] = array(
                                'secured_area' => array(
                                    'pattern' => '^/(\w+)/dashboard/',
                                    'form' => array('login_path' => '/{cname}/login', 
                                                    'check_path' => '/{cname}/dashboard/login_check',
                                                     'default_target_path' => '/{cname}/dashboard/home'
                                                    ),
                                    'users' => $app->share(function () use ($app) {
                                                    return new Dashboard\Service\UserProvider($app['db']);
                                                }),

                                    'logout' => array('logout_path' => '/{cname}/dashboard/logout',
                                                      'target' => '/'),

                                ),
                            );

我已经尝试过了,但是它不起作用,然后我在4个核心文件AbstractAuthenticationListener,FormAuthenticationEntryPoint,DefaultAuthenticationSuccessHandler,LogoutListener中放置了一些代码补丁,所以我只用了几行代码来替换其中的“ {cname}”类,

   on AbstractAuthenticationListener class,
     protected function requiresAuthentication(Request $request)
        {
            /* PATCH */
            if($route_params = $request->attributes->get("_route_params")){
                foreach($route_params as $key => $val){
                   $this->options['check_path'] = str_replace("{".$key."}", $val, $this->options['check_path']);
                }
            }
            /**/
            return $this->httpUtils->checkRequestPath($request, $this->options['check_path']);
        }

    On FormAuthenticationEntryPoint Class
    public function start(Request $request, AuthenticationException $authException = null)
        {
            /* PATCH */
            if($route_params = $request->attributes->get("_route_params")){
                foreach($route_params as $key => $val){
                   $this->loginPath = str_replace("{".$key."}", $val, $this->loginPath);
                }
            }
            /**/


            if ($this->useForward) {
                $subRequest = $this->httpUtils->createRequest($request, $this->loginPath);

                return $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
            }

            return $this->httpUtils->createRedirectResponse($request, $this->loginPath);
        }

    On DefaultAuthenticationSuccessHandler
    protected function determineTargetUrl(Request $request)
        {
            /* PATCH */
            if($route_params = $request->attributes->get("_route_params")){
                foreach($route_params as $key => $val){
                   $this->options['default_target_path'] = str_replace("{".$key."}", $val, $this->options['default_target_path']);
                   $this->options['login_path'] = str_replace("{".$key."}", $val, $this->options['login_path']);
                }
            }
            /**/

            if ($this->options['always_use_default_target_path']) {
                return $this->options['default_target_path'];
            }

            if ($targetUrl = $request->get($this->options['target_path_parameter'], null, true)) {
                return $targetUrl;
            }

            if (null !== $this->providerKey && $targetUrl = $request->getSession()->get('_security.'.$this->providerKey.'.target_path')) {
                $request->getSession()->remove('_security.'.$this->providerKey.'.target_path');

                return $targetUrl;
            }

            if ($this->options['use_referer'] && ($targetUrl = $request->headers->get('Referer')) && $targetUrl !== $this->httpUtils->generateUri($request, $this->options['login_path'])) {
                return $targetUrl;
            }

            return $this->options['default_target_path'];
        }

on LogoutListener
protected function requiresLogout(Request $request)
    {
        /* PATCH */
        if($route_params = $request->attributes->get("_route_params")){
            foreach($route_params as $key => $val){
               $this->options['logout_path'] = str_replace("{".$key."}", $val, $this->options['logout_path']);
            }
        }
        /**/

        return $this->httpUtils->checkRequestPath($request, $this->options['logout_path']);
    }

我知道这不是正确的方法。 如果有任何适当的方法可以做到这一点,我真的很期待。

如果像这样修改代码,您可能只想扩展修改后的类。 Silex\\Application\\Pimple (服务容器)的实例,它使您可以动态地重新定义或扩展现有服务。 我自己还没有尝试过,但是您很可能可以替换防火墙的入口点(以及其他已更改的类)。

暂无
暂无

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

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