Angular - oidc-client 为每个 REST API 调用发送一个 /authorize 请求
Angular - oidc-client sends an /authorize request for each REST API call
我目前正在设置一个 身份服务器,其中有一个 SPA 客户端 和一些 REST 服务来使用来自的数据。
一切似乎都正常,但我目前很难理解,为什么每次使用有效 access_token
的 API 调用都会触发对身份服务器 /authorize
端点的请求。
登录页面上的按钮
这个按钮只是通过 @angular/common/http
中的 HttpClient
实例调用我的 REST API
这些按钮在我的 /login
页面上。
来自身份服务器的回调设置为转到 /login/callback
。
请求 /authorize
端点
每次单击该按钮都会向 /authorize
端点发送一个请求,结果使用 http 302
将我重定向到 /login/callback
页面。
请求仍然通过并且一切正常,但始终存在这种重定向。
我原以为如果 access_token
有效,则不需要此请求?
AccessToken 拦截器
在 AccessTokenInterceptor
中,我确实调用了我的 OidcService
,它可以访问 oidc-client 库中的 UserManager
。
出于某种原因,每个涉及 UserManager
上的 getUser()
的请求都会触发此 /authorize
请求作为响应 - 即使 access_token
仍然有效。我在这里错过了什么?
@Injectable()
export class AccessTokenInterceptor implements HttpInterceptor {
constructor(private oidcService: OidcService) { }
intercept(request: HttpRequest<any>, next: HttpHandler):
Observable<HttpEvent<any>> {
return this.oidcService.getUser()
.mergeMap((user: User) => {
if (user) {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${user.access_token}`
}
});
}
return next.handle(request);
});
}
}
感谢您在这方面的帮助,如果您需要更多代码示例,请告诉我。
更新 1
一旦我调用 "Call Api" 按钮,就会发出以下三个请求。
OPTIONS
请求我的 REST API.
/authorize
请求(最终返回一个 http 302
并执行我想避免的重定向)
GET
请求这是我打算做的。
更新 2
Web 应用程序 - UserManagerSettings
{
"authority": "https://localhost:44327",
"client_id": "webClient",
"response_type": "id_token token",
"scope": "openid testclientapi testclientapi.read testclientapi.write",
"redirect_uri": "http://localhost:4200/login/callback",
"post_logout_redirect_uri": "http://localhost:4200/logout/callback",
"silent_redirect_uri": "http://localhost:4200/login/silentLogin",
"automaticSilentRenew": true,
"monitorSession": true,
"revokeAccessTokenOnSignout": true,
"loadUserInfo": true
}
身份服务器 - 客户端配置
new Client {
ClientId = "webClient",
ClientName = "myclient",
AllowedGrantTypes = GrantTypes.Implicit,
AccessTokenType = AccessTokenType.Reference,
AccessTokenLifetime = 60 * 60,
IdentityTokenLifetime = 30,
RequireConsent = false,
AllowOfflineAccess = true,
AllowAccessTokensViaBrowser = true,
ClientSecrets =
{
new Secret("XYZ)
},
AllowedCorsOrigins = new string[]
{
"http://localhost:4200",
},
RedirectUris =
{
"http://localhost:4200/login/callback",
"http://localhost:4200/login/silentLogin",
"http://localhost:4200/logout/callback",
},
PostLogoutRedirectUris =
{
"http://localhost:4200/logout/callback",
},
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
"testclientapi",
"testclientapi.read",
"testclientapi.write"
}
}
};
更新 3
getUser() : Observable<User> {
return Observable.fromPromise(this.userManager.getUser())
}
我找到了该问题的临时解决方法。
一旦我没有从我的 /login
路由触发 REST API 请求而是从其他位置触发 REST API 请求,它就像魔术一样工作,例如 /admin
.
最终没有向 /authorize
端点发送任何请求。
一旦我弄清楚为什么会发生这种情况,我将更新此答案。
更新 1
我 运行 现在在执行 /logout
请求时遇到了类似的问题 - 还不得不将其移至另一条完整的路线 ...
我目前正在设置一个 身份服务器,其中有一个 SPA 客户端 和一些 REST 服务来使用来自的数据。
一切似乎都正常,但我目前很难理解,为什么每次使用有效 access_token
的 API 调用都会触发对身份服务器 /authorize
端点的请求。
登录页面上的按钮
这个按钮只是通过 @angular/common/http
HttpClient
实例调用我的 REST API
这些按钮在我的 /login
页面上。
来自身份服务器的回调设置为转到 /login/callback
。
请求 /authorize
端点
每次单击该按钮都会向 /authorize
端点发送一个请求,结果使用 http 302
将我重定向到 /login/callback
页面。
请求仍然通过并且一切正常,但始终存在这种重定向。
我原以为如果 access_token
有效,则不需要此请求?
AccessToken 拦截器
在 AccessTokenInterceptor
中,我确实调用了我的 OidcService
,它可以访问 oidc-client 库中的 UserManager
。
出于某种原因,每个涉及 UserManager
上的 getUser()
的请求都会触发此 /authorize
请求作为响应 - 即使 access_token
仍然有效。我在这里错过了什么?
@Injectable()
export class AccessTokenInterceptor implements HttpInterceptor {
constructor(private oidcService: OidcService) { }
intercept(request: HttpRequest<any>, next: HttpHandler):
Observable<HttpEvent<any>> {
return this.oidcService.getUser()
.mergeMap((user: User) => {
if (user) {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${user.access_token}`
}
});
}
return next.handle(request);
});
}
}
感谢您在这方面的帮助,如果您需要更多代码示例,请告诉我。
更新 1
一旦我调用 "Call Api" 按钮,就会发出以下三个请求。
OPTIONS
请求我的 REST API./authorize
请求(最终返回一个 http302
并执行我想避免的重定向)GET
请求这是我打算做的。
更新 2
Web 应用程序 - UserManagerSettings
{
"authority": "https://localhost:44327",
"client_id": "webClient",
"response_type": "id_token token",
"scope": "openid testclientapi testclientapi.read testclientapi.write",
"redirect_uri": "http://localhost:4200/login/callback",
"post_logout_redirect_uri": "http://localhost:4200/logout/callback",
"silent_redirect_uri": "http://localhost:4200/login/silentLogin",
"automaticSilentRenew": true,
"monitorSession": true,
"revokeAccessTokenOnSignout": true,
"loadUserInfo": true
}
身份服务器 - 客户端配置
new Client {
ClientId = "webClient",
ClientName = "myclient",
AllowedGrantTypes = GrantTypes.Implicit,
AccessTokenType = AccessTokenType.Reference,
AccessTokenLifetime = 60 * 60,
IdentityTokenLifetime = 30,
RequireConsent = false,
AllowOfflineAccess = true,
AllowAccessTokensViaBrowser = true,
ClientSecrets =
{
new Secret("XYZ)
},
AllowedCorsOrigins = new string[]
{
"http://localhost:4200",
},
RedirectUris =
{
"http://localhost:4200/login/callback",
"http://localhost:4200/login/silentLogin",
"http://localhost:4200/logout/callback",
},
PostLogoutRedirectUris =
{
"http://localhost:4200/logout/callback",
},
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
"testclientapi",
"testclientapi.read",
"testclientapi.write"
}
}
};
更新 3
getUser() : Observable<User> {
return Observable.fromPromise(this.userManager.getUser())
}
我找到了该问题的临时解决方法。
一旦我没有从我的 /login
路由触发 REST API 请求而是从其他位置触发 REST API 请求,它就像魔术一样工作,例如 /admin
.
最终没有向 /authorize
端点发送任何请求。
一旦我弄清楚为什么会发生这种情况,我将更新此答案。
更新 1
我 运行 现在在执行 /logout
请求时遇到了类似的问题 - 还不得不将其移至另一条完整的路线 ...