亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

API 平臺禁用重定向登錄

API 平臺禁用重定向登錄

PHP
嚕嚕噠 2022-06-11 09:50:22
我正在使用 Symfony 4 和 API 平臺開發一個網站。我的用戶資源受到標準 Symfony 安全性 ( "security": "is_granted('ROLE_USER')") 的保護:/*** @ORM\Entity(repositoryClass="App\Repository\UserRepository")* @ApiResource(*     collectionOperations={*         "get_user_profile": {*             "method": "GET",*             "path": "/users/profile",*             "controller": GetUserProfile::class,*             "pagination_enabled": false,*             "openapi_context": {"summary": "Retrieves the current User's profile."},*             "security": "is_granted('ROLE_USER')"*         },*     },*     itemOperations={},*     normalizationContext={"groups": {"read"}},* )*/class User implements UserInterface{}問題是,如果我嘗試訪問此 API,則響應將重定向到登錄頁面。如何禁用重定向并將響應保留為 401?重定向對網站用戶非常有用,但 API 用戶不應該收到 HTML 響應。這是我的security.yml:security:    encoders:        App\Entity\User:            algorithm: auto    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers    providers:        # used to reload user from session & other features (e.g. switch_user)        app_user_provider:            entity:                class: App\Entity\User                property: username    firewalls:        dev:            pattern: ^/(_(profiler|wdt)|css|img|js)/            security: false        main:            anonymous: lazy            guard:                authenticators:                    - App\Security\LoginFormAuthenticator            logout:                path: app_logout                # where to redirect after logout                # target: app_any_route            remember_me:                secret:   '%kernel.secret%'                lifetime: 604800 # 1 week in seconds                path:     /            # 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
查看完整描述

1 回答

?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

問題是您只有一個保護身份驗證器,并且該身份驗證器是 LoginFormAuthenticator,它在AbstractFormLoginAuthenticator.


這個抽象類很方便,但它被認為是用于常規 Web 客戶端,而不是用于 API 流。


如果你檢查它的start()方法,你會發現:


    /**

     * Override to control what happens when the user hits a secure page

     * but isn't logged in yet.

     *

     * @return RedirectResponse

     */

    public function start(Request $request, AuthenticationException $authException = null)

    {

        $url = $this->getLoginUrl();

        return new RedirectResponse($url);

    }

因此,只要經過身份驗證的請求命中,它就會得到這個重定向響應。


您的選擇是:


便宜又容易:

覆蓋start(),LoginFormAuthenticator因此它返回如下內容:


return new JsonReponse(

    ['status' => 'KO', 'message' => 'Unauthorized'],

    Response::HTTP_UNAUTHORIZED]

);

這將返回一個帶有 401 的 JSON 和所有未經授權的請求。但是“常規”網絡請求不會被重定向到登錄表單。這并不理想。


稍微好一些:

檢查start()請求是否具有application/*內容類型,并在這些內容上返回 401,但將其余部分重定向到登錄表單。


你實際上應該做的事情:

API URL 應該由不同于常規 Web 路徑的保護身份驗證器保護。為與 API 無關的流量保留登錄表單。Api-Platform 包內置了對 JWT 身份驗證的支持。只需使用它,它很容易實現。


查看完整回答
反對 回復 2022-06-11
  • 1 回答
  • 0 關注
  • 121 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號