Symfony 2.1安全登录检查错误

问题描述 投票:-1回答:1

我正在构建登录表单,如symfony 2.1文档中所示。我在做同样的事情。我在简单的授权方面取得了成功,但是当我创建简单的登录表单时,我找不到login_check路由。如果我把login_check路由然后我得到login_check错误。 文档没有说明这一点。我的security.yml是

# app/config/security.yml
security:
  providers:
    in_memory:
        memory:
            users:
                ryan:  { password: ryanpass, roles: 'ROLE_USER' }
                admin: { password: kitten, roles: 'ROLE_ADMIN' }

firewalls:
    login:
        pattern:  ^/login$
        security: false
    secured_area:
        pattern:    ^/admin
        anonymous: false
        form_login:
            login_path: /login
            check_path: /login_check
            always_use_default_target_path: false
            default_target_path:            /admin/content/index
            target_path_parameter:          _target_path
            use_referer:                    false
            username_parameter:             _username
            password_parameter:             _password
            csrf_parameter:                 login[_token]
        logout: 
            path: /admin/logout
            target: /login
access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin, roles: ROLE_ADMIN }

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

和routing.yml是

login:
pattern:  /login
defaults: { _controller: AcmeTaskBundle:Default:login}

login_check:
pattern: /login_check        
defaults: { _controller: AcmeTaskBundle:Default:loginCheck}

logout:
pattern: /admin/logout
defaults: { _controller: AcmeTaskBundle:Default:logout}
content_index:
pattern: /admin/content/index
defaults: { _controller: AcmeTaskBundle:Default:index }

我的控制器登录操作是

namespace Acme\TaskBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Acme\TaskBundle\Entity\Product;
use Acme\TaskBundle\Entity\CmsContentMst;
use Acme\TaskBundle\Entity\CmsSectionsMst;

// use Acme\TaskBundle\Entity\ProductType;
use Symfony\Component\HttpFoundation\Response;
use Acme\TaskBundle\Form\Type\AddContent;
use Symfony\Component\Security\Core\SecurityContext;

class DefaultController extends Controller
{   
public function loginAction()
{
    $request = $this->getRequest();
    $session = $request->getSession();

    // get the login error if there is one
    if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
        $error = $request->attributes->get(
            SecurityContext::AUTHENTICATION_ERROR
        );
    } else {
        $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
        $session->remove(SecurityContext::AUTHENTICATION_ERROR);
    }

    return $this->render(
        'AcmeTaskBundle:Default:login.html.twig',
        array(
            // last username entered by the user
            'last_username' => $session->get(SecurityContext::LAST_USERNAME),
            'error'         => $error,
        )
    );
  }
public function panelAction(Request $request)
{   

}
public function loginCheckAction()
  { 

    return new Response('true');

  }

和login.html.twig是

{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}
{% if error %}
  <div>{{ error.message }}</div>
{% endif %}

 <form action="{{ path('login_check') }}" 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>

我应该在登录check.controller中添加什么...如果有人在symfony中有完整的登录示例2.1 plz将其置于答案中..

php symfony symfony-2.1 symfony-forms
1个回答
0
投票

您不必为login_check创建控制器。 Symfony已经默认定义了它的功能。我想你不想搞砸它。如果要在登录后执行操作,可以在服务等上定义操作。

这可能有助于Symfony2: After successful login event, perform set of actions

© www.soinside.com 2019 - 2024. All rights reserved.