首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony 3-身份验证

Symfony 3-身份验证
EN

Stack Overflow用户
提问于 2016-06-29 14:08:35
回答 1查看 802关注 0票数 1

我正在尝试在Symfony 3中进行身份验证。例如How to Load Security Users from the Database

实体用户和模式数据库,我很喜欢这些文档。我尝试将密码与md5、bcrypt、明文一起使用

Security.yml

代码语言:javascript
复制
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

控制器:

代码语言:javascript
复制
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
    ]);
}

如何将错误数据提供给登录以获取

代码语言:javascript
复制
exception 'Symfony\Component\Security\Core\Exception\BadCredentialsException' with message 'Bad credentials.'

正如我将更正的那样,它不会将我重定向到admin_dashboard,也不会返回任何错误。调试我仍然是匿名的

在数据库中,我有一个角色ROLE_ADMIN,我们在getRoles中有一个返回给User实体的角色。

怎么啦?

EN

回答 1

Stack Overflow用户

发布于 2016-08-06 08:37:16

查看Symfony文档。记住,你的用户实体必须实现UserInterface类。here you can find what you need

您还可以实现自己的身份验证系统

代码语言:javascript
复制
class WeirdFormAuthenticator extends AbstractGuardAuthenticator
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38091575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档