API平台Symfony 5 JWT加速控制



我使用jwt令牌启动了一个带有API平台的REST API。主要目的是注册和登录用户。我试图为未通过身份验证的用户授予对后路由的访问权限api/users但是以下输出出现问题:

{
"code": 401,
"message": "JWT Token not found"
}

然而,get路由在没有任何授权承载的情况下工作。

这是我的安全。yaml

# config/packages/security.yaml
security:
encoders:
AppEntityUser:
algorithm: auto
providers:
app_user_provider:
entity:
class: AppEntityUser
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"}
*     }

最新更新