Symfony 2 SecurityContext class deprecated

前端 未结 3 1499
醉梦人生
醉梦人生 2021-02-02 09:12

I get the following error when I try to reach app/example on symfony demo

Error: The Symfony\\Component\\Security\\Core\\SecurityContext class is deprec

3条回答
  •  清酒与你
    2021-02-02 09:36

    It gets so annoying to see that warning. At the same time you don't want to turn off the warnings. So I thought maybe it's useful to give an example of changing your code to get rid of it. Here's how I changed HWIOAuthBundle's OAuthUtils class to do so. First, I changed /vendor/hwi/oauth-bundle/HWI/Bundle/OAuthBundle/Resources/config/oauth.html from this:

    
        
        
        %hwi_oauth.connect%
    
    

    to this:

    
        
        
        %hwi_oauth.connect%
    
    

    Now we have to change it in the /vendor/hwi/oauth-bundle/HWI/Bundle/OAuthBundle/Security/OAuthUtils class from this:

        use Symfony\Component\Security\Core\SecurityContextInterface;
        ...
    
        /**
         * @var SecurityContextInterface
         */
        private $securityContext;
    
        /**
         * @param HttpUtils                $httpUtils
         * @param SecurityContextInterface $securityContext
         * @param boolean                  $connect
         */
        public function __construct(HttpUtils $httpUtils, SecurityContextInterface $securityContext, $connect)
        {
            $this->httpUtils       = $httpUtils;
            $this->securityContext = $securityContext;
            $this->connect         = $connect;
        }
    

    to this:

        use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
        ...
    
        /**
         * @var AuthorizationChecker
         */
        private $authorizationChecker;
    
        /**
         * @param HttpUtils                $httpUtils
         * @param AuthorizationChecker     $authorizationChecker
         * @param boolean                  $connect
         */
        public function __construct(HttpUtils $httpUtils, AuthorizationChecker $authorizationChecker, $connect)
        {
            $this->httpUtils            = $httpUtils;
            $this->authorizationChecker = $authorizationChecker;
            $this->connect              = $connect;
        }
    

    Then I made changes where the securityContext was used. Replaced it with authorizationChecker.

        public function getAuthorizationUrl(Request $request, $name, $redirectUrl = null, array $extraParameters = array())
        {
            $resourceOwner = $this->getResourceOwner($name);
            if (null === $redirectUrl) {
                if (!$this->connect || !$this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
                    $redirectUrl = $this->httpUtils->generateUri($request, $this->ownerMap->getResourceOwnerCheckPath($name));
                } else {
                    $redirectUrl = $this->getServiceAuthUrl($request, $resourceOwner);
                }
            }
    
            return $resourceOwner->getAuthorizationUrl($redirectUrl, $extraParameters);
        }
    

    The reason of replacing SecurityContext with AuthorizationChecker is because only isGranted method is used in this case. Maybe you could replace it with TokenStorage or use both AuthorizationChecker and TokenStorage if you needed for your case.

提交回复
热议问题