Symfony2,使用数据库登录,凭据错误,未执行数据库查询

时间:2015-11-23 10:41:19

标签: php database security symfony login

Symfony 2.7

我正在关注并修改example-project: http://sourceforge.net/p/p5chi-xshare/wiki/Home/

我收到错误的凭据错误。我不明白错误在哪里。第二天我就坐在这上面了。我查看了十几个论坛,更正了表单字段,security.yml。但错误仍然存​​在。

http://127.0.0.1:8000/acme/demo/secured/login 用户名:xshare,密码:xshare。

我相信错误可能出现在编码器中 如果我使用:

encoders:
    Symfony\Component\Security\Core\User\User: plaintext
    Xshare\UserBundle\Entity\User: sha512

我收到错误:凭据错误。 Symfony分析器显示“未执行任何数据库查询。 “

如果我尝试:

encoders:
    Xshare\UserBundle\Entity\User: sha512

我收到错误:凭据错误。 Symfony分析器显示“未执行任何数据库查询。 “

如果我尝试:

   encoders:
        Symfony\Component\Security\Core\User\User: plaintext
        Xshare\UserBundle\Entity\User: 
            algorithm: sha512
            encode_as_base64: false
            iterations: 10

我收到错误:控制器必须返回响应(给定null)。您是否忘记在控制器中的某处添加return语句? Symfony分析器显示“未执行任何数据库查询。 “ 或者我收到错误:找不到“GET /”的路由(来自“http://127.0.0.1:8000/acme/demo/secured/login”)。 Symfony分析器显示“未执行任何数据库查询。 “

C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ xshare \应用\配置\ security.yml

# To get started with security, check out the documentation:
# http://symfony.com/doc/current/book/security.html
security:

    # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
    providers:
        xshare_db: 
            entity: { class: XshareUserBundle:User, property: username }         

    encoders:
        Symfony\Component\Security\Core\User\User: plaintext
        Xshare\UserBundle\Entity\User: sha512

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        login:
            pattern:   ^/acme/demo/secured/login$
            security: false
            provider: xshare_db
            #anonymous: ~    
            # http://symfony.com/doc/current/cookbook/security/form_login_setup.html#avoid-common-pitfalls            

        default:
            pattern: ^/acme/demo/secured
            provider: xshare_db
            form_login:
                check_path: /acme/demo/secured/loginCheck
                login_path: /acme/demo/secured/login
            logout:  
                path: /acme/demo/secured/logout
                target: /acme/demo/secured/login


    access_control:
        - { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/, roles: [ROLE_USER, ROLE_ADMIN] }        

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN  

C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ xshare \应用\配置\ routing.yml中

logout:
    path: /acme/demo/secured/logout
    defaults: { _controller: Acme:DemoBundle:Secured:logout}

login:
    path: /acme/demo/secured/login
    defaults: { _controller: Acme:DemoBundle:Secured:login}    

login_check:
    path: /acme/demo/secured/loginCheck
    #route-path, acme_demo_secured_logincheck   ANY ANY ANY  /acme/demo/secured/loginCheck  
    defaults: { _controller: Acme:DemoBundle:Secured:loginCheck}

.. \ sym_prog \ xshare \ SRC \ Xshare \ UserBundle \实体\ user.php的

   <?php

    namespace Xshare\UserBundle\Entity;

    use Symfony\Component\Security\Core\User\UserInterface;
    use …

    /**
     * Xshare\UserBundle\Entity\User
     *
     * @ORM\Table(name="user", indexes = {@ORM\Index(name="search_idx", columns={"firstname", "lastname", "created_at"})})
     * @ORM\Entity(repositoryClass="Xshare\UserBundle\Repository\UserRepository")
     * @ORM\HasLifecycleCallbacks()
     * @UniqueEntity(fields="email", message="user.email.not_unique")
     * @UniqueEntity(fields="username", message="user.login.not_unique")
     */
    class User implements UserInterface, \Serializable
    {
        /**
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $user_id;

        /**
         * @var string $username
         * @ORM\Column(type="string", length=25, unique=true)
         * @Assert\NotBlank(
         *      message="user.not_blank"
         * )
         */
        private $username;

….

C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ xshare \ SRC \ Xshare \ UserBundle \ DataFixtures \ ORM \ UserFixtures01.php

<?php

namespace Xshare\UserBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use ...

/**
 * Description of UserFixtures
 */
class UserFixtures01 extends AbstractFixture implements OrderedFixtureInterface {

    public function load(ObjectManager $manager)
    {
        //simple user
        $user1 = new User();
        $user1->setActive(1);
        $user1->setBirthDate('11/07/1989');
        $user1->setEmail('xshare@xshare.com');
        $user1->setFirstname('xshare');
        $user1->setLastname('xshare');
        $user1->setPassword('xshare');

        $encoder = new MessageDigestPasswordEncoder('sha512', false, 10);
        $password = $encoder->encodePassword($user1->getPassword(), $user1->getSalt());
        $user1->setPassword($password);        

        $user1->setPhone('079256354');
        $user1->setSex('m');
        $user1->setTypeAccess('user');
        $user1->setUsername('xshare');
        $manager->persist($user1);        
       ….
        $manager->flush();

    }

}

?>

C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ xshare \ SRC \ Acme公司\ DemoBundle \控制器\ SecuredController.php

<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use ...
/**
 * @Route("/demo/secured")
 */
class SecuredController extends Controller
{
    /**
     * @Route("/login", name="_demo_login")
     */
    public function loginAction()
    {
        if ($this->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $this->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
        } else {
            $error = $this->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
        }

     return $this->render( 'AcmeDemoBundle:Secured:login.html.twig',
        array('error'         => $error,

         ) );


    }

    /**  
     * @Route("/loginCheck")
     */
    public function loginCheckAction()
    //public function securityCheckAction()
    {
        // The security layer will intercept this request
    }

    /**
     * @Route("/logout", name="_demo_logout")
     */
    public function logoutAction()
    {
        // The security layer will intercept this request
    }

    /**
     * @Route("/logoutCheck")
     */
    public function logoutCheckAction()
    //public function logoutAction()
    {
        // The security layer will intercept this request
    }

C:\ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ xshare \ SRC \ Acme公司\ DemoBundle \资源\视图\担保\ login.html.twig

{% extends 'AcmeDemoBundle::layout.html.twig' %}

{% block content %}
    <h1>Login</h1>

    <p>
        Choose between two default users: <em>user/userpass</em> <small>(ROLE_USER)</small> or <em>admin/adminpass</em> <small>(ROLE_ADMIN)</small>
    </p>
    <p> There exists: user: xshare, password: xshare </p>

    {% if error %}
        <div class="error">{{ error.message }}</div>
    {% endif %}

    <form action="{{ path("acme_demo_secured_logincheck") }}" method="post" id="login">
        <div>
            <label for="username">Username</label>
            <input type="text" id="username" name="_username" value="" />
        </div>

        <div>
            <label for="password">Password</label>
            <input type="password" id="password" name="_password" />
        </div>

        <input type="submit" class="symfony-button-grey" value="LOGIN" />
    </form>
    {% endblock %}

    {% set code = code(_self) %}

0 个答案:

没有答案