Symfony 登录表单 [英] Symfony login form
本文介绍了Symfony 登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Symfony 的新手,我正在尝试为我的应用程序创建一个访问登录表单,但是当我尝试登录时,它只会一次又一次地将我重定向到表单登录.在此之前,我使用了 HTTP 基本表单,它运行良好,但现在我正在尝试使用登录表单,但这是不可能的.
I'm new on Symfony and I'm trying to do a access login form for my app but when I try to logged, it only redirect me to the form login again and again. Before of that, I used HTTP basic form and it worked perfectly, but now I'm trying with login form and it's impossible.
这是我的代码:
用户类(带教义):
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Persona
*
* @ORM\Table(name="persona", uniqueConstraints={@ORM\UniqueConstraint(name="dni", columns={"dni"}), @ORM\UniqueConstraint(name="correo", columns={"correo"}), @ORM\UniqueConstraint(name="username", columns={"username"})}, indexes={@ORM\Index(name="id_localidad", columns={"id_localidad"}), @ORM\Index(name="id_lugar_trabajo", columns={"id_lugar_trabajo"}), @ORM\Index(name="id_puesto", columns={"id_puesto"}), @ORM\Index(name="id_grupo", columns={"id_grupo"}), @ORM\Index(name="nombre", columns={"nombre"}), @ORM\Index(name="prioridad", columns={"id_prioridad"}), @ORM\Index(name="apellidos", columns={"apellidos"})})
* @ORM\Entity
*/
class Persona implements UserInterface, \Serializable
{
/**
* @var string
*
* @ORM\Column(name="dni", type="string", length=10, nullable=false)
*/
private $dni;
/**
* @var string
*
* @ORM\Column(name="nombre", type="string", length=256, nullable=false)
*/
private $nombre;
/**
* @var string
*
* @ORM\Column(name="apellidos", type="string", length=256, nullable=false)
*/
private $apellidos;
/**
* @var string
*
* @ORM\Column(name="telefono", type="string", length=256, nullable=false)
*/
private $telefono;
/**
* @var string
*
* @ORM\Column(name="direccion", type="string", length=256, nullable=false)
*/
private $direccion;
/**
* @var string
*
* @ORM\Column(name="correo", type="string", length=256, nullable=false)
*/
private $correo;
/**
* @var integer
*
* @ORM\Column(name="codigo_postal", type="integer", nullable=false)
*/
private $codigoPostal;
/**
* @var boolean
*
* @ORM\Column(name="es_tecnico", type="boolean", nullable=false)
*/
private $esTecnico;
/**
* @ORM\Column(type="string", length=25, unique=true, nullable=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64, nullable=true)
*/
private $password;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \AppBundle\Entity\Puesto
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Puesto")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_puesto", referencedColumnName="id")
* })
*/
private $idPuesto;
/**
* @var \AppBundle\Entity\Grupo
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Grupo")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_grupo", referencedColumnName="id")
* })
*/
private $idGrupo;
/**
* @var \AppBundle\Entity\LugarTrabajo
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\LugarTrabajo")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_lugar_trabajo", referencedColumnName="id", nullable=false)
* })
*/
private $idLugarTrabajo;
/**
* @var \AppBundle\Entity\Localidad
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Localidad")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_localidad", referencedColumnName="id", nullable=false)
* })
*/
private $idLocalidad;
/**
* @var \AppBundle\Entity\Prioridad
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Prioridad")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_prioridad", referencedColumnName="id", nullable=false)
* })
*/
private $idPrioridad;
SecurityController.php:
<?php
// src/AppBundle/Controller/SecurityController.php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
class SecurityController extends Controller
{
/**
* @Route("/login", name="login")
*/
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(
'security/login.html.twig',
array(
// last username entered by the user
'last_username' => $lastUsername,
'error' => $error,
)
);
}
}
Security.yml
security:
encoders:
AppBundle\Entity\Persona:
algorithm: bcrypt
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
our_db_provider:
entity:
class: AppBundle:Persona
property: username
# if you're using multiple entity managers
# manager_name: customer
firewalls:
login_firewall:
pattern: /login
anonymous: ~
secured_area:
pattern: /
anonymous: ~
provider: our_db_provider
pattern: ^/
form_login:
login_path: login
check_path: login_check
#http_basic: ~
# http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
access_control:
# require ROLE_USER for /*
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_USER }
login.html.twig
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>-Incidencias- Acceso</title>
</head>
<body>
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<form action="{{ path('login') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
{#
If you want to control the URL the user
is redirected to on success (more details below)
<input type="hidden" name="_target_path" value="/account" />
#}
<button type="submit">login</button>
</form>
</body>
</html>
有什么想法吗?谢谢!!!!!!
Any idea? Thanks!!!!!
推荐答案
最后我解决了这个问题.这是我的安全措施,以防有人需要.
Finally I solved this. Here is my security in case someone needs it.
security:
encoders:
AppBundle\Entity\Persona:
algorithm: bcrypt
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
our_db_provider:
entity:
class: AppBundle:Persona
property: username
# if you're using multiple entity managers
# manager_name: customer
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
provider: our_db_provider
anonymous: ~
form_login:
login_path: login
check_path: login
logout:
path: /logout
target: /
#http_basic: ~
# http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate
access_control:
# require ROLE_USER for /*
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_USER }
谢谢大家!!
这篇关于Symfony 登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文