api - API 平台 Symfony 5 JWT 访问控制
问题描述
我使用 jwt 令牌启动了一个带有 api 平台的 REST API。主要目的是注册和登录用户。我试图授予未经过身份验证的用户对 post 路由的访问权限,
api/users
但以下输出有问题:
{
"code": 401,
"message": "JWT Token not found"
}
但是,在没有任何授权承载的情况下获取路由工作。
这是我的 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"}
* }
推荐阅读
- ruby-on-rails - 带有 has_many_through 的每个用户的 Rails 唯一标签
- python - 从文本中提取数据点
- c - LLVM IR 以可移植方式调用和链接 printf 或任何外部 C 函数
- actionscript-3 - Actionscript 3 相当于 KeyboardEvent.Repeat
- node.js - 让npm包动态安装自己的依赖
- javascript - 试图向弹簧控制器发出发布请求
- reactjs - React 将数据传递给父错误 this.props。不是函数
- java - Spring Boot 2.0.6 中的“未找到类型错误返回值的转换器”
- c# - 如何获取文件的一部分并将这些行写入 C# 中的数组
- php - 为什么我们需要 webhook,API 可以做 webhook 可以做的所有事情?