看到这个警告真烦人。同时,您不想关闭警告。所以我想举一个例子来改变你的代码来摆脱它可能是有用的。以下是我更改HWIOAuthBundle 的OAuthUtils 类的方法。
首先,我把/vendor/hwi/oauth-bundle/HWI/Bundle/OAuthBundle/Resources/config/oauth.html改成了这样:
<service id="hwi_oauth.security.oauth_utils" class="%hwi_oauth.security.oauth_utils.class%">
<argument type="service" id="security.http_utils" />
<argument type="service" id="security.context" />
<argument>%hwi_oauth.connect%</argument>
</service>
到这里:
<service id="hwi_oauth.security.oauth_utils" class="%hwi_oauth.security.oauth_utils.class%">
<argument type="service" id="security.http_utils" />
<argument type="service" id="security.authorization_checker" />
<argument>%hwi_oauth.connect%</argument>
</service>
现在我们必须在 /vendor/hwi/oauth-bundle/HWI/Bundle/OAuthBundle/Security/OAuthUtils 类中更改它:
使用 Symfony\Component\Security\Core\SecurityContextInterface;
...
/**
* @var 安全上下文接口
*/
私人 $securityContext;
/**
* @param HttpUtils $httpUtils
* @param SecurityContextInterface $securityContext
* @param 布尔值 $connect
*/
公共函数 __construct(HttpUtils $httpUtils, SecurityContextInterface $securityContext, $connect)
{
$this->httpUtils = $httpUtils;
$this->securityContext = $securityContext;
$this->connect = $connect;
}
到这里:
使用 Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
...
/**
* @var 授权检查器
*/
私人 $authorizationChecker;
/**
* @param HttpUtils $httpUtils
* @param AuthorizationChecker $authorizationChecker
* @param 布尔值 $connect
*/
公共函数 __construct(HttpUtils $httpUtils, AuthorizationChecker $authorizationChecker, $connect)
{
$this->httpUtils = $httpUtils;
$this->authorizationChecker = $authorizationChecker;
$this->connect = $connect;
}
然后我在使用securityContext 的地方进行了更改。将其替换为authorizationChecker。
公共函数 getAuthorizationUrl(请求 $request,$name,$redirectUrl = null,数组 $extraParameters = 数组())
{
$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));
} 别的 {
$redirectUrl = $this->getServiceAuthUrl($request, $resourceOwner);
}
}
返回 $resourceOwner->getAuthorizationUrl($redirectUrl, $extraParameters);
}
之所以用 AuthorizationChecker 代替 SecurityContext 是因为这种情况下只使用了 isGranted 方法。如果您的情况需要,也许您可以将其替换为 TokenStorage 或同时使用 AuthorizationChecker 和 TokenStorage。