2016-11-10 26 views
2

symfonyの認証に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); 
    } 

Insideは、私がユーザエンティティからのトークンを作成することができますが、私はリフレッシュトークンを作成するために管理することはできませんアクションメソッドを生成また、プロバイダ私は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を共有できますか?私はログイン後にリフレッシュトークンを生成しません。 –

関連する問題