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

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

在驗證用戶與 Symfony 4 和 FOSUserBundle 連接后重定向用戶

在驗證用戶與 Symfony 4 和 FOSUserBundle 連接后重定向用戶

PHP
躍然一笑 2022-05-27 10:18:37
你好世界 !我是一名 laravel 開發人員,但有一段時間我一直在從事 symfony 項目。在我的工作中,我剛剛遇到了一個主要問題,即在檢查用戶是否登錄后運行一段代碼。在 laravel 上,我可以使用提供者、中間件或基本控制器來做到這一點。但是在 Symfony 4 上我被阻止了。 每次檢查是否可以運行此方法時,我都會使用我想要的方法:$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');if ($this->getUser()->getMetaValue('level') == "ADMIN") {     $ip = file_get_contents("http://ipecho.net/plain");     $record = $this->get('geoip2.reader')->city($ ip);     $isoCode = $record->country->isoCode;     if ($isoCode! = "USA") {         return $this->render('backOffice/vpn_error.html.twig');     }}因此,每次連接管理員時,我們都會檢查它是否從美國連接,否則會被要求使用 VPN 來獲得 IP 地址。感謝您的關注。
查看完整描述

1 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

我找到了解決方案,但我知道這不是最好的,但目前它讓我滿意。我已經編輯了denyAccessUnlessGranted() 方法,并使用回顯強制顯示錯誤 我的解決方案是這樣的:


    /**

     * Throws an exception unless the attributes are granted against the current authentication token and optionally

     * supplied subject.

     *

     * @throws AccessDeniedException

     *

     * @final

     */

    protected function denyAccessUnlessGranted($attributes, $subject = null, string $message = 'Access Denied.')

    {

        if (!$this->isGranted($attributes, $subject)) {

            $exception = $this->createAccessDeniedException($message);

            $exception->setAttributes($attributes);

            $exception->setSubject($subject);


            throw $exception;

        } 

        if ($this->getUser()->getMetaValue('level') == "ADMIN") {

            $ip = file_get_contents("http://ipecho.net/plain");

            $record = $this->get('geoip2.reader')->city($ ip);

            $isoCode = $record->country->isoCode;

            if ($isoCode!= "USA") {

                echo $this->renderView('backOffice/vpn_error.html.twig');

                die();

            } 

        }

    }


查看完整回答
反對 回復 2022-05-27
  • 1 回答
  • 0 關注
  • 93 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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