Symfony 2 FOSUserBundle带有休息登录和注册功能[英] Symfony 2 FOSUserBundle with rest login and registration

本文是小编为大家收集整理的关于Symfony 2 FOSUserBundle带有休息登录和注册功能的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我经历了很多堆栈流的问题和文章,但找不到合适的答案.

我正在使用fosuserbundle,hwiouthbundle和lexikjwt捆绑包.

我正在开发基于Symfony的API,该Android应用程序和Angular App将消费.

现在,我需要带有hwiouthbundle和lexikjwt捆绑包的fosuserbundle facebook登录的寄存器和登录系统.

我已经实现了FosuserBundle和Hwiouthbundke,并且都没有编写用户控制器.但是我不需要休息.但是我不能输入:在路由器中休息.

现在如何登录,在fosuserbundle上注册用户?我不想使用Fosouth服务器.只需要注册并使用API​​登录,而不会从Web中休息.

推荐答案

因此,如果要使用FosuserBundle手动注册用户,请创建一个控制器并添加寄存器方法:

// Acme/AppBundle/Controller/SecurityController

public function registerAction(Request $request)
{
    $userManager = $this->get('fos_user.user_manager');
    $entityManager = $this->get('doctrine')->getManager();
    $data = $request->request->all();

    // Do a check for existing user with userManager->findByUsername

    $user = $userManager->createUser();
    $user->setUsername($data['username']);
    // ...
    $user->setPlainPassword($data['password']);
    $user->setEnabled(true);

    $userManager->updateUser($user);

    return $this->generateToken($user, 201);
}

和GenerateToken方法

protected function generateToken($user, $statusCode = 200)
{
    // Generate the token
    $token = $this->get('lexik_jwt_authentication.jwt_manager')->create($user)

    $response = array(
        'token' => $token,
        'user'  => $user // Assuming $user is serialized, else you can call getters manually
    );

    return new JsonResponse($response, $statusCode); // Return a 201 Created with the JWT.
}

和路线

security_register:
    path: /api/register
    defaults: { _controller: AcmeAppBundle:Security:registerAction }
    methods: POST

配置与登录的防火墙相同

// app/config/security.yml

firewalls:
    // ...
    register:
        pattern: ^/api/register
        anonymous: true
        stateless: true
    // ...

access_control:
    // ...
    - { path: ^/api/register, role: IS_AUTHENTICATED_ANONYMOUSLY }

登录,Juste使用Fosuser登录防火墙的check_path.

有关代币生成的更多信息,请参见 jwtmanager . 希望这对您有帮助.

编辑

如果您想要一个lexikjwtauthenticationbundle + fosuserbundle + fosrestBundle的完整示例/p>

本文地址:https://www.itbaoku.cn/post/2090749.html

问题描述

I have gone through lots of stackoveflow question and articles, but can't find a suitable answer.

I'm using fosuserbundle, hwiouthbundle and lexikjwt bundle.

I'm developing an api based on symfony which will be consumed by an android app and angular app.

Now I need the register and login system with fosuserbundle facebook login with hwiouthbundle and api protection with lexikjwt bundle.

I have implemented fosuserbundle and hwiouthbundke and both working without even writing user controller. But I need this with rest not with form. But I can't out type : rest in router.

Now how can I login, register user with fosuserbundle with rest? I don't want to use fosouth server. Just need registration and login with api not rest from web.

推荐答案

So, if you want register user manually using FOSUserBundle, create a controller and add a register method :

// Acme/AppBundle/Controller/SecurityController

public function registerAction(Request $request)
{
    $userManager = $this->get('fos_user.user_manager');
    $entityManager = $this->get('doctrine')->getManager();
    $data = $request->request->all();

    // Do a check for existing user with userManager->findByUsername

    $user = $userManager->createUser();
    $user->setUsername($data['username']);
    // ...
    $user->setPlainPassword($data['password']);
    $user->setEnabled(true);

    $userManager->updateUser($user);

    return $this->generateToken($user, 201);
}

And, the generateToken method

protected function generateToken($user, $statusCode = 200)
{
    // Generate the token
    $token = $this->get('lexik_jwt_authentication.jwt_manager')->create($user)

    $response = array(
        'token' => $token,
        'user'  => $user // Assuming $user is serialized, else you can call getters manually
    );

    return new JsonResponse($response, $statusCode); // Return a 201 Created with the JWT.
}

And the route

security_register:
    path: /api/register
    defaults: { _controller: AcmeAppBundle:Security:registerAction }
    methods: POST

Configure the firewall same as login

// app/config/security.yml

firewalls:
    // ...
    register:
        pattern: ^/api/register
        anonymous: true
        stateless: true
    // ...

access_control:
    // ...
    - { path: ^/api/register, role: IS_AUTHENTICATED_ANONYMOUSLY }

For login, juste use the check_path of your FOSUser login firewall.

For more information about the token generation, see JWTManager. Hope this help you.

EDIT

If you want a full example of LexikJWTAuthenticationBundle + FOSUserBundle + FOSRestBundle implementation see my symfony-rest-api