我试图让我的 Symfony2 网站在尝试访问登录页面时将登录用户重定向到另一个页面。
这是我的security.yml:
security:
encoders:
Cocoa\LoginBundle\Entity\User:
algorithm: sha512
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH ]
providers:
administrators:
entity: { class: CocoaLoginBundle:User, property: username }
firewalls:
admin_area:
pattern: ^/administration
form_login:
login_path: login
check_path: login_check
logout:
path: /administration/logout
invalidate_session: true
target: /
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY}
- { path: ^/administration, roles: ROLE_ADMIN }
这是我的控制器:
class LoginController extends Controller {
public function loginAction() {
if (false === $this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
return $this->redirect($this->generateUrl('public_homepage'));
}
$request = $this->getRequest();
$session = $request->getSession();
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->render('CocoaLoginBundle:Login:login.html.twig', array(
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error));
}
public function loginCheckAction() {
}
}
如果我以匿名用户身份访问登录页面,我会看到登录表单。但是在我登录并加载登录页面后,我收到 500 服务器错误:
The security context contains no authentication token. One possible reason may be that there is no firewall configured for this URL.
我在另一个主题中读到的一个建议修复方法是将登录页面放在防火墙后面。我这样做了,但仍然错误 500。
我究竟做错了什么?
谢谢!