作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Symfony 2.8 的新 Guard 身份验证系统,并且我希望允许用户使用两种方法中的任意一种进行身份验证。因此,我为两者实现了 Guard 身份验证器,并像这样配置它们:
security:
firewalls:
my_firewall:
pattern: ^/some-pattern
guard:
authenticators:
- my_first_auth
- my_second_auth
entry_point: my_first_auth
问题是它们都运行,所以如果用户在第一个上成功,但在第二个上失败,他会得到一个禁止的响应。在这种情况下,我希望他能成功。
有没有办法以 OR 关系配置多个 Guard 身份验证器,以便如果第一个成功,则跳过第二个?
最佳答案
最后,我自己手动实现了一个解决方案,利用了如果 getCredentials()
返回 null 则跳过身份验证器的事实。两个验证器的代码如下所示:
class MyAuthenticator extends AbstractGuardAuthenticator {
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage) {
$this->tokenStorage = $tokenStorage;
}
public function getCredentials(Request $request) {
if ($this->isAlreadyAuthenticated()) {
return null; // Skip this authenticator
}
// Get the credentials and continue with this authenticator
}
protected function isAlreadyAuthenticated() {
$token = $this->tokenStorage->getToken();
return $token && $token->isAuthenticated();
}
关于symfony - OR 关系中的多个 Guard 身份验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34599392/
我是一名优秀的程序员,十分优秀!