【发布时间】:2026-02-04 18:55:01
【问题描述】:
我目前正在关注我的 oAuth2:
https://manfredsteyer.github.io/angular-oauth2-oidc/docs/index.html
我正在使用身份验证流授权。
我有一个主页,用户可以在其中单击按钮并将其重定向到身份验证服务器。 用户输入他们的凭据后,它将被重定向到一个临时页面,我想在那里使用身份验证代码来获取访问令牌。
我目前停留在关于如何获取代码的临时页面。
所以有几个问题:
- 如何实现获取访问码和获取访问令牌?
- 获取访问令牌后如何重定向到主页?
-
这就是我希望流程的样子:
登录前 -> 验证服务器登录页面 -> 登录后 -> 应用程序主页
-
PreLoginComponent.ts:
export class PreLoginComponent implements OnInit {
constructor(private oauthService: OAuthService) {}
ngOnInit() {}
public login() {
this.oauthService.initCodeFlow();
}
}
authConfig.ts:
export const authConfig: AuthConfig = {
responseType: environment.authRequestType,
loginUrl: environment.authServerUrl,
redirectUri: environment.redirectUrl,
clientId: environment.clientId,
scope: environment.scope,
requireHttps: false,
showDebugInformation: true
};
PreLoginComponent.ts:
export class PreLoginComponent implements OnInit {
constructor(private oauthService: OAuthService) {}
ngOnInit() {}
public login() {
this.oauthService.initCodeFlow();
}
}
AppComponent.ts:
import { OAuthService, JwksValidationHandler } from 'angular-oauth2-oidc';
import { authConfig } from './core/authentication/auth.config';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit, OnDestroy {
constructor(
private oauthService: OAuthService
) {
this.configure();
}
ngOnInit() {
}
private configure() {
this.oauthService.configure(authConfig);
this.oauthService.tokenValidationHandler = new JwksValidationHandler();
}
}
PostLoginComponent.ts:
import { Component, OnInit } from '@angular/core';
import { OAuthService, JwksValidationHandler } from 'angular-oauth2-oidc';
@Component({
selector: 'app-post-login',
templateUrl: './post-login.component.html',
styleUrls: ['./post-login.component.scss']
})
export class PostLoginComponent implements OnInit {
constructor(private oauthService: OAuthService) {
}
ngOnInit() {
// how do i implement to get the code and get access token
// how do i redirect after success
//this.oauthService.tryLoginCodeFlow();
}
}
【问题讨论】:
标签: angular typescript oauth-2.0