如何将身份验证的用户在HTTP请求中使用Symfony 4.4中的外部服务?
我正在与PHP/Symfony 4.4后端API合作,该API通过外部身份服务创建的JWT代币对用户进行身份验证。 lexik/jwt-authentication-bundle用于验证令牌。我需要使用用户的JWT令牌将HTTP请求发送到外部服务路由以获取一些必要的数据,因为该路由根据JWT令牌中的sub
字段返回数据。我尝试使用tokenStorageInterface
(tokenStorage-> 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
则可以从Lexik_jwt_authentication.yaml中的手表中获得编码的令牌字符串
如果已指定了令牌提取器, 。
这是2个令牌提取器的配置。一个从授权头中提取另一个以从cookie中提取的东西:
服务
如果是,则可以使用服务容器中的
来获取原始令牌。 但是!此服务是私人的,因此您必须通过services.yaml将其注入对象中,或者,如果使用自动启动,请在您的控制器类中写下方法,并根据需要进行标记:
您不会通过容器get()方法来检索它!
最后调用Tokenextractor Internethorface上的提取方法
You can get the encoded token string from
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:
If so, you can use the service
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:
You won't retrieve it by containers get() method!
Finally call the extract method on TokenExtractorInterface