Symfony 2.6安全登录成功登录后重定向回登录页面

时间:2015-05-09 23:23:45

标签: php symfony login

我目前正在为Symfony2设置一个自定义用户提供程序的登录系统。我创建了User和Provider类,登录系统可以正常使用" manual"测试变量。例如,这是我的提供商,其测试用户数据总是成功登录:

<?php

namespace SoftAltern\DashboardBundle\Security\User;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;

class WebserviceUserProvider implements UserProviderInterface
{
    public function loadUserByUsername($username)
    {
        $userData = [
            'username' => 'test',
            'password' => 'testpass',
            'roles' => [],
        ];

        if ($userData) {
            return new WebserviceUser($userData['username'], $userData['password'], '', $userData['roles']);
        }

        throw new UsernameNotFoundException(
            sprintf('Username "%s" does not exist.', $username)
        );
    }

    public function refreshUser(UserInterface $user)
    {
        if (!$user instanceof WebserviceUser) {
            throw new UnsupportedUserException(
                sprintf('Instances of "%s" are not supported.', get_class($user))
            );
        }

        return $this->loadUserByUsername($user->getUsername());
    }

    public function supportsClass($class)
    {
        return $class === 'SoftAltern\DashboardBundle\Security\User\WebserviceUser';
    }
}

这显然很好。但是,当我尝试添加自己的检查并返回以相同格式构建的数组时,它只会返回到登录页面。这是一个例子:

<?php

namespace SoftAltern\DashboardBundle\Security\User;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;

use SoftAltern\DashboardBundle\Helper\I5ToolkitHelper;

class WebserviceUserProvider implements UserProviderInterface
{
    public function loadUserByUsername($username)
    {
        $request = Request::createFromGlobals();
        $toolkit = new I5ToolkitHelper;
        $userData = $toolkit->checkUserData($username, $request->request->get('_password'));

        if ($userData) {
            return new WebserviceUser($userData['username'], $userData['password'], '', $userData['roles']);
        }

        throw new UsernameNotFoundException(
            sprintf('Username "%s" does not exist.', $username)
        );
    }

    public function refreshUser(UserInterface $user)
    {
        if (!$user instanceof WebserviceUser) {
            throw new UnsupportedUserException(
                sprintf('Instances of "%s" are not supported.', get_class($user))
            );
        }

        return $this->loadUserByUsername($user->getUsername());
    }

    public function supportsClass($class)
    {
        return $class === 'SoftAltern\DashboardBundle\Security\User\WebserviceUser';
    }
}

我认为问题在于使用请求来获取密码。但是,在我当前的系统(AS / 400 IBM iSeries)中,无法根据用户名获取密码。所以我想要做的就是使用i5ToolKit,检查凭据,并根据成功的iSeries登录返回userData。它返回数组很好,但仍然只是在成功时重定向回登录页面。

我认为它与请求有关的原因是因为如果我评论该部分并手动向我的I5ToolKitHelper提交有效的用户名和密码,那么一切都按预期工作。

我也已经尝试过他们在this post中使用request建议的方式,但它也没有帮助。

这是我的security.yml

# you can read more about security in the related section of the documentation
# http://symfony.com/doc/current/book/security.html
security:
    # http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password
    encoders:
        SoftAltern\DashboardBundle\Security\User\WebserviceUser: plaintext

    # http://symfony.com/doc/current/book/security.html#hierarchical-roles
    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
    providers:
        webservice:
            id: webservice_user_provider

    # the main part of the security, where you can set up firewalls
    # for specific sections of your app
    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false
        # the login page has to be accessible for everybody
        login:
            pattern:  ^/login$
            security: false

        # secures part of the application
        soft_altern_dashboard_secured_area:
            pattern:    ^/
            # it's important to notice that in this case _demo_security_check and _demo_login
            # are route names and that they are specified in the AcmeDemoBundle
            form_login:
                check_path: soft_altern_dashboard_login_check
                login_path: soft_altern_dashboard_login
            logout:
                path:   soft_altern_dashboard_logout
                target: soft_altern_dashboard_homepage
            #anonymous: ~
            #http_basic:
            #    realm: "Secured Demo Area"

    # with these settings you can restrict or allow access for different parts
    # of your application based on roles, ip, host or methods
    # http://symfony.com/doc/current/cookbook/security/access_control.html
    access_control:
        #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }

非常感谢任何帮助!

编辑:我在一次登录时错误地记录了密码,看起来它点击了两次登录检查,第二次密码是空的。此行为可能是出于安全原因。我不确定我是否设置错误可能导致此问题。

1 个答案:

答案 0 :(得分:0)

可能有很多相关的事情。 Symfony重定向到的页面通常存储在会话中。你可以改变它。但首先,尝试在form_login下添加以下选项:

crossType