Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2使用api密钥访问控制问题对用户进行身份验证_Php_Api_Symfony_Authentication - Fatal编程技术网

Php Symfony2使用api密钥访问控制问题对用户进行身份验证

Php Symfony2使用api密钥访问控制问题对用户进行身份验证,php,api,symfony,authentication,Php,Api,Symfony,Authentication,嗨,我正在为自己开发一个项目。我想做的是验证除用户注册之外的所有api调用。我创建了用户身份验证和用户提供程序,如此链接中所述: 一切正常,我的后端的所有URL都经过身份验证,并由UserTokenAuthenticator和UserTokenProvider处理。但它能证明每个人的身份。我不想验证这个url /v1.0/users with POST method 因此,我在security.yml文件中设置了如下访问控制: access_control: user_reg

嗨,我正在为自己开发一个项目。我想做的是验证除用户注册之外的所有api调用。我创建了用户身份验证和用户提供程序,如此链接中所述:

一切正常,我的后端的所有URL都经过身份验证,并由UserTokenAuthenticator和UserTokenProvider处理。但它能证明每个人的身份。我不想验证这个url

    /v1.0/users with POST method
因此,我在security.yml文件中设置了如下访问控制:

access_control:
    user_register:
        path: ^/v1.0/users
        roles: IS_AUTHENTICATED_ANONYMOUSLY
        methods: [POST]
但每当我尝试对此url进行post调用时,它都会尝试进行身份验证(bec调用没有令牌作为参数),结果失败。如何防止仅对此路由进行身份验证??以下是我的全部代码:

security.yml:

security:
    encoders:
        entity_user:
            class: KBell\AppBundle\Entity\User
            algorithm: sha1
            iterations: 1
            encode_as_base64: false
        entity_device:
            class: KBell\AppBundle\Entity\Device
            algorithm: sha1
            iterations: 1
            encode_as_base64: false

    role_hierarchy:
        ROLE_DEVICE: ROLE_DEVICE
        ROLE_USER : ROLE_USER

    providers:
        entity_device:
            entity:
                class: KBell\AppBundle\Entity\Device
                property: name
        entity_user:
            entity:
                class: KBell\AppBundle\Entity\User
                property: email
        user_token_provider:
            id: user_token_provider
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        api_user_secured_area:
            pattern: ^/v1.0/users
            stateless: true
            simple_preauth:
                authenticator: user_token_authenticator
            provider: user_token_provider
    access_control:
        user_register:
            path: ^/v1.0/users
            roles: IS_AUTHENTICATED_ANONYMOUSLY
            methods: [POST]
以下是UserTokenAuthenticator:

<?php

class UserTokenAuthenticator implements SimplePreAuthenticatorInterface,    AuthenticationFailureHandlerInterface
{

    public function createToken(Request $request, $providerKey)
    {
        $accessToken = $request->query->get('access_token');
        if (!$accessToken) {
            throw new BadCredentialsException('Access Token is missing');
        }

        return new PreAuthenticatedToken(
            'anon.',
            $accessToken,
            $providerKey
        );
    }

    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
    {
        $accessToken = $token->getCredentials();
        $user = $userProvider->getUserForAccessToken($accessToken);
        if (!$user) {
            throw new AuthenticationException(
                sprintf('Access Token "%s" does not exist.', $accessToken)
            );
        }

        return new PreAuthenticatedToken(
            $user,
            $accessToken,
            $providerKey,
            $user->getRoles()
        );
    }

    public function supportsToken(TokenInterface $token, $providerKey)
    {
        return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
    }

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        return ApiUtil::getJsonErrorResponse("User authentication is failed: ".$exception->getMessage(), Response::HTTP_UNAUTHORIZED, "AuthenticationException");
    }
}

我为此花了很多时间。任何帮助都将不胜感激!谢谢大家!

我认为您可以尝试如下配置防火墙设置:

firewalls:
    secured_user_area:
        pattern: /v(\d+\.?\d*)/(?!users)
        stateless: true
        provider: user_token_provider

    ....

这里是
?!用户
(甚至多个URL,如
?!用户| URL2 | URL3
)声明将URL从防火墙中排除。

我认为您可以尝试如下配置防火墙设置:

firewalls:
    secured_user_area:
        pattern: /v(\d+\.?\d*)/(?!users)
        stateless: true
        provider: user_token_provider

    ....

这里是
?!用户
(甚至多个URL,如
?!users | URL2 | URL3
)声明将URL从防火墙中排除。

它将允许所有其他方法(获取、放置等)到
/v1.0/用户
终点。我只是希望POST不被认证,我也不知道为什么
访问控制
在这种情况下不起作用?它将允许所有其他方法(获取、放置等)到达
/v1.0/用户
终点。我只是希望帖子不被认证,我也不知道为什么
access\u control
在这种情况下不起作用?