2016-11-10 4 views
2

심포니에서 인증을 위해 JWT를 사용하는 API 작업을하고 있습니다. JWT의 경우 LexikJWTAuthenticationBundle을 사용하고 JWTRefreshTokenBundle을 사용하여 토큰 리프레쉬를 수행합니다. 내가 원하는 것은 토큰을 통해 사용자를 인증하고 새로 고침 토큰을 제공하는 것입니다.Symfony 3 사용자 등록시 JWT 인증

firewalls: 
     # disables authentication for assets and the profiler, adapt it according to your needs 
     dev: 
      pattern: ^/(_(profiler|wdt)|css|images|js)/ 
      security: false 

     login: 
      pattern: ^/api/login_check 
      stateless: true 
      anonymous: true 
      form_login: 
       check_path:    fos_user_security_check 
       username_parameter:  username 
       password_parameter:  password 
       success_handler:   lexik_jwt_authentication.handler.authentication_success 
       failure_handler:   lexik_jwt_authentication.handler.authentication_failure 
       require_previous_session: false 
     register: 
      pattern: ^/api/register 
      anonymous: true 
      stateless: true 
     refresh: 
      pattern: ^/api/token/refresh 
      stateless: true 
      anonymous: true 
     api: 
      pattern: ^/api 
      provider: fos_userbundle 
      stateless: true 
      guard: 
       authenticators: 
        - lexik_jwt_authentication.jwt_token_authenticator 

을 그리고 내 등록 작업을 내부에 내가 가지고 : 보안에서 난이

/** 
    * @Route("/api/register") 
    * @Method({"POST"}) 
    */ 
    public function registerAction(Request $request) 
    { 
     $userManager = $this->get('fos_user.user_manager'); 
     $data = $request->request->all(); 

     $mailValidator = $this->get('validator.email'); 
     $mailValidator->validate($data['email']); 

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

     $userManager->updateUser($user); 

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

    protected function generateToken(User $user, $statusCode = 200) 
    { 
     $token = $this->get('lexik_jwt_authentication.jwt_manager')->create($user); 
     $response = array(
      'token' => $token, 
      'refreshToken' => null, 
      'username' => $user->getUsername(), 
      'mail'  => $user->getEmail(), 
     ); 

     return new JsonResponse($response, $statusCode); 
    } 

내부 내가 사용자 개체에서 토큰을 생성 할 수 있습니다 액션 메소드를 생성하지만 난 새로 고침 토큰을 생성 관리 할 수 ​​없습니다 또한. 공급자의 경우 FOSUserBundle을 사용하고 login_check 컨트롤러입니다. generateToken 메서드에서 해당 컨트롤러에 게시물 요청을 보내려고했지만 성공하지 못했습니다. 어떤 도움을 주시면 감사하겠습니다.

+0

저도 그렇습니다. 이것에 대한 해결책을 찾았습니까? –

답변

0

해결했는지 모르겠지만 로그인 성공 후 새로 고침 토큰이 생성됩니다. 따라서 등록 할 때 새로 고침 토큰을 생성 할 필요가 없습니다.

Security.yml :

security: 
encoders: 
    FOS\UserBundle\Model\UserInterface: bcrypt 

role_hierarchy: 
    ROLE_ADMIN:  ROLE_USER 
    ROLE_SUPER_ADMIN: ROLE_ADMIN 

providers: 
    fos_userbundle: 
     id: fos_user.user_provider.username_email 

firewalls: 
    login: 
     pattern: ^/login_check 
     stateless: true 
     anonymous: true 
     form_login: 
      check_path:    fos_user_security_check 
      success_handler:   lexik_jwt_authentication.handler.authentication_success 
      failure_handler:   lexik_jwt_authentication.handler.authentication_failure 
      require_previous_session: false 
    register: 
     pattern: ^/register 
     anonymous: true 
     stateless: true 
    refresh: 
     pattern: ^/token/refresh 
     stateless: true 
     anonymous: true 
    api: 
     pattern: ^/ 
     provider: fos_userbundle 
     stateless: true 
     guard: 
       authenticators: 
        - 'token_authenticator' 

access_control: 
    - { path: ^/token/refresh, roles: IS_AUTHENTICATED_ANONYMOUSLY } 

Service.yml : 이것은 내 코드입니다

services: 
token_authenticator: 
    class: UserBundle\Security\TokenAuthenticator 
    arguments: ['@lexik_jwt_authentication.encoder', '@doctrine.orm.entity_manager'] 

config.yml : 터미널에서

lexik_jwt_authentication: 
    private_key_path: '%jwt_private_key_path%' 
    public_key_path: '%jwt_public_key_path%' 
    pass_phrase:  '%jwt_key_pass_phrase%' 
    token_ttl:  '%jwt_token_ttl%' 

gesdinet_jwt_refresh_token: 
    user_provider: fos_user.user_provider.username_email 
    ttl: 2592000 
    ttl_update: true 
    firewall: api 

실행 :

curl -X POST http://demowebsite.com/login_check -d username=USERNAME -d password=PASSWORD 

토큰을 생성하고 토큰을 새로 고칩니다. 새로 고침 토큰은 DB 테이블에 저장됩니다. refresh_token

+0

TokenAuthenticator를 공유 할 수 있습니까? 내 로그인 후 새로 고침 토큰을 생성하지 않습니다. –