【问题标题】:Symfony FOSOAuthServerBundle ERR_TOO_MANY_REDIRECTSSymfony FOSOAuthServerBundle ERR_TOO_MANY_REDIRECTS
【发布时间】:2017-10-11 08:11:51
【问题描述】:

我正在尝试使用 symfony 和 FOSOAuthServerBundle 包创建 OAuth 服务器。我正在关注this tutorial,我在“授权代码”部分(也许你应该在之前检查这些部分)。当我在浏览器中打开 URL PROVIDER_HOST/oauth/v2/auth?client_id=CLIENT_ID&response_type=code&redirect_uri=CLIENT_HOST 时,我收到 ERR_TOO_MANY_REDIRECTS 错误。这是我的日志文件的输出:

[2017-10-11 09:50:58] request.INFO:匹配的路由 “fos_oauth_server_authorize”。 {"route":"fos_oauth_server_authorize","route_parameters":{"_controller":"FOS\OAuthServerBundle\Controller\AuthorizeController::authorizeAction","_route":"fos_oauth_server_authorize"},"request_uri":"http://example.de/app_dev.php/oauth/v2/auth?client_id=3_4ip472z6jf6scgoog0kssg8so0sosg0ok400w80ccog0s88gs0&redirect_uri=test.local&response_type=code", "方法":"GET"}

[] [2017-10-11 09:50:58] security.INFO: An AuthenticationException was 抛出;重定向到身份验证入口点。 {“异常”:“[对象] (Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException(代码: 0):在 TokenStorage 中没有找到 Token。在 .../vendor/symfony/symfony/src/Symfony/Component/Security/Http/Firewall/AccessListener.php:53)"}

[] [2017-10-11 09:50:58] security.DEBUG: Calling Authentication entry 观点。 [] []

[2017-10-11 09:51:00] request.INFO:匹配的路由 “acme_oauth_server_auth_login”。 {"route":"acme_oauth_server_auth_login","route_parameters":{"_controller":"SsoBundle\Controller\SecurityController::loginAction","_route":"acme_oauth_server_auth_login"},"request_uri":"http://example.de/app_dev.php/oauth/v2/auth_login","method ":"GET"}

[] [2017-10-11 09:51:00] security.INFO: An AuthenticationException was 抛出;重定向到身份验证入口点。 {“异常”:“[对象] (Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException(代码: 0):在 TokenStorage 中没有找到 Token。在 .../vendor/symfony/symfony/src/Symfony/Component/Security/Http/Firewall/AccessListener.php:53)"}

[] [2017-10-11 09:51:00] security.DEBUG: Calling Authentication entry 观点。 [] []

最后 3 个日志现在重复...我尝试在 AuthorizeController 和 SecurityController 中使用 echo "test"; die(); 对其进行调试,但这甚至不起作用。

这是我的安全控制器:

namespace SsoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;

class SecurityController extends Controller
{
    public function loginAction(Request $request)
    {

        $session = $request->getSession();

        if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(Security::AUTHENTICATION_ERROR);
        } elseif (null !== $session && $session->has(Security::AUTHENTICATION_ERROR)) {
            $error = $session->get(Security::AUTHENTICATION_ERROR);
            $session->remove(Security::AUTHENTICATION_ERROR);
        } else {
            $error = '';
        }

        if ($error) {
            $error = $error->getMessage(
            ); // WARNING! Symfony source code identifies this line as a potential security threat.
        }

        $lastUsername = (null === $session) ? '' : $session->get(Security::LAST_USERNAME);

        return $this->render(
            'SsoBundle:Security:login.html.twig',
            array(
                'last_username' => $lastUsername,
                'error' => $error,
            )
        );
    }

    public function loginCheckAction(Request $request)
    {

    }
}

这里是我的 security.yml:

security:

    # https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
    providers:
        in_memory:
            memory: ~
        user_provider:
            id: platform.user.provider

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        oauth_token:
            pattern:    ^/oauth/v2/token
            security:   false

        secured_area:
            pattern:    ^/
            form_login:
                provider: user_provider
                check_path: /oauth/v2/auth_login_check
                login_path: /oauth/v2/auth_login
            logout:
                path:   /logout
                target: /

        oauth_authorize:
            pattern:    ^/oauth/v2/auth
            form_login:
                provider: user_provider
                check_path: /oauth/v2/auth_login_check
                login_path: /oauth/v2/auth_login
            anonymous: true

        api:
            pattern:    ^/api/.*
            fos_oauth:  true
            stateless:  true

        main:
            anonymous: ~
            # activate different ways to authenticate

            # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
            #http_basic: ~

            # https://symfony.com/doc/current/security/form_login_setup.html
            #form_login: ~

    encoders:
        SsoBundle\Entity\User:
            algorithm:        sha1
            encode_as_base64: false
            iterations:       1

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    access_control:
        - { path: ^/api, roles: [ IS_AUTHENTICATED_FULLY ] }
        - { path: ^/demo/secured/hello/admin/, roles: ROLE_ADMIN }
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }

我不得不更改教程中的一些内容,因为它并不能正常工作。但现在我不知道这次我能做什么。

有人知道可能是什么问题吗?如果您需要更多代码,请告诉我。谢谢!

【问题讨论】:

    标签: php symfony security oauth


    【解决方案1】:

    Symfony 将选择第一个适合 url 的防火墙。在您的情况下,多个防火墙将与 ^/oauth/v2/auth 匹配。 secured_areaoauth_authorize。由于secured_area 看起来像是一个后备,可以捕获所有未被其他防火墙覆盖的 url,您可能希望将其移至文件末尾,因此最后检查它。

    我的猜测是secured_area(它不允许匿名访问?)被调用,然后将重定向以请求身份验证登陆同一防火墙并因此循环。

    【讨论】:

      猜你喜欢
      • 2013-07-05
      • 2021-11-01
      • 2018-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 2017-07-20
      相关资源
      最近更新 更多