Php Symfony 3无法使用ajax登录
首先,我没有在这个项目中使用FOSUserBundle。所以,让我们把它放一边 我可以注册一个用户,但无法让他登录 我验证了表单已发布到LoginController,并且它生成了authenticationUtils对象,但它在未生成错误之前就停止了,即使下面一行有错误: $error=$authUtils->getLastAuthenticationError() 否则不会产生任何错误。它默默地失败了。即使我提供了正确的凭据,_wdt仍然显示为匿名 security.ymlPhp Symfony 3无法使用ajax登录,php,ajax,symfony,security,login,Php,Ajax,Symfony,Security,Login,首先,我没有在这个项目中使用FOSUserBundle。所以,让我们把它放一边 我可以注册一个用户,但无法让他登录 我验证了表单已发布到LoginController,并且它生成了authenticationUtils对象,但它在未生成错误之前就停止了,即使下面一行有错误: $error=$authUtils->getLastAuthenticationError() 否则不会产生任何错误。它默默地失败了。即使我提供了正确的凭据,_wdt仍然显示为匿名 security.yml security
security:
encoders:
UsedBundle\Entity\User:
algorithm: bcrypt
providers:
db_provider:
entity:
class: UsedBundle:User
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
provider: db_provider
form_login:
username_parameter: _email
登录控制器:
UsedBundle\Controller\LoginController
namespace UsedBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
class LoginController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request)
{
if ($request->isMethod('POST')) {
var_dump($_POST);
$authUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authUtils->getLastAuthenticationError();
var_dump($error);
var_dump($authUtils);
if(isset($error)){
$message = $error->getMessageData();
var_dump($message);
}
// last username entered by the user
$lastUsername= $authUtils->getLastUsername();
return new JsonResponse(array(
'last_username' => $lastUsername,
'error' => $error,)
);
}else{
return $this->render('common/login.html.twig');
}
}
}
表单模板:
app\Resources\Views\Common\login.html.twig
{% if error is defined and error is not null %}
{{ dump(error) }}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% else %}
<form action="" method="post" name="login_form" id="login_form" >
<div class="contact" >
<input type="email" id="email" name="_email" class="form-control" placeholder="e-mail" value=" {% if last_username is defined %}
{{ last_username }}
{% endif %}
" />
</div>
<div class="contact" >
<input type="password" id="password" name="_password" placeholder="mot de passe" />
</div>
<div>
<button type="submit" class="sub_ok btn btn-sm" name="submit" >Valider</button>
</div>
</form>
{% endif %}
表单通过Ajax提交,如下所示:
$(document).ready(function() {
$('#login_form').submit(function(e) {
var str = $("#login_form").serialize();
$.ajax({
url: "/login",
type: "POST",
dataType: "json",
data: str,
success: function(data) {
alert(data);
}
e.preventDefault(); //STOP default action
});
});
});
我在下面添加Firebug的输出。我不确定它是如何产生的,但它显示了role属性为空。还是不知道为什么。所有用户在数据库上都具有角色\用户
object(Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken)#2384 (6) {
["credentials":"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":private]=>
string(8) "senha444"
["providerKey":"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":private]=>
string(4) "main"
["user":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
string(22) "myemail@gmail.com"
["roles":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
array(0) {
}
["authenticated":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
bool(false)
["attributes":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
array(0) {
}
这不会像上面设置的那样工作。Symfony不能很好地使用Ajax,因此需要自定义登录
我在整个设置中添加了一个答案。我最近添加了一个类似的问题,问题是我试图不使用HTTPS登录,而在config.yml framework.session.cookie中,安全是正确的。因此,应用程序无法创建cookie。不知道这是否是同一个问题,但我无法登录并且没有错误。谢谢@Picoss,我在config.yml上根本没有设置该参数。我确实检查了/web目录中的php.ini,但它没有关于该目录的指令,而且MAMP的php info session.cookie\u secure和session.cookie\u httponly都处于禁用状态。嗯,我认为是您发送帖子的url不正确。您不应在/login上提交表单,而应在/login\u check上提交表单。或者,如果要在/login上发布登录表单,请添加您的防火墙配置登录路径并选中路径。看看Symfony2文档中关于“如何构建一个三元登录表单”:@Picoss,我认为在这个例子中没有必要这样做。Ajax执行到正确控制器的路由。设置是这样的,每个页面都有一个登录表单。如果我把它放在上面,它会无缘无故地开始重定向。@BernardA当你检查探查器时,你能检查对
/login\u check
URL的请求吗?您可以使用探查器中的“last 10”按钮检查上一个请求。我还建议您使用,它为您提供了dump
功能,而不是var\u dump
?
object(Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken)#2384 (6) {
["credentials":"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":private]=>
string(8) "senha444"
["providerKey":"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":private]=>
string(4) "main"
["user":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
string(22) "myemail@gmail.com"
["roles":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
array(0) {
}
["authenticated":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
bool(false)
["attributes":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
array(0) {
}