【发布时间】:2022-12-22 07:44:07
【问题描述】:
用户登录后,Angular 不会将我重定向到指定页面。
我正在使用 wso2 Identity Server 对用户进行身份验证。我想使用授权码流程。
在我的角度应用程序(我在其中使用依赖项 angular-oauth2-oidc)中,我有一个登录页面,其中仅包含一个重定向到 wso2 授权服务器的按钮。 登录后,我没有重定向到正确的页面。
我有一个服务:
const oAuthConfig : AuthConfig = {
issuer: 'https://localhost:9443/oauth2/token',
strictDiscoveryDocumentValidation: false,
redirectUri: 'http://localhost:4200/dashboard',
postLogoutRedirectUri: 'http://localhost:4200/login',
tokenEndpoint: 'https://localhost:9443/oauth2/token',
userinfoEndpoint: 'https://localhost:9443/oauth2/userinfo',
clientId: '{{clientId}}',
responseType: 'code',
scope: 'openid',
showDebugInformation: true
}
@Injectable({
providedIn: 'root'
})
export class OauthWso2IdentityServerService {
constructor(private readonly oauthService: OAuthService) {}
loginWithWso2IdentityServer() {
this.oauthService.configure(oAuthConfig);
this.oauthService.loadDiscoveryDocument().then(() => {
this.oauthService.tryLoginCodeFlow().then(() => {
if(!this.oauthService.hasValidAccessToken()) {
//initalize the login flow
this.oauthService.initCodeFlow()
} else {
this. oauthService.loadUserProfile().then((userProfile) => {
console.log(JSON.stringify(userProfile));
})
}
})
})
}
}
在我的 LoginComponent 中,我有:
constructor(private wso2: OauthWso2IdentityServerService) {}
login() {
this.wso2.loginWithWso2IdentityServer();
}
登录.html:
<button (click)="login()">login</button>
然后我实施了一个守卫:
export class AuthGuard implements CanActivate {
constructor(private oauthService: OAuthService, private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.oauthService.hasValidIdToken()) {
this.router.navigate(['/dashboard']);
return true;
}
return false;
}
}
在 app-routing.module.ts 中:
{ path: 'dashboard', loadChildren: () => import('./features/dashboard/dashboard.module').then(m => m.DashboardModule), canActivate: [AuthGuard] },
我被重定向到:http://localhost:4200(这是一个空白页面)
【问题讨论】:
-
你检查网络电话了吗? IS 是否有任何错误响应?
标签: angular oauth-2.0 openid-connect openid wso2-identity-server