douzhan1238 2014-11-07 22:14
浏览 99
已采纳

Symfony 2.3登录表单未进行身份验证

I'm trying to implement a rather basic login form with Symfony2.3, but I'm running into an error where I'm sometimes redirected to the expected page after providing correct credentials, but sometimes not (instead I'm just redirected back to the login page). Here is my security.yml file:

security:
    encoders:
        Symfony\Component\Security\Core\User\User: plaintext
        Acme\MyBundle\Entity\User: sha512
    providers:
        main:
            id: acme.user.provider
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        login_firewall:
            pattern: ^/login$
            security: false
        secured_area:
            pattern: ^/
            form_login: ~
            logout:
                path:   /logout
                target: /
    access_control:
        - { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
        - { path: ^/, roles: ROLE_USER, requires_channel: https }

Here is my SecurityController:

<?php

namespace Acme\MyBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContextInterface;

/**
 * Class SecurityController
 * @package Acme\MyBundle\Controller
 *
 * @Route("/")
 */
class SecurityController extends Controller
{
    /**
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @Route("/login", requirements={"_scheme" = "https"}, path="login")
     */
    public function LoginAction(Request $request)
    {
        $session = $request->getSession();

        // get the login error if there is one
        if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(
                SecurityContextInterface::AUTHENTICATION_ERROR
            );
        } elseif (null !== $session && $session->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
            $error = $session->get(SecurityContextInterface::AUTHENTICATION_ERROR);
            $session->remove(SecurityContextInterface::AUTHENTICATION_ERROR);
        } else {
            $error = '';
        }

        // last username entered by the user
        $lastUsername = (null === $session) ? '' : $session->get(SecurityContextInterface::LAST_USERNAME);

        return $this->render(
            'AcmeMyBundle:Security:login.html.twig',
            array(
                // last username entered by the user
                'last_username' => $lastUsername,
                'error'         => $error,
            )
        );
    }

    /**
     * @Route("/login_check", requirements={"_scheme" = "https"}, path="login_check")
     */
    public function LoginCheckAction()
    {

    }

    /**
     * @Route("/logout", requirements={"_scheme" = "https"}, path="logout")
     */
    public function LogoutAction()
    {

    }
}

And here is my bundle's routing.yml file:

_security:
    resource: "@AcmeMyBundle/Controller/SecurityController.php"
    type: annotation

When I provide the proper credentials, I'm correctly logged in / redirected to the given URL about 20% of the time. The other 80% of the time I'm just redirected back to the form login page with no error message. I also don't see any error message when I try to login with incorrect credentials.

UPDATE: It looks like the issue is that Symfony is creating more than one session in my database on each request. I am using the pdo handler.

  • 写回答

2条回答 默认 最新

  • dongxun7962 2014-11-10 14:44
    关注

    Turns out the issue was actually with the Session entity / table in which I was storing my session. I was using the PDOSessionHandler to store sessions in the database, and had the following $id in my ORM Session entity:

    <?php
    
    namespace Acme\MyBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * Session
     *
     * @ORM\Table()
     * @ORM\Entity
     */
    class Session
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    

    The $id should not have been of type integer, but (obviously (-_-) of type string. After changing the property to:

    /**
     * @var integer
     *
     * @ORM\Column(type="string", length=255)
     * @ORM\Id
     */
    private $id;
    

    and running php app/console doctrine:schema:update --force, everything appears to be working correctly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵