gpt4 book ai didi

authentication - Symfony2 - 成功登录后重定向

转载 作者:行者123 更新时间:2023-12-04 06:41:23 28 4
gpt4 key购买 nike

我刚刚开始学习 Symfony 2 教程。我创建了一个包含用户类的 bundle ,并尝试按照说明设置登录过程。我想我快到了,但是我目前正处于最后一个障碍。

我设置了一个包:Dan\AuthBundle,其中包含我的用户类和另一个包:Dan\HelloBundle,我希望只允许登录用户访问。

我的security.yml文件如下:

security:
encoders:
Dan\AuthBundle\Entity\User: sha512

providers:
main:
entity: { class: Dan\AuthBundle\Entity\User, property: username }
administrators:
entity: { class: DanAuthBundle:User }

firewalls:
secured_area:
pattern: ^/*
form_login:
check_path: /login_check
login_path: /login
always_use_default_target_path: false
default_target_path: /hello

access_control:
- { path: ^/hello/.* }

主要的 routing.yml 文件如下所示:

DanAuthBundle:
resource: "@DanAuthBundle/Resources/config/routing.yml"
prefix: /auth/

DanHelloBundle_homepage:
pattern: /hello/
defaults: { _controller: DanHelloBundle:Default:index }

login:
pattern: /login
defaults: {_controller: DanAuthBundle:Default:login }

login_check:
pattern: /login_check

我手动创建了我的用户类的几个实例。

如果我尝试访问 url/hello,我会正确地重定向到登录页面。如果我输入了错误的详细信息,我会在模板中收到正确的消息,但是,当我使用正确的详细信息登录时,我会收到 324(空响应)错误(此时,浏览器中显示的 url是 login_check)。

通过阅读文档,我认为我应该被重定向到我最初尝试访问的页面?

http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form

By default, if the submitted credentials are correct, the user will be redirected to the original page that was requested (e.g. /admin/foo). If the user originally went straight to the login page, he'll be redirected to the homepage. This can be highly customized, allowing you to, for example, redirect the user to a specific URL.

另外,如果我在输入正确的详细信息后尝试访问该页面,我会再次被重定向到登录页面。

谁能看看我是否遗漏了什么明显的东西?

这是来 self 的日志文件:

[2012-06-18 18:33:47] doctrine.DEBUG: SELECT t0.id AS id1, t0.username AS username2, t0.salt AS salt3, t0.hashed_password AS hashed_password4 FROM User t0 WHERE t0.username = ? (["hello"]) [] [] [2012-06-18 18:33:47] security.INFO: User "hello" has been authenticated successfully [] [] [2012-06-18 18:33:47] event.DEBUG: Listener "Symfony\Component\Security\Http\Firewall::onKernelRequest" stopped propagation of the event "kernel.request". [] [] [2012-06-18 18:33:47] event.DEBUG: Listener "Symfony\Bundle\FrameworkBundle\EventListener\RouterListener" was not called for event "kernel.request". [] [] [2012-06-18 18:33:47] event.DEBUG: Listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener" was not called for event "kernel.request". [] [] [2012-06-18 18:33:47] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\Security\Http\Firewall\ContextListener::onKernelResponse". [] [] [2012-06-18 18:33:47] security.DEBUG: Write SecurityContext in the session [] []

任何建议表示赞赏。

谢谢。

最佳答案

// if you're using Symfony 2.0
$key = '_security.target_path';
// if you're using Symfony 2.1 or greater
// where "main" is the name of your firewall in security.yml
$key = '_security.main.target_path';

// try to redirect to the last page, or fallback to the homepage
if ($this->container->get('session')->has($key)) {
$url = $this->container->get('session')->get($key);
$this->container->get('session')->remove($key);
} else {
$url = $this->container->get('router')->generate('homepage');
}

return new RedirectResponse($url);

关于authentication - Symfony2 - 成功登录后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11073600/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com