我看到了myapp
能够在服务器上正确处理 OAuth2 JWT 令牌,但在本地主机上给出令牌转换错误。
我的流程如下 -
在服务器上, the myapp
就在我们的后面custom api-gateway
-
获取访问令牌- 通过邮递员,我点击了 api-gateway 令牌端点,该端点又调用了 authserver 令牌端点。我得到 OAuth JWT 令牌作为响应。
所以,总而言之,邮递员
request:--(creds)--> api-gateway --(samecreds) --> auth-server
回复:jwt 令牌
-
接下来,我打
myapp
终点- 再次通过邮递员,我点击了 api-gateway 端点,该端点又点击了相应的myapp
端点。我得到了所需的回应。对于此请求,在访问 api-gateway 之前,我设置了标头Authorization: Bearer JWT from step1
As the myapp
开发人员,我知道 api 网关正在将 JWT 令牌重新发送到myapp
使用相同的标头机制,即Authorization: Bearer JWT from step1
。从日志中,我看到这与我在邮递员中点击时提供的值相同api-gateway
因此,邮递员请求: --(jwt)--> api-gateway --(相同的 jwt)-->myapp
响应是一些数据,这对于本次讨论来说是微不足道的。
解码后的 JWT Payload 如下:
{
"app-userId": "c54a-4140-9fa0-0f39",
"user_name": "[email protected] /cdn-cgi/l/email-protection",
"scope": [
"all"
],
"exp": 1656929583,
"authorities": [
"app1_viewer",
"app1_modifier",
"app2_viewer",
"app2_blog_creator],
"client_id": "api-gw-client"
...
}
请注意 -"client_id": "api-gw-client"
上述有效负载中的字段。因此,身份验证服务器正在将令牌颁发给api-gateway
client.
现在在 mylocal dev env 上,即myapp
在本地主机上运行 - 我正在尝试实现与服务器上类似的流程。
在本地主机上,myapp 不在 api-gateway 后面运行,但它直接命中
- 获取访问令牌 - 通过邮递员,我点击 api-gateway (与上面的服务器流中的服务器实例相同。即 api-gateway/auth-server 不在本地主机上运行,而是在服务器上运行)令牌端点,该端点又调用 authserver 令牌端点。我得到 OAuth JWT 令牌作为响应。
因此,邮递员请求: --(creds)--> api-gateway --(samecreds) --> auth-server
响应:jwt token
是的,此令牌可以在服务器流步骤 2 中使用并且有效。解码后的 JWT 令牌有效负载与我之前发布的相同,即
{
"app-userId": "c54a-4140-9fa0-0f39",
"user_name": "[email protected] /cdn-cgi/l/email-protection",
"scope": [
"all"
],
"exp": 1656929583,
"authorities": [
"app1_viewer",
"app1_modifier",
"app2_viewer",
"app2_blog_creator],
"client_id": "api-gw-client"
...
}
请再次注意 -"client_id": "api-gw-client"
上述有效负载中的字段。因此,身份验证服务器正在将令牌颁发给api-gateway
客户。我还不确定这个领域是微不足道的还是重要的。
- 接下来,我打
myapp
本地主机端点- 再次通过邮递员,但我打了myapp
直接端点(不通过本地主机上运行的 api-gateway)。对于这个请求,在点击之前,我设置了标头Authorization: Bearer JWT from step1
.
但这次我收到错误:
p.a.OAuth2AuthenticationProcessingFilter :身份验证请求
失败:error=“invalid_token”,error_description=“无法转换”
JSON 的访问令牌”
我想知道是什么导致了这个错误。我不认为client_id: api-gw-client
正在造成这种情况。无论如何,我创建了一个签名的 jwt 令牌client_id: myapp
并在请求中使用它。但我仍然遇到同样的错误。
The key
我在本地主机上使用的与身份验证服务器用于签名的匹配(对应)。我仔细检查了。所以它肯定不是key
issue.
我需要基于 localhost 的设置工作,这样我就可以在本地测试我的 api,而无需部署到服务器(在我的情况下,部署到服务器非常耗时)。因此,这个设置对于我按时完成任务非常重要。任何答案/建议都将不胜感激。
我的项目中使用的 Spring OAuth2 库如下 -
org.springframework.security:spring-security-oauth2-jose:5.4.2
org.springframework.cloud:spring-cloud-starter-oauth2:2.1.3.RELEASE
给出错误的类是:OAuth2AuthenticationProcessingFilter.java https://github.com/spring-attic/spring-security-oauth/blob/main/spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/authentication/OAuth2AuthenticationProcessingFilter.java (API Doc https://docs.spring.io/spring-security/oauth/apidocs/org/springframework/security/oauth2/provider/authentication/OAuth2AuthenticationProcessingFilter.html)