使用 JWT 发送不正确的凭据时出现 401 错误?
401 error When sending incorrect credentials using JWT?
所以我正在使用 JWT,当我插入有效数据(用户名和密码)时,我收到状态为 200 的响应,但我插入了不正确的数据,我收到 401 错误消息,提示凭据错误,这显然不好,因为我在浏览器控制台总是会出现红色漂亮的错误。
我正在使用 Symfony 4 api 平台,这里是完成此类任务所需的文件,
security.yaml :
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
#in_memory: { memory: ~ }
database:
entity:
class: App\Entity\User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
api:
pattern: ^/api
stateless: true
anonymous: true
json_login:
check_path: /api/login_check
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#firewalls-authentication
# https://symfony.com/doc/current/security/impersonating_user.html
# switch_user: true
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_ANONYMOUSLY }
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
和用户实体:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ApiResource(attributes={
* "force_eager"=false,
* "normalization_context"={"groups"={"read"}, "enable_max_depth"=true},
* },)
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface {
/*
Just Variables and getters and setters here
*/
public function getRoles(){
return ['ROLE_USER'];
}
public function getSalt(){
}
public function eraseCredentials(){
}
}
?>
所以我不知道是什么导致了这个问题,如果您能提供帮助,我们将不胜感激。
这种情况下出错是正常的。在 REST 中,您使用状态代码来告诉前端应用程序出了什么问题。带有 'Not Ok"' 坏主意消息的示例 '200 响应,因为状态代码 200 表示 'Everything okay' 但在消息中你告诉了错误发生了什么。
通常错误状态代码 4** 用于此目的,例如 400 - 错误请求或 422 - 您向后端发送无效数据/
所以我正在使用 JWT,当我插入有效数据(用户名和密码)时,我收到状态为 200 的响应,但我插入了不正确的数据,我收到 401 错误消息,提示凭据错误,这显然不好,因为我在浏览器控制台总是会出现红色漂亮的错误。
我正在使用 Symfony 4 api 平台,这里是完成此类任务所需的文件,
security.yaml :
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
#in_memory: { memory: ~ }
database:
entity:
class: App\Entity\User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
api:
pattern: ^/api
stateless: true
anonymous: true
json_login:
check_path: /api/login_check
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#firewalls-authentication
# https://symfony.com/doc/current/security/impersonating_user.html
# switch_user: true
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_ANONYMOUSLY }
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
和用户实体:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ApiResource(attributes={
* "force_eager"=false,
* "normalization_context"={"groups"={"read"}, "enable_max_depth"=true},
* },)
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface {
/*
Just Variables and getters and setters here
*/
public function getRoles(){
return ['ROLE_USER'];
}
public function getSalt(){
}
public function eraseCredentials(){
}
}
?>
所以我不知道是什么导致了这个问题,如果您能提供帮助,我们将不胜感激。
这种情况下出错是正常的。在 REST 中,您使用状态代码来告诉前端应用程序出了什么问题。带有 'Not Ok"' 坏主意消息的示例 '200 响应,因为状态代码 200 表示 'Everything okay' 但在消息中你告诉了错误发生了什么。 通常错误状态代码 4** 用于此目的,例如 400 - 错误请求或 422 - 您向后端发送无效数据/