我如何使用symfony 6和react js构建身份验证?



在我的应用程序中,我不需要注册功能,所以我手动在数据库中添加了一个用户,实际上我尝试了lexjwtauthenticationbundle我遵循文档,但不幸的是,当我使用cURL发送请求时,我得到以下错误

<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<hr>
<address>Apache/2.4.52 (Win64) OpenSSL/1.1.1m PHP/8.1.4 Server at localhost Port 443</address>
</body></html>```
**the cURL request is 

curl -X POST -H "内容类型:application/json;https://localhost/api/check_login——data '{"email":"johndoe@gmail.fr";password"; "test"}' -k


Moreover frankly i m confused about the logic of the bundle so im worried if i need a controller or not .
However in the frontENd i m using reactJS i just sent a post request using axios 
but when i hit submit in the network when i inspect i get 
{
"code": 401,
"message": "JWT Token not found"
}

我认为文档只是展示了一个例子。不确定这是否可以开箱即用,因为在安装它之后,我还没有看到一个新的控制器来处理login_check路由。实际上,要求手动添加路由,而不引用资源。

要从用户生成Token,您需要在控制器中实例化Jwt。

// src/Controller/AuthController.PHP
use AppEntityUser;
use LexikBundleJWTAuthenticationBundleServicesJWTTokenManagerInterface;
class AuthController
{

private JWTTokenManagerInterface $jwt;
public function __construct(JWTTokenManagerInterface $jwt)
{
$this->jwt = $jwt;
}
#[Route(path: '/token', name: 'api_auth_token')]
public function login(Request $request): Response
{
// Code to retrieve your user 
// [...]
// User must extends UserInterface for Lexik to work properly
$token = $this->jwt->create($user);
// return the token 
}

这段代码将向您展示如何创建令牌,但不用于登录。您必须检查您的身份验证流程。

旁注:不要混淆散列密码和生成令牌。你需要分别做这两件事

我建议您通过探索自动布线来查找您需要的服务:

php bin/console debug:autowiring jwt

最新更新