在 API 平臺中,我使用 JWT 保護了所有端點,但我希望公開 POST 用戶,以便用戶可以自行注冊。
如何在物體級別執行此操作?
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ApiResource(
collectionOperations: [
'get',
"post" => ["security" => "is_granted('PUBLIC_ACCESS')"], //this does not work
],
itemOperations: [
'get'
],
)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
如果我security.yaml像往常一樣在 Symfony 中實作它,它會起作用
access_control:
- { path: ^/docs, roles: PUBLIC_ACCESS } # Allows accessing the Swagger UI
- { path: ^/authentication_token, roles: PUBLIC_ACCESS }
- { path: ^/users, roles: PUBLIC_ACCESS, methods: POST }
- { path: ^/, roles: IS_AUTHENTICATED_FULLY }
我只是想知道我是否可以在物體級別使用注釋來做到這一點。
uj5u.com熱心網友回復:
Api-Platform 安全規則在Symfony 的安全訪問規則之后處理。
因此,如果 Symfony 的訪問規則比 Api-Platform 的規則更嚴格,則這些規則適用并且請求將被拒絕。
如果相反(security.access_rules宣告端點“打開”,但在您的資源配置中您宣告了更嚴格的is_granted()配置),它會起作用,因為那時請求將通過 Symfony 防火墻并到達 Api-Platform 訪問偵聽器。
為了能夠使用屬性/注釋配置安全性,安全性配置需要比 Symfony 防火墻上的配置更嚴格。
例如設定/為PUBLIC_ACCESS,然后在每個資源上配置security相應的is_granted()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/350068.html
標籤:php symfony api-platform.com symfony-安全
