【问题标题】:Symfony authentication api keys lost sessionsSymfony 身份验证 API 密钥丢失会话
【发布时间】:2014-04-08 07:10:48
【问题描述】:

我正在按照本指南http://symfony.com/doc/current/cookbook/security/api_key_authentication.html 通过 API KEY 进行身份验证。

在调用最后一个方法身份验证令牌后,我可以登录用户,但它不会保留会话。

return new PreAuthenticatedToken(
        $user,
        $apiKey,
        $providerKey,
        $user->getRoles()
    );

我还尝试使用以下方法进行身份验证:

$user = new User("Test", null, array("ROLE_USER"));
$token = new UsernamePasswordToken($user, null, 'secured_area', $user->getRoles());
$this->get('security.context')->setToken($token);
$this->get('session')->set('_security_secured_area',serialize($token));

在这两种情况下,我只能让用户登录一个页面,而不是所有项目。

安全就像指南:

firewalls:
    secured_area:
        pattern: ^/testproject
        stateless: true
        simple_preauth:
            authenticator: apikey_authenticator

谢谢

[更新]

$user = new ApiKeyUserProvider();
$user = $user->loadUserByUsername("111111");

$apiKeyProvider = $this->get('apikey_authenticator');
$token = $apiKeyProvider->authenticateToken("111111",$user);
$this->get('security.context')->setToken($token);
$this->get('session')->set('_security_secured_area',serialize($token));

loadUserByUsername 就像 symfony 指南。

谢谢

【问题讨论】:

  • 对于第二种方法,您可能需要使用您的令牌PreAuthenticatedToken 而不是UsernamePasswordToken

标签: php api symfony authentication


【解决方案1】:

我遇到了同样的问题,发现 apiKey 被保存为 Token 凭据。如果您不设置以下内容,这些将被删除:

security:
    erase_credentials:    false

【讨论】:

    【解决方案2】:

    如果您正确遵循本指南,身份验证应该会自动进行。您只需将参数apikey 附加到请求的末尾即可。

    IE:

    www.mydomain.com/testproject/get-some-random-data?apikey=vdsadbrfadbfda
    

    Symfony 将在您的防火墙中接收此请求并通过 ApiKeyAuthenticator 运行它。

    如果您想将该身份验证保存在会话中,以便后续请求不需要apikey,那么您可以在security.yml 中将stateless 配置更改为false

    所以改变:

    firewalls:
        secured_area:
            pattern: ^/testproject
            stateless: true
            simple_preauth:
                authenticator: apikey_authenticator
    

    收件人:

    firewalls:
        secured_area:
            pattern: ^/testproject
            stateless: false
            simple_preauth:
                authenticator: apikey_authenticator
    

    看看:http://symfony.com/doc/current/cookbook/security/api_key_authentication.html#cookbook-security-api-key-session

    您是否在 UserProvider 中实现了refreshUser 方法?

    public function refreshUser(UserInterface $user)
    {
        // this is used for storing authentication in the session
        // but in this example, the token is sent in each request,
        // so authentication can be stateless. Throwing this exception
        // is proper to make things stateless
        throw new UnsupportedUserException();
    }
    

    需要成为:

    public function refreshUser(UserInterface $user)
    {
        // $user is the User that you set in the token inside authenticateToken()
        // after it has been deserialized from the session
    
        // you might use $user to query the database for a fresh user
        // $id = $user->getId();
        // use $id to make a query
    
        // if you are *not* reading from a database and are just creating
        // a User object (like in this example), you can just return it
        return $user;
    }
    

    【讨论】:

    • 我在哪里可以拿到这个 $apiKeyProvider ?谢谢
    • 谢谢!我也更新了我的帖子!我收到此错误:ApiKeyAuthenticator::authenticateToken() 必须实现接口 Symfony\Component\Security\Core\Authentication\Token\TokenInterface 再次感谢
    • 在完全阅读指南后彻底检查了我的答案
    • 如果我输入 ?apikey=vdsadbrfadbfd 我可以登录用户,但是当我更改页面时会丢失会话,实际上我得到了“错误令牌丢失”。我正在使用该安全 xml。我不想在每个请求的 url 中传递令牌。真的很感谢你帮助我
    • 你把无状态选项改成false了吗?
    猜你喜欢
    • 2023-02-19
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2016-11-10
    • 2012-12-08
    • 1970-01-01
    相关资源
    最近更新 更多