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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Symfony 3.2“security.firewall.map.context.main”依赖于不存在的服务^我的身份验证处理程序^脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
在我个人的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' ) );
        }
    }
}

我像这样配置了我的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 }

但是我得到以下错误

你有什么想法可以解决概率吗?我已将authentication_handler我的服务设置为services.yml文件,但是我得到了上面提到的错误.

也许您可能会搞砸如何在services.yml中定义服务.请检查空格和yml语法.

脚本宝典总结

以上是脚本宝典为你收集整理的php – Symfony 3.2“security.firewall.map.context.main”依赖于不存在的服务^我的身份验证处理程序^全部内容,希望文章能够帮你解决php – Symfony 3.2“security.firewall.map.context.main”依赖于不存在的服务^我的身份验证处理程序^所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。