繁体   English   中英

Symfony2自定义身份验证系统在不应该的情况下传递用户

[英]Symfony2 custom authentication system passes user when it shouldn't

我正在为Symfony2编写一个包,我需要编写一个带有警卫的自定义身份验证系统。 整个系统基于令牌。 我们假设我需要发送一个POST请求。 在标题中我必须包含'TOKEN:testtoken'。

  1. 我在标题中发送没有'TOKEN:testtoken'的请求,我得到了

     { "message": "Authentication Required" } 
  2. 我发送了一个'TOKEN:badtoken'的请求,我得到了

     { "message": "Username could not be found." } 

    不要看'用户名'。 这是错误的。

  3. 我发送请求'TOKEN:testtoken'然后我得到了

     { "token": "testtoken" } 

    这只是示例页面。

  4. 现在我从头文件中删除'TOKEN:testtoken'(我使用Postman来测试REST API)然后我得到了

     { "token": "testtoken" } 

    我不知道为什么。 在我看来,在这种情况下我的系统应该返回

     { "message": "Authentication Required" } 

这是我的TokenAuthenticator.php

<?php
namespace WLN\AuthTokenBundle\Security;

use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;

class TokenAuthenticator extends AbstractGuardAuthenticator
{
    private $em;
    private $user_repository;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function setConfig($user_repo)
    {
        $this->user_repository = $user_repo;
    }

    public function getCredentials(Request $request)
    {
        if($token = $request->headers->get('WLN-AUTH-TOKEN'))
        {
            return [
                'token' => $token
            ];
        }

        return null;
    }

    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        $token = $credentials['token'];

        return $this->em->getRepository($this->user_repository)
            ->findOneBy(array('token' => $token));
    }

    public function checkCredentials($credentials, UserInterface $user)
    {
        return true;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
    {
        return null;
    }

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        $data = array(
            'message' => strtr($exception->getMessageKey(), $exception->getMessageData())
        );

        return new JsonResponse($data, 403);
    }

    public function start(Request $request, AuthenticationException $authException = null)
    {
        $data = array(
            'message' => 'Authentication Required'
        );

        return new JsonResponse($data, 401);
    }

    public function supportsRememberMe()
    {
        return false;
    }
}

PS我的应用程序是共享托管。 可能缓存导致它或类似的东西?

我弄清楚出了什么问题。 当我在我的会话中请求有效令牌时形成了PHPSESSID。 因此,我的API行为变得非常怪异。 在我的情况下,解决方案是将security.firewalls.main.stateless设置为true :)。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM