dongqi7631 2017-06-16 10:34
浏览 50

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

I am building a login form in symfony and I get this Exception "The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?

It makes sense bc every Action in Symfony has to have a response but in this case the firewall should intercept and authenticate the user via the FormLoginAuthenticator.

I think the reason is a wrong config in the security.yml.

This is my 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] }

here is the code of my login controller

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()
{

}

}

here is my authenticator.

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思