我正在使用 angular-oauth2-oidc 来获取 PKCE 流,但是当我从资源服务器调用 api 时,它似乎没有在标头中发送 access_token 。我尝试检查 angular-oauth2-oidc 是否获取 access_token 但我没有看到任何内容
我认为图书馆会将 access_token 发送到 http://localhost:8080 对吗?
@NgModule({
declarations: [
AppComponent,
HomeComponent,
LoginComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
OAuthModule.forRoot({
resourceServer: {
allowedUrls: ['http://localhost:8080'],
sendAccessToken: true
}
})
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
我正在使用 initCodeFlow();
export class LoginComponent {
title = 'for-south-ui';
constructor(private oauthService: OAuthService, private loginService: LoginService, private oAuthStorage: OAuthStorage) {
this.configure();
}
authConfig: AuthConfig = {
issuer: 'http://localhost:9090/realms/for-south-app',
redirectUri: window.location.origin,
clientId: 'for-south-ui',
scope: 'openid profile email',
responseType: 'code',
showDebugInformation: true,
requestAccessToken: true,
dummyClientSecret: 'test'
}
public login() {
this.loginService.login();
}
public logoff() {
this.loginService.logout();
}
public getAccessToken() {
console.log(this.oAuthStorage.getItem('localStorage'));
}
private configure() {
this.oauthService.configure(this.authConfig);
this.oauthService.tokenValidationHandler = new NullValidationHandler();
this.oauthService.loadDiscoveryDocumentAndTryLogin();
this.oauthService.setStorage(this.oAuthStorage);
}
这是我发送到资源服务器的请求
export class TopicService {
private url = "http://localhost:8080/api/v1/topic/test";
constructor(private httpClient: HttpClient) {}
public getTest(): Observable<any> {
return this.httpClient.get<any>(this.url);
}
您提供并配置正确吗?
对于独立组件:
providers: [
provideHttpClient(withInterceptorsFromDi()),
provideOAuthClient({
resourceServer: {
allowedUrls: ['http://localhost:8080'],
sendAccessToken: true
}
})
]
对于模块:
OAuthModule.forRoot({
resourceServer: {
allowedUrls: ['http://localhost:8080'],
sendAccessToken: true
}
})