我有一个 symfony 项目,经理two
参与其中website
,ionic app
所以当我安装JWT Authentication
以获取token
应用程序时,Login Form in website not working
我不知道为什么?
这个问题在我使用时FOSUserBundle
也会发生。
在我添加 JWT 之前,登录表单运行良好
安全.yml
security:
encoders:
UserBundle\Entity\User:
algorithm: bcrypt
# https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
providers:
db_provider:
entity:
class: UserBundle:User
#property: username to login just with the username
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: ^/api/login
stateless: true
anonymous: true
form_login:
check_path: /api/login_check
username_parameter: username
password_parameter: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
require_previous_session: false
api:
pattern: ^/api
stateless: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
main:
anonymous: ~
logout:
path: /logout
target: /
access_control:
# for the website
- { path: ^/login , roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register , roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/home , roles: IS_AUTHENTICATED_FULLY }
# for the app
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }`
登录控制器.php
<?php
namespace UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class LoginController extends Controller
{
/**
* @Route("/login" ,name="login")
*/
public function loginAction(Request $request, AuthenticationUtils $authenticationUtils)
{
$errors = $authenticationUtils->getLastAuthenticationError();
$lastUserName = $authenticationUtils->getLastUsername();
return $this->render('@User\Login\login.html.twig',array(
'errors'=>$errors,
'username'=>$lastUserName,
));
}
/**
* @Route("/logout" ,name="logout")
*/
public function logoutAction(){}
}
登录.html.twig
{% extends 'base.html.twig' %}
{% block body %}
<div class="panel panel-success" style="width: 500px;">
<div class="panel-heading">
<h2>Login</h2>
</div>
<div class="panel-body">
<form action="{{ path('login') }}" method="post">
<div class="form-group">
<label for="username">Username : </label>
<input type="text" name="_username" id="username" class="form-control" value="{{ username }}">
</div>
<div class="form-group">
<label for="password">Password : </label>
<input type="password" name="_password" id="password" class="form-control">
</div>
<div class="form-group">
<button class="btn-info btn pull-right">Login</button>
</div>
</form>
</div>
{% if errors %}
<div class="alert alert-danger">
{{ errors.messageKey }}
</div>
{% endif %}
</div>
{% endblock %}
路由.yml
api_login_check:
path: /api/login_check
问题是当我尝试登录时,它会返回相同的登录页面而没有任何错误。
当我尝试127.0.0.1:8000/api/login_check
时,Postman
我得到了Token