简体   繁体   English

Symfony 3.2“security.firewall.map.context.main”依赖于不存在的服务^我的身份验证处理程序^

[英]Symfony 3.2 “security.firewall.map.context.main” has a dependency on a non-existent service ^my authentication handler^

On my personal Symfony 3.2 project ( https://github.com/pc-magas/photoalbum ) because I wanted to get a Json instead of redirecting based on http://www.webtipblog.com/adding-an-ajax-login-form-to-a-symfony-project/ I made the following authentication Manager: 在我个人的Symfony 3.2项目( https://github.com/pc-magas/photoalbum )上因为我想获得一个Json而不是基于http://www.webtipblog.com/adding-an-ajax-login的重定向-form-to-a-symfony-project /我做了以下身份验证管理器:

<?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 <joe@webtipblog.com>
     * @param   RouterInterface $router
     * @param   Session $session
     */
    public function __construct( RouterInterface $router, Session $session )
    {
        $this->router  = $router;
        $this->session = $session;
    }

    /**
     * onAuthenticationSuccess
     *
     * @author  Joe Sexton <joe@webtipblog.com>
     * @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 <joe@webtipblog.com>
     * @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' ) );
        }
    }
}

And I configured my services.yml like this: 我配置了我的services.yml如下所示:

parameters:

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

And I configured security.yml like that: 我配置了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 }

But I get the following Error: 但是我得到以下错误:

The service "security.firewall.map.context.main" has a dependency on a non-existent service "authentication_handler". 服务“security.firewall.map.context.main”依赖于不存在的服务“authentication_handler”。

Do you have any sort of idea how the prob will be solved? 你有什么想法可以解决问题吗? I have set the authentication_handler my service into services.yml file but I get the error mentioned above. 我已将authentication_handler我的服务设置为services.yml文件,但我得到上面提到的错误。

Perhaps you may messed up on how the service have been defined in services.yml . 也许您可能会搞砸如何在services.yml定义services.yml Please check the spaces and the yml syntax. 请检查空格和yml语法。

Yes, you need to add the service 是的,您需要添加服务

in services.xml 在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>

or in services.yml 或者在services.yml中

app.security.authentication_handler:
  class: AppBundle\Security\AuthenticationHandler
  arguments: ["@router", "@session", "@doctrine.orm.entity_manager" ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 服务“ security.context_listener.0”依赖于不存在的服务“ fos_user.user_manager” - The service “security.context_listener.0” has a dependency on a non-existent service “fos_user.user_manager” 服务“ twig”依赖于不存在的服务“ security.context” - The service “twig” has a dependency on a non-existent service “security.context” 服务“ form.type.users”依赖于不存在的服务“ security.context” - The service “form.type.users” has a dependency on a non-existent service “security.context” Symfony2错误:服务“模板”依赖于不存在的服务“ templating.globals” - Symfony2 error: The service “templating” has a dependency on a non-existent service “templating.globals” Symfony:服务...依赖于不存在的参数kernel.secret - Symfony: The service … has a dependency on a non-existent parameter kernel.secret Symfony突然失败,“服务”session.storage.metadata_bag“依赖于不存在的参数” - Symfony is suddenly failing with “The service ”session.storage.metadata_bag“ has a dependency on a non-existent parameter” Symfony 3.4 - 服务“Symfony\\Component\\Ldap\\Ldap”依赖于不存在的服务“Symfony\\Component\\Ldap\\Adapter\\ExtLdap\\Adapter” - Symfony 3.4 - The service "Symfony\Component\Ldap\Ldap" has a dependency on a non-existent service "Symfony\Component\Ldap\Adapter\ExtLdap\Adapter" 您已请求不存在的服务“security.context” - You have requested a non-existent service “security.context” Symfony 3.4-服务“ AppBundle \\ Controller \\ [some] Controller”依赖于不存在的服务“ Symfony \\ Component \\ Serializer \\ Serializer” - Symfony 3.4 - The service “AppBundle\Controller\[some]Controller” has a dependency on a non-existent service “Symfony\Component\Serializer\Serializer” Symfony 2称不存在的服务“路由器” - Symfony 2 calling non-existent service “router”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM