简体   繁体   English

symfony2中的身份验证系统无法正常工作?

[英]Authentification system in symfony2 is not working?

I have a problem with my authentification system in symfony2. 我在symfony2中的身份验证系统有问题。

My routing file : 我的路由文件:

shop_show_login_page:
  path: /login
  defaults: { _controller: ShopDesktopBundle:User:loginPage }
shop_login_user:
  path: /loginUser
  defaults: { _controller: ShopDesktopBundle:User:loginCheck }
shop_logout_user:
  path: /logout

My User controller : 我的用户控制器:

class UserController extends Controller{
public function loginPageAction(){
    return $this->render('ShopDesktopBundle:User:loginPage.html.twig');
}
public function loginCheckAction(Request $request){
    $password = $request->request->get('password');
    $login    = $request->request->get('username');
    $em = $this->getDoctrine()->getEntityManager();
    $repository = $em->getRepository('ShopDesktopBundle:Customer');
    $user = $repository->findOneBy(array('customer_login'=> $login, 'customer_password'=> $password));
    if($user){
        return $this->redirect($this->generateUrl('shop_desktop_homepage'));
    }else{
        return $this->render('ShopDesktopBundle:User:loginPage.html.twig',array('message_failed' => 'Eroare : wrong login'));
    }
}
}

My view : 我的观点 :

{% if message_failed is defined %}
     <div class="alert alert-danger">
         <button type="button" class="close" data-dismiss="alert">×</button>
         <strong>{{ message_failed }}</strong>
      </div>
{% endif %}
<form action="{{ path('shop_login_user') }}" method="post">
       <div class="form-group">
         <div class="input-group">
           <span class="input-group-addon"><i class="fa fa-user"></i></span>
           <input type="text" class="form-control" placeholder="Username" name="username">
         </div>
      </div>
      <div class="form-group">
         <div class="input-group">
            <span class="input-group-addon"><i class="fa fa-lock"></i></span>
            <input type="text" class="form-control" placeholder="Password" name="password">
          </div>
      </div>
     <div class="form-group">
         <button type="submit" class="button">Authentification</button>
     </div>
</form>

The security.yml: security.yml:

security:
encoders:
    Symfony\Component\Security\Core\User\User: plaintext

# http://symfony.com/doc/current/book/security.html#hierarchical-roles
role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
    in_memory:
        memory:
            users:
                user:  { password: userpass, roles: [ 'ROLE_USER' ] }
                admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }

# the main part of the security, where you can set up firewalls
# for specific sections of your app
firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    secured_area:
        pattern:    ^/
        form_login:
            check_path: shop_login_user
            login_path: shop_show_login_page
        logout:
            invalidate_session: true
            path:   shop_logout_user
            target: /
        anonymous: true
        #http_basic:
        #    realm: "Secured Demo Area"

# with these settings you can restrict or allow access for different parts
# of your application based on roles, ip, host or methods
# http://symfony.com/doc/current/cookbook/security/access_control.html
access_control:
    #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }

The debug toolbar shows that I authentificated as anonym after submit login form for all situations and I don't understand where is the problem. 调试工具栏显示,在所有情况下都提交登录表单后,我已通过匿名身份验证,我不知道问题出在哪里。 Exist a solution? 存在解决方案? Can anyone help me?. 谁能帮我?。 Thx in advance 提前Thx

try adding underscores in the values of the "name" attributes of your input elements. 请尝试在输入元素的“名称”属性的值中添加下划线。 As far as I know, _username and _password are the default values as shown in the docs. 据我所知, _username_password是默认值,如文档所示。

http://symfony.com/doc/current/reference/configuration/security.html http://symfony.com/doc/current/reference/configuration/security.html

<input type="text" class="form-control" placeholder="Username" name="_username">

Also, you must not specify a controller for the check_path because Symfony handles this internally. 另外,您不得为check_path指定控制器,因为Symfony在内部进行处理。 Check this link for more reference. 检查此链接以获取更多参考。

http://symfony.com/doc/current/cookbook/security/form_login_setup.html http://symfony.com/doc/current/cookbook/security/form_login_setup.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM