控制器必须返回响应 - login_check防火墙拦截

时间:2017-06-16 10:34:34

标签: php symfony

我正在symfony中构建一个登录表单,我得到了这个异常“控制器必须返回一个响应(给定null)。你是否忘记在控制器的某处添加一个return语句?

有意义的是,每个Symfony中的Action都必须有响应,但在这种情况下,防火墙应该通过FormLoginAuthenticator拦截和验证用户。

我认为原因是security.yml中的配置错误。

这是我的security.yml

security:
    encoders:
      AppBundle\Entity\User: bcrypt
    providers:
      in_memory:
        memory:
          users:
            admin:
              password: test1234
              roles: 'ROLE_ADMIN'
      database_users:
          entity: { class: AppBundle:User, property: email }
    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        default:
          anonymous: ~
          http_basic: ~
        secured_area:
            pattern:    ^/
            form_login:
              check_path: /login_check
              login_path: /login
        main:
            pattern:    ^/
            form_login:
                check_path: /login_check
                login_path: /login
                always_use_default_target_path: true
                default_target_path: /secured
    access_control:
        - {path: ^/admin,roles: ROLE_ADMIN}
        - {path: ^/showRoutes,roles: ROLE_ADMIN}
        - {path: ^/routeDetails,roles: ROLE_ADMIN}
        - {path: ^/allRoutes,roles: ROLE_ADMIN}
      #  - { path: ^/getTripOverview, roles: ROLE_USER }
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, methods: [GET] }

这是我的登录控制器的代码

class SecurityController extends Controller
{



    /**
     * @Route("/login", name="login")
     */
    public function loginAction(Request $request)
    {
    $helper = $this->get('security.authentication_utils');

    return $this->render(
        'AppBundle::Login.html.twig',
        array(
            'last_username' => $helper->getLastUsername(),
            'error'         => $helper->getLastAuthenticationError(),
        )
    );
}

/**
 * @Route("/login_check", name="security_login_check")
 */
public function loginCheckAction()
{
}

/**
 * @Route("/logout", name="logout")
 */
public function logoutAction()
{

}

}

这是我的身份验证员。

class FormLoginAuthenticator extends AbstractFormLoginAuthenticator
{
    private $router;

private $ encoder;

public function __construct(RouterInterface $router, UserPasswordEncoderInterface $encoder)
{
    $this->router = $router;
    $this->encoder = $encoder;
}

public function getCredentials(Request $request)
{
    if ($request->getPathInfo() != '/login_check') {
        return;
    }

    $email = $request->request->get('_email');
    $request->getSession()->set(Security::LAST_USERNAME, $email);
    $password = $request->request->get('_password');

    return [
        'email' => $email,
        'password' => $password,
    ];
}

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

    return $userProvider->loadUserByUsername($email);
}

public function checkCredentials($credentials, UserInterface $user)
{
    $plainPassword = $credentials['password'];
    if ($this->encoder->isPasswordValid($user, $plainPassword)) {
        return true;
    }

    throw new BadCredentialsException();
}

public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
    $url = $this->router->generate('getTripOverview');

    return new RedirectResponse($url);
}

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
    $request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);

    $url = $this->router->generate('login');

    return new RedirectResponse($url);
}

protected function getLoginUrl()
{
    return $this->router->generate('login');
}

protected function getDefaultSuccessRedirectUrl()
{
    return $this->router->generate('getTripOverview');
}

public function supportsRememberMe()
{
    return false;
}

}

0 个答案:

没有答案