2016-05-20 16 views
2

メンバーをフロントエンドからログインさせて、私の下に自分の認証ハンドラを定義し、これを期待どおりのjson応答を与えるサービスとして追加しました。Symfony3とAjax認証

<?php 

namespace AppBundle\Handler; 

use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; 
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; 
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; 
use Symfony\Component\Routing\RouterInterface; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\Routing\Router; 
use Symfony\Component\Security\Core\SecurityContext; 
use Symfony\Component\Security\Core\Exception\AuthenticationException; 


class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface 
{ 

protected $router; 
//protected $security; 
protected $userManager; 
protected $service_container; 

public function __construct(RouterInterface $router, $userManager, $service_container) 
{ 
    $this->router = $router; 
    //$this->security = $security; 
    $this->userManager = $userManager; 
    $this->service_container = $service_container; 

} 
public function onAuthenticationSuccess(Request $request, TokenInterface $token) { 
    if ($request->isXmlHttpRequest()) { 
     $result = array('success' => true); 
     $response = new Response(json_encode($result)); 
     $response->headers->set('Content-Type', 'application/json'); 
     return $response; 
    } 
    else { 
     // Create a flash message with the authentication error message 
     $request->getSession()->set(SecurityContext::AUTHENTICATION_ERROR, $exception); 
     $url = $this->router->generate('fos_user_security_login'); 

     return new RedirectResponse($url); 
    } 

    return new RedirectResponse($this->router->generate('anag_new')); 
} 
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) { 

    if ($request->isXmlHttpRequest()) { 
     $result = array('success' => false, 'message' => $exception->getMessage()); 
     $response = new Response(json_encode($result)); 
     $response->headers->set('Content-Type', 'application/json'); 
     return $response; 
    } 
    return new Response(); 
} 
} 

ただし、ユーザーが登録されているかどうかに関係なく、同じ結果が得られます。ここでは、応答

{"success":false,"message":"Bad credentials."} 

は、ここで私のためのAPIの認証を実装するesiest方法は、ブランドの新しいガードを実装することである私のsecurity.yml

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

    admin: 
     pattern:   /admin(.*) 
     context:   user 
     form_login: 
      provider:  fos_userbundle 
      login_path:  /admin/login 
      use_forward: false 
      check_path:  /admin/login_check 
      failure_path: null 
     logout: 
      path:   /admin/logout 
      target:   /admin/login 
     anonymous:   true 

    main: 
     pattern:    .* 
     context:    user 
     form_login: 
      provider:  fos_userbundle 
      login_path:  /login 
      use_forward: false 
      check_path:  fos_user_security_check 
      failure_path: null 
      success_handler: authentication_handler 
      failure_handler: authentication_handler 
     logout:    true 
     anonymous:   true 

のrouting.yml

fos_user_security_check: 
    path: /login_check 
    defaults: 
     _controller: FOSUserBundle:Security:check 

fos_user_security_logout: 
    path: /logout 
    defaults: 
     _controller: FOSUserBundle:Security:logout 

答えて

0

です認証インターフェイス

http://symfony.com/doc/current/cookbook/security/guard-authentication.html

この単純なクラスを使用すると、認証をインスタンス化、処理、および後処理するプロセスを定義できます。サービスを有効にする

は、あなたはまた、ユーザーがカスタム認証(ベアラ、フォームの任意の型を扱うことができるガードを使用して、この

http://symfony.com/doc/current/cookbook/security/custom_provider.html

を提供する必要がある

# app/config/security.yml 
security: 
    # ... 

    firewalls: 
     # ... 

     main: 
      anonymous: ~ 
      logout: ~ 

      guard: 
       authenticators: 
        - app.my_authenticator 

      # if you want, disable storing the user in the session 
      # stateless: true 

      # maybe other things, like form_login, remember_me, etc 
      # ... 

と同じくらい簡単です、クッキー、GETトークンなど)

関連する問題