0

我有一个使用 Silex 的项目,而且我真的是这个 symfony 框架的新人。Silex 带有安全插件,我相信它与 symfony 库相同。那么问题是我需要在 login_path 防火墙配置上使用一些路由参数,如下例所示

我这样设置我的路由,cname 将是路由变量,我试图用secured_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']);
    }

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

4

1 回答 1

0

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

于 2013-01-16T12:47:15.123 回答