Symfony3和Ajax身份验证

时间:2016-05-20 09:53:18

标签: php symfony fosuserbundle

我希望成员从前端登录,并且我已经在下面定义了我的身份验证处理程序,并将其添加为服务,它按预期为我提供了json响应。

<?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."}

这是我的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 

1 个答案:

答案 0 :(得分:0)

为我实施API身份验证的最有效方法是实现全新的Guard身份验证接口

https://api.mongodb.com/csharp/current/html/T_MongoDB_Bson_BsonDocument.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
            # ...

您还需要用户提供此

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

使用Guard,您可以处理任何类型的自定义身份验证(承载,表单,Cookie,GET令牌等)