如何将身份验证的用户在HTTP请求中使用Symfony 4.4中的外部服务?

发布于 2025-02-10 19:18:22 字数 767 浏览 0 评论 0原文

我正在与PHP/Symfony 4.4后端API合作,该API通过外部身份服务创建的JWT代币对用户进行身份验证。 lexik/jwt-authentication-bundle用于验证令牌。我需要使用用户的JWT令牌将HTTP请求发送到外部服务路由以获取一些必要的数据,因为该路由根据JWT令牌中的sub字段返回数据。我尝试使用tokenStorageInterfacetokenStorage-> getToken() - > $ rawToken)尝试使用RAW JWT令牌,但该属性受到保护。

这是tokenStorage-> getToken() - > $ rawToken

object(lexik \ bundle \ jwtauthenticationbundle \ security \ security \ authention \ authention \ token \ token \ jwtusertoken)[638] [638]保护'[638]保护'[638]保护'[638]保护'[638]保护'[638]保护'[638] rawtoken'=>字符串''...(长度= 1476)受保护的'ProviderKey'=>字符串'api'(长度= 3)私人'用户'(Symfony \ Component \ Component \ Security \ Core \ Core \ authentication \ token \ token \ abracsiveToken)... ...

I'm working with a PHP/Symfony 4.4 Backend api which authenticates users via jwt tokens created by an external identity service. lexik/jwt-authentication-bundle is used to validate tokens. I need to send an http request to an external service route with the user's jwt token to get some necessary data because the route returns the data depending the sub field in the jwt token. I tried getting the raw jwt token using TokenStorageInterface (tokenStorage->getToken()->$rawToken) but the property is protected.

Here is a preview of the object returned by tokenStorage->getToken()->$rawToken:

object(Lexik\Bundle\JWTAuthenticationBundle\Security\Authentication\Token\JWTUserToken)[638] protected 'rawToken' => string ''... (length=1476) protected 'providerKey' => string 'api' (length=3) private 'user' (Symfony\Component\Security\Core\Authentication\Token\AbstractToken)...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百变从容 2025-02-17 19:18:22

则可以从Lexik_jwt_authentication.yaml中的手表中获得编码的令牌字符串

\Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\TokenExtractorInterface

如果已指定了令牌提取器, 。
这是2个令牌提取器的配置。一个从授权头中提取另一个以从cookie中提取的东西:

lexik_jwt_authentication:
  ...
  token_extractors:
    authorization_header:
      enabled: true
      prefix: Bearer
      name: Authorization
    cookie:
      enabled: true
      name: 'TOKEN_COOKIE_NAME'

服务

lexik_jwt_authentication.extractor.chain_extractor

如果是,则可以使用服务容器中的

来获取原始令牌。 但是!此服务是私人的,因此您必须通过services.yaml将其注入对象中,或者,如果使用自动启动,请在您的控制器类中写下方法,并根据需要进行标记:

/**
 * @required
 */
public function setTokenExtractor(TokenExtractorInterface $tokenExtractor)
{
    $this->tokenExtractor = $tokenExtractor;
}

您不会通过容器get()方法来检索它!

最后调用Tokenextractor Internethorface上的提取方法

$rawToken = $this->tokenExtractor->extract(<instance of current request>);

You can get the encoded token string from

\Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\TokenExtractorInterface

Watch in your lexik_jwt_authentication.yaml, if you have token extractors specified.
Here is a configuration for 2 token extractors. One to extract from authorization-header und another to extract from cookie:

lexik_jwt_authentication:
  ...
  token_extractors:
    authorization_header:
      enabled: true
      prefix: Bearer
      name: Authorization
    cookie:
      enabled: true
      name: 'TOKEN_COOKIE_NAME'

If so, you can use the service

lexik_jwt_authentication.extractor.chain_extractor

from your service-container to get the raw token.

BUT! this service is private, so you have either to inject it via services.yaml into your object or, if using autowiring, write a method within your controller class or the like and mark it as required:

/**
 * @required
 */
public function setTokenExtractor(TokenExtractorInterface $tokenExtractor)
{
    $this->tokenExtractor = $tokenExtractor;
}

You won't retrieve it by containers get() method!

Finally call the extract method on TokenExtractorInterface

$rawToken = $this->tokenExtractor->extract(<instance of current request>);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文