我已尝试过每本手册的所有配置,仍然无法让它工作! 有人可以帮我找到错误吗?这是我的代码:
Security.yml
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
firewalls:
main:
pattern: ^/
provider: in_memory
anonymous: true
security: true
form_login:
login_path: default_login
check_path: default_check
logout:
path: default_logout
target: default_index
remember_me:
key: %secret%
lifetime: 3600
domain: ~
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
#- { path: ^/afiliados, roles: ROLE_SUPER_ADMIN}
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
#- { path: ^/_internal, roles: IS_AUTHENTICATED_ANONYMOUSLY, ip: 127.0.0.1 }
DefaultController.php
namespace Epika\ClubBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Security\Core\SecurityContext;
/**
* Default controller.
*
*/
class DefaultController extends Controller
{
/**
* @Route("/login", name="default_login")
* @Template()
*/
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);
}
return array(
// last username entered by the user
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error
);
}
/**
* @Route("/login_check", name="default_check")
*
*/
public function loginCheckAction()
{
//Security Layer
}
/**
* @Route("/logout", name="default_logout")
*/
public function logoutAction()
{
//Security Layer
}
}
我的表格中有
<form name="loginForm" action="{{ path('default_check') }}" method="POST" enctype="text/plain">
<input type="text" name="_username" placeholder="Nombre de Usuario" />
<input type="password" name="_password" placeholder="Tú Contraseña" />
<input type="checkbox" name="_remember_me" />
<input type="hidden" name="_target_path" value="/index" />
<a href="javascript:submit()"><img src="{{ asset('bundles/epikaclub/images/entrar.png') }}" /></a>
<script type="text/javascript">
function submit()
{
document.loginForm.submit();
}
</script>
OK大家非常感谢您的帮助,问题是登录表单的enctype设置为
<form name="loginForm" action="{{ path('login_check') }}" method="post" enctype="text/plain">
导致请求为空:对不起,一旦我将其设置为
<form name="loginForm" action="{{ path('login_check') }}" method="post" enctype="application/x-www-form-urlencoded">
它工作正常,现在我必须与Doctrine Entity Provider一起检查它,看看它是怎么回事:D
答案 0 :(得分:3)
我相信你需要:
这里的一个旁注中解释了这一点:http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form
即使输入正确的信息,“错误凭证”错误也只是指出某些地方发生了一些错误。检查错误日志是否仍然无法解决上面提到的问题并告诉我们。
答案 1 :(得分:0)
您可能需要将login_path和check_path值调整为实际的URL路径,如下所示:
firewalls:
[...]
main:
[...]
form_login:
login_path: /login
check_path: /login_check
萨姆
答案 2 :(得分:0)
与其他选项不同,check_path
不支持路由名称。将其设置为/login_check
。