我是 symfony2 的新手,我正在尝试在 symfony2 中学习登录页面,但系统给了我这个错误
安全上下文不包含身份验证令牌。一个可能的原因可能是没有为此 URL 配置防火墙。
我的 security.yml 代码是:
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: admin, roles: [ 'ROLE_ADMIN' ] }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/demo/secured/login$
security: false
emp:
pattern: ^/emp
form_login:
check_path: acme_radixemp_logincheck
login_path: acme_radixemp_login
logout:
path: acme_radixemp_logout
target: demo
#anonymous: ~
#http_basic:
# realm: "Secured Demo Area"
access_control:
- { path: ^/emp, roles: ROLE_ADMIN }
我正在尝试/emp
从 url运行
我在控制器中的登录功能是:
public function loginAction(Request $request)
{
$login = new user();
$form = $this->createFormBuilder($login)
->add('Username','text',array('attr' => array('size' => '50',"value"=>"",'class'=>'form-control','placeholder'=>"Please Enter Your Username")))
->add('Password', 'password',array('attr' => array('size' => '50',"value"=>"",'class'=>'form-control','placeholder'=>"Enter Your Password")))
->add('login', 'submit',array('attr' => array('class '=>"btn btn-success btn-lg","style"=>"margin-top:15px")))
->getForm()
;
$form->handleRequest($request);
$session = $this->getRequest()->getSession();
$url = $this->generateUrl("emp");
$loginurl = $this->generateUrl("acme_radixemp_login",array("login"=>"fail"));
$postData = $request->request->get('form');
if(!empty($postData)) {
$repository = $this->getDoctrine()->getRepository('AcmeRadixempBundle:user');
$user_name = $repository->findOneBy(array('username' => $postData['Username'],'password'=>$postData['Password']));
if($user_name=="") {
return $this->redirect($loginurl);
} else {
//$this->get('security.context')->isGranted('ROLE_ADMIN');
if(TRUE ===$this->get('security.context')->isGranted('ROLE_ADMIN') )
{
$session->set('username', $postData['Username']);
return $this->redirect($url);
}
}
}
}
return $this->render('AcmeRadixempBundle:Default:login.html.twig',array('login'=> $form->createView()));
}
我忘了做什么?