Problems configuring user authentication by external API on Symfony2

后端 未结 1 1664
轮回少年
轮回少年 2021-01-21 04:19

I have a problem authenticating users for my new Symfony2 application.

This applications gets all the info through an API, so no database is used. When a user goes to lo

1条回答
  •  北海茫月
    2021-01-21 05:03

    If you want to write custom authentication you have found the correct links. As an example you can see the implementation of the OAuth authorization HWIOAuthBundle. But keep in mind that this type of authentication creates a user on your system. If you do not use a database, you must make a request to the API every time user send a request.

    First you need to understand that there is no magic. On every request symfony checks if url matches one of the specified firewalls (see secutity.yml). Listener that fired you can see in the firewall factory. If matches are found, the action switches to the corresponding AuthenticationListener. Listener attempts to authenticate the credewntials by creating Token, which is sended to AuthenticationProvider

    $this->authenticationManager->authenticate(new UsernamePasswordToken($username, $password, $this->providerKey));
    

    in AuthenticationProvider

    public function authenticate(TokenInterface $token) {
        ...
    }
    

    AuthenticationProvider try to get user via UserProvider. In case of success, Token stored in the session. On subsequent requests, ContextListener comes into play first, checks the session, extract token and send it to AuthenticationProvider similar.

    In general terms, the scheme looks like that. More info you can find examining the source code of Symfony Security component.

    Really good starting point is a UsernamePasswordFormAuthenticationListener. It just take login and password from request and make simplest UsernamePasswordToken.

    protected function attemptAuthentication(Request $request)
    {
        ...
    }
    

    Good luck!

    0 讨论(0)
提交回复
热议问题