2017-05-03 11 views
6

自分の個人的なSymfony 3.2プロジェクト(https://github.com/pc-magas/photoalbum)では、リダイレクトベースの代わりにJsonを取得したかったのでhttp://www.webtipblog.com/adding-an-ajax-login-form-to-a-symfony-project/に私は、次の認証マネージャ製:symfony 3.2 "security.firewall.map.context.main"は存在しないサービスに依存しています^私の認証ハンドラ^

<?php 

namespace AppBundle\Security; 

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

class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface 
{ 
    private $router; 
    private $session; 

    /** 
    * Constructor 
    * 
    * @author Joe Sexton <[email protected]> 
    * @param RouterInterface $router 
    * @param Session $session 
    */ 
    public function __construct(RouterInterface $router, Session $session) 
    { 
     $this->router = $router; 
     $this->session = $session; 
    } 

    /** 
    * onAuthenticationSuccess 
    * 
    * @author Joe Sexton <[email protected]> 
    * @param Request $request 
    * @param TokenInterface $token 
    * @return Response 
    */ 
    public function onAuthenticationSuccess(Request $request, TokenInterface $token) 
    { 
     // if AJAX login 
     if ($request->isXmlHttpRequest()) { 

      $array = array('status' => true); // data to return via JSON 
      $response = new Response(json_encode($array)); 
      $response->headers->set('Content-Type', 'application/json'); 

      return $response; 

      // if form login 
     } else { 

      if ($this->session->get('_security.main.target_path')) { 

       $url = $this->session->get('_security.main.target_path'); 

      } else { 

       $url = $this->router->generate('home_page'); 

      } // end if 

      return new RedirectResponse($url); 

     } 
    } 

    /** 
    * onAuthenticationFailure 
    * 
    * @author Joe Sexton <[email protected]> 
    * @param Request $request 
    * @param AuthenticationException $exception 
    * @return Response 
    */ 
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception) 
    { 
     // if AJAX login 
     if ($request->isXmlHttpRequest()) { 

      $array = array('status' => false, 'message' => $exception->getMessage()); // data to return via JSON 
      $response = new Response(json_encode($array)); 
      $response->headers->set('Content-Type', 'application/json'); 

      return $response; 

      // if form login 
     } else { 

      // set authentication exception to session 
      $request->getSession()->set(SecurityContextInterface::AUTHENTICATION_ERROR, $exception); 

      return new RedirectResponse($this->router->generate('login_route')); 
     } 
    } 
} 

をそして私はこのように私のservices.ymlを設定した:

parameters: 

services: 
    authentication_handler: 
    class: AppBundle\Security\AuthenticationHandler 
    public: false 
    arguments: ["@router","@session"] 

そして、私はそのような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 

    firewalls: 
     main: 
      pattern: ^/ 
      form_login: 
       provider: fos_userbundle 
       csrf_token_generator: security.csrf.token_manager 
       check_path:  security_check_route 
       success_handler: authentication_handler 
       failure_handler: authentication_handler 

      logout:  true 
      anonymous: true 

    access_control: 
     - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/admin/, role: ROLE_ADMIN } 

は、しかし、私は次のエラーを取得する:

The service "security.firewall.map.context.main" has a dependency on a non-existent service "authentication_handler".

あなたはPROBを解決する方法のアイデアの任意の並べ替えを持っていますか? authentication_handler私のサービスをservices.ymlファイルに設定しましたが、上記のエラーが発生します。

答えて

4

services.ymlでサービスがどのように定義されているのかが間違っている可能性があります。スペースとymlの構文を確認してください。

+0

中にサービス

を追加する必要がそれは私をたくさん助けたですありがとうございました。 –

+0

私はハンドラをサービスとして宣言し、security.ymlでこのサービスを参照しましたが、それでも同じエラーが発生しました – r4ccoon

2

はい、あなたはservices.xmlの

<service id="app.security.authentication_handler" class="AppBundle\Security\AuthenticationHandler"> 
    <argument type="service" id="router" /> 
    <argument type="service" id="session" /> 
    <argument type="service" id="doctrine.orm.entity_manager" /> 
</service> 

またはservices.yml

app.security.authentication_handler: 
    class: AppBundle\Security\AuthenticationHandler 
    arguments: ["@router", "@session", "@doctrine.orm.entity_manager" ] 
関連する問題