首页 文章

如何以角度处理会话到期?

提问于
浏览
2

我正在使用角度应用的spring boot微服务 . 我正在使用UAA作为授权服务器 . 如果会话过期意味着应用程序应该弹出消息并重定向到登录页面 . 如何实现角度?

任何人都能提供解决方案吗?

感谢致敬

Shilpa Kulkarni

2 回答

  • 0

    您需要检查 HTTP 个请求,例如,如果会话(令牌)过期并且用户尝试从客户端尝试某些HTTP调用,则服务器必须返回一些相关的状态代码,让's say it' 401 .

    因此,在这种情况下,您将检查服务器是否正在响应状态代码 401 然后显示弹出窗口并重定向到 log in 屏幕 .

  • 0

    您可以使用角度 http-interceptorintercept 所有请求 . 当您的令牌或会话到期时,http响应将为 401(unauthorized) . 基于此,您可以将用户重定向到登录路由 . 请参阅HttpInterceptor的文档 .

    像这样的东西 .

    export class YourInterceptor implements HttpInterceptor {
      constructor() {}
      intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    
        return next.handle(request).do((event: HttpEvent<any>) => {
          if (event instanceof HttpResponse) {
            // do stuff with response if you want
          }
        }, (err: any) => {
          if (err instanceof HttpErrorResponse) {
            if (err.status === 401) {
              // redirect to the login route
              // or show a modal
            }
          }
        });
      }
    }
    

    希望这可以帮助 .

相关问题