我有以下实现:

前端:localhost:4200(这是Angular应用)

后端:localhost:3000(这是我在Node.js和Express上运行的REST API)

-

步骤1.在Angular应用程序中,用户单击一个链接,该链接会将他重定向到我的REST API(localhost:3000 / auth / steam)。

步骤2.我的REST API将他重定向到他登录的OpenID提供程序(Steam)。

步骤3.成功登录后,OpenID提供程序会将用户与用户数据一起重定向到我的后端服务器。我的后端服务器将此数据保存在数据库中,并创建一个包含用户数据的JWT(JSON网络令牌)。

步骤4.将用户与令牌一起重定向到前端...

这是我真正不知道实现此目标的优雅方式的部分。我曾想过将令牌作为URL参数(?token = ...)传递,但这会有点不安全,因为令牌将在URL中可见。

有更好的方法吗?谢谢 :)

最佳答案

我认为最好的方法是在Http Headers中发送它。
最好的方法是对HTTP请求使用HttpClientModule
此外,该模块还以以下方式提供Interceptor功能:



providers: [
      ...
      { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }
]

export class AuthInterceptor implements HttpInterceptor {
    constructor(private authService: AuthService) {}
    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        const copy = req.clone({headers: req.headers.set('Authorization', this.authService.getToken())});
        return next.handle(copy);
    }
}

关于javascript - 将 token 传递到我的Angular 4应用程序的一种优雅方法是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46710724/

10-11 07:55
查看更多