我正在尝试在Symfony 3中进行身份验证。例如How to Load Security Users from the Database
实体用户和模式数据库,我很喜欢这些文档。我尝试将密码与md5、bcrypt、明文一起使用
Security.yml
security:
encoders:
AppBundle\Entity\User: plaintext
role_hierarchy:
ROLE_ADMIN: [ROLE_USER]
providers:
our_db_provider:
entity: { class: AppBundle:User, property: username }
firewalls:
main:
pattern: ^/
anonymous: ~
provider: our_db_provider
form_login:
login_path: /
check_path: /
logout:
path: /logout
target: /
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }在登录表单中,我有_username和_password
控制器:
public function indexAction(Request $request)
{
$session = $request->getSession();
$authenticationUtils = $this->get('security.authentication_utils');
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
if ($this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')){
return $this->redirect($this->generateUrl('admin_dashboard'));
}
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
'body_id' => "simple-page",
'last_username' => $lastUsername,
'error' => $error
]);
}如何将错误数据提供给登录以获取
exception 'Symfony\Component\Security\Core\Exception\BadCredentialsException' with message 'Bad credentials.'正如我将更正的那样,它不会将我重定向到admin_dashboard,也不会返回任何错误。调试我仍然是匿名的
在数据库中,我有一个角色ROLE_ADMIN,我们在getRoles中有一个返回给User实体的角色。
怎么啦?
发布于 2016-08-06 08:37:16
查看Symfony文档。记住,你的用户实体必须实现UserInterface类。here you can find what you need
您还可以实现自己的身份验证系统
class WeirdFormAuthenticator extends AbstractGuardAuthenticatorhttps://stackoverflow.com/questions/38091575
复制相似问题