首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >scheb/双因子包:如何使用exclude_pattern进行认证路由?

scheb/双因子包:如何使用exclude_pattern进行认证路由?
EN

Stack Overflow用户
提问于 2016-04-06 13:49:30
回答 1查看 542关注 0票数 2

我正在Symfony3项目中使用Scheb的Symfony3,我想要处理与它不同的exclude_pattern参数,但我不知道如何处理。

通常,unauthenticated用于将exclude_pattern路由排除在双因素身份验证之外,例如调试页或静态内容:

代码语言:javascript
复制
# config/config.yml

scheb_two_factor:
    ...
    exclude_pattern: ^/(_(profiler|wdt)|css|images|js)/

它的行为实现如下:

代码语言:javascript
复制
/* vendor/scheb/two-factor-bundle/Security/TwoFactor/EventListener/RequestListener.php */

public function onCoreRequest(GetResponseEvent $event)
{
    $request = $event->getRequest();

    // Exclude path
    if ($this->excludePattern !== null && preg_match('#'.$this->excludePattern.'#', $request->getPathInfo())) {
        return;
    }

    ...

}

我还想为认证的路由处理,以便在调用它们时可以跳过双因素身份验证。对于身份验证,我指的是access_control节中的security.yml,如下所示:

代码语言:javascript
复制
# app/config/security.yml
security:
    ...
    access_control:
        - { path: ^/test, role: ROLE_USER }

现在,如果我在exclude_pattern下添加了一个经过身份验证的路由,我所得到的就是一个AccessDeniedException,可能是因为包要求将access_decision_manager参数设置为strategy: unanimous

目的是长时间讲,英语不是我的母语,但如果你真的需要知道它,我可以尝试解释。

我用symfony3和symfony2标记了这个问题,因为我使用的是Symfony3.0,但我很确定它在Symfony2.8中是相同的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-06 15:38:09

通过从包中重写选民类,我找到了一个解决方案:

代码语言:javascript
复制
// AppBundle/Security/TwoFactor/Voter.php

namespace AppBundle\Security\TwoFactor;
use Scheb\TwoFactorBundle\Security\TwoFactor\Session\SessionFlagManager;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class Voter extends \Scheb\TwoFactorBundle\Security\TwoFactor\Voter
{


    /**
     * @var string
     */
    protected $excludePattern;

    /**
     * Voter constructor.
     * @param SessionFlagManager $sessionFlagManager
     * @param array $providers
     * @param $excludePattern
     */
    public function __construct(SessionFlagManager $sessionFlagManager, array $providers, $excludePattern)
    {

        parent::__construct($sessionFlagManager, $providers);
        $this->excludePattern = $excludePattern;
    }

    /**
     * @param TokenInterface $token
     * @param mixed          $object
     * @param array          $attributes
     *
     * @return mixed result
     */
    public function vote(TokenInterface $token, $object, array $attributes)
    {

        if ($this->excludePattern !== null && preg_match('#'.$this->excludePattern.'#', $object->getPathInfo()))
        {
            return true;
        }

        parent::vote($token, $object, $attributes);
    }

}
代码语言:javascript
复制
# app/config/services.yml

services:
    ...
    scheb_two_factor.security_voter:
    class: 'AppBundle\Security\TwoFactor\Voter'
    arguments:
        - '@scheb_two_factor.session_flag_manager'
        - ~
        - '%scheb_two_factor.exclude_pattern%'

这样,每当触发GetResponseEvent时,将调用正确的投票者,如果exclude_pattern与路径匹配,则该选民将投票给exclude_pattern

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36453147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档