我正在使用symfony 3創建一個應用程序,該應用程序將用於爲槍支範圍預留通道。我遵循symfony 3文檔獲取登錄和註冊表單設置和配置。我的註冊表單正在工作,但我的登錄表單不起作用。我只是得到「無效的憑證」。無論如何回到我身邊。Symfony 3身份驗證/登錄表單不能正常工作
以下是我的安全YML。
# To get started with security, check out the documentation:
# http://symfony.com/doc/current/book/security.html
security:
hide_user_not_found: false
encoders:
AppBundle\Entity\User:
algorithm: bcrypt
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
our_db_provider:
entity:
class: AppBundle:User
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_check
csrf_token_generator: security.csrf.token_manager
username_parameter: _username
password_parameter: _password
logout: true
anonymous: true
access_control:
- { path: ^/profile, roles: ROLE_USER }
- { path: ^/reservation, roles: ROLE_USER }
這是我的登錄控制器。
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Form\UserType;
use AppBundle\Entity\User;
class LoginController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request)
{
// loads security utilities
$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();
// renders route
return $this->render('default/login.html.twig', [
'year' => date("Y"),
'error' => $error,
'last_user' => $lastUsername,
]);
}
/**
* @Route("/login_check", name="login_check")
*/
public function loginCheckAction()
{
}
}
這是我的倉庫,因此您可以用電子郵件或用戶名
<?php
namespace AppBundle\Repository;
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository implements UserLoaderInterface {
public function loadUserByUsername($username)
{
$user = $this->createQueryBuilder('u')
->where('u.username = :username OR u.email = :email')
->setParameter('username', $username)
->setParameter('email', $username)
->getQuery()
->getOneOrNullResult();
if (null === $user) {
$message = sprintf(
'Unable to find an active admin AppBundle:User object identified by "%s".',
$username
);
throw new UsernameNotFoundException($message);
}
return $user;
} }
登錄這是我的用戶實體
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @Assert\NotBlank()
* @Assert\Length(max = 4096)
*/
public $plainPassword;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="string", length=60, unique=true)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
public function __construct()
{
$this->isActive = true;
}
public function getUsername()
{
return $this->username;
}
public function getSalt()
{
return null;
}
public function getPassword()
{
return $this->password;
}
public function getPlainPassword()
{
return $this->password;
}
public function getRoles()
{
return array('ROLE_USER');
}
public function eraseCredentials()
{
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
) = unserialize($serialized);
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
*
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Set email
*
* @param string $email
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set isActive
*
* @param boolean $isActive
*
* @return User
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
}
不知道怎麼回事,但我會很感激一些幫助。
感謝, 羅伯特
您是如何將用戶導入數據庫的?我懷疑它有一個不正確的編碼密碼? – Cerad
我根據symfony的文檔使用了教義:http://symfony.com/doc/current/cookbook/doctrine/registration_form.html 編輯:在鏈接之前意外提交。 – Psyco430404
您的登錄表單中是否有適當的字段名稱? –