问题描述
我使用 jwt 令牌在 api 平台上启动了一个 REST API。
主要目的是注册和登录用户。
我试图为未经身份验证的用户授予对 post 路由的访问权限
api/users
但以下输出有问题:
{
"code": 401,"message": "JWT Token not found"
}
但是 get 路由在没有任何授权承载的情况下工作。
这是我的 security.yaml
# config/packages/security.yaml
security:
encoders:
App\Entity\User:
algorithm: auto
providers:
app_user_provider:
entity:
class: App\Entity\User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/api/login
stateless: true
anonymous: true
json_login:
check_path: /api/login
username_path: username
password_path: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
api:
pattern: ^/api/
stateless: true
anonymous: true
provider: app_user_provider
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
main:
anonymous: lazy
provider: app_user_provider
access_control:
- { path: ^/api/docs,roles: IS_AUTHENTICATED_ANONYMOUSLY } # Allows accessing the Swagger UI
- { path: ^/api/login,roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/users,roles: IS_AUTHENTICATED_ANONYMOUSLY }
解决方法
我解决了我的问题。我只是更改我的模型注释:
collectionOperations={
* "get",* "post"={"security"="is_granted('ROLE_USER')"}
* }
到
collectionOperations={
* "get",* "post"={"method"="POST","access_control"="is_granted('IS_AUTHENTICATED_FULLY') === false"}
* }