在symfony3中进行身份验证

时间:2016-10-20 08:51:56

标签: symfony symfony-forms

我尝试在symfony3中创建身份验证但不起作用。我没有错误,在开发栏中我有Interceptor。我的实体:

Logged in as anon

我的路由文件:

<?php

namespace App\DesktopBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Member
*
* @ORM\Table(name="member")
* @ORM\Entity(repositoryClass="App\DesktopBundle\Repository   \MemberRepository")
*/
class Member
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="username", type="string", length=255)
 */
private $username;

/**
 * @var string
 *
 * @ORM\Column(name="email", type="string", length=255)
 */
private $email;

/**
 * @var string
 *
 * @ORM\Column(name="password", type="string", length=255)
 */
private $password;


/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * Set nickname
 *
 * @param string username
 *
 * @return Member
 */
public function setUsername($username)
{
    $this->username = $username;

    return $this;
}

/**
 * Get username
 *
 * @return string
 */
public function getUsername()
{
    return $this->username;
}

/**
 * Set email
 *
 * @param string $email
 *
 * @return Member
 */
public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

/**
 * Get email
 *
 * @return string
 */
public function getEmail()
{
    return $this->email;
}

/**
 * Set password
 *
 * @param string $password
 *
 * @return Member
 */
public function setPassword($password)
{
    $this->password = $password;

    return $this;
}

/**
 * Get password
 *
 * @return string
 */
public function getPassword()
{
    return $this->password;
}
}

我的控制器:

app_desktop_homepage:
path:     /
defaults: { _controller: AppDesktopBundle:Default:index }
login:
path:   /login
defaults: { _controller: AppDesktopBundle:Member:login }

我的安全档案:

namespace App\DesktopBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class MemberController extends Controller{


public function loginAction(Request $request)
{
    $authenticationUtils = $this->get('security.authentication_utils');

    // get the login error if there is one
    $error = $authenticationUtils->getLastAuthenticationError();

    // last username entered by the user
    $lastUsername = $authenticationUtils->getLastUsername();

    return $this->render('AppDesktopBundle:Header:disconnected.html.twig', array(
        'last_username' => $lastUsername,
        'error'         => $error,
    ));
  }
}

我的模板:

security:
hide_user_not_found: false
encoders:
    AppDesktopBundle\Entity\Member:
        algorithm: bcrypt

# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
    our_db_provider:
        entity:
            class: AppDesktopBundle:Member
            property: username

firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        pattern: ^/
        provider: our_db_provider
        form_login:
            login_path: login
            check_path: login
            csrf_token_generator: security.csrf.token_manager
            username_parameter: _username
            password_parameter: _password
        logout: true
        anonymous: true

我不明白问题出在哪里。当我提交表单时,如果用户存在或其他原因我没有错误。

1 个答案:

答案 0 :(得分:0)

在安全性部分中,尝试添加access_control信息作为示例:

security.yml

access_control:
    # URL which need to be available to anonymous users
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, role: ROLE_USER }

希望这个帮助