简体   繁体   English

处理 SPA 的外部无法访问服务的最佳方法是什么?

[英]What's the best way to handle external unreachable service for SPA?

For a personal project, I'm developing a portal that provide some services only to connected users.对于个人项目,我正在开发一个仅向连接用户提供一些服务的门户。 When you are not connected you are redirected to keycloak that manages the authentication flow (login, password recovery, ....) and redirects to my angular app with a token, then for each call to my webservice i provide the token that is verified for each request between my webservice and keycloak.当您未连接时,您将被重定向到管理身份验证流程(登录、密码恢复等)的 keycloak,并使用令牌重定向到我的 angular 应用程序,然后对于我的网络服务的每次调用,我都会提供经过验证的令牌对于我的网络服务和 keycloak 之间的每个请求。

I designed my app as follow: (really simple view)我设计了我的应用程序如下:(非常简单的视图) 在此处输入图像描述

The problem I have is that my angular app has a big dependency to Keycloak and my webservice.我遇到的问题是我的 angular 应用程序对 Keycloak 和我的网络服务有很大的依赖关系。 I would like my app to be resilient and be prepared if at least keycloak and/or my webservice is offline and ideally if any error or unwanted behavior happens (ex: error 500)如果至少 keycloak 和/或我的网络服务处于离线状态,并且理想情况下发生任何错误或不需要的行为(例如:错误 500),我希望我的应用程序具有弹性并做好准备

I see several things I could do:我看到了我可以做的几件事:

  • I could create an anonymous layout (so anyone could access it directly) in my angular app with one page dedicated to redirection that first would check the availability of keycloak and my webservice (simple http request to see if I get a 200 status code), I could do it in a loop (let's say every 2 seconds) with an information message showing the status then once both are ok, redirecting to keycloak app.我可以在我的 angular 应用程序中创建一个匿名布局(因此任何人都可以直接访问它),其中一个页面专门用于重定向,首先将检查 keycloak 和我的 web 服务的可用性(简单的 http 请求,看看我是否获得 200 状态代码),我可以在一个循环中执行此操作(假设每 2 秒),并显示一条显示状态的信息消息,然后一旦两者都正常,重定向到 keycloak 应用程序。
  • I could create an interceptor that shows an overlay each time I get a response with status code 401 for x seconds then redirect to keycloak login page.我可以创建一个拦截器,每次我收到状态码为 401 的响应 x 秒时都会显示覆盖,然后重定向到 keycloak 登录页面。
  • I could show a generic modal with informative message when I get a response with any other 400 or 500 status code, that could be closed via a button inviting the user to test it again later.当我收到任何其他 400 或 500 状态代码的响应时,我可以显示一个带有信息性消息的通用模式,可以通过一个按钮关闭它,邀请用户稍后再次测试它。

Those are the ideas I have right now but i'm not sure it's the right way.这些是我现在的想法,但我不确定这是正确的方法。

I didn't find any relevant tutorial or article explaining how to do this properly (maybe i'm using bad keywords)我没有找到任何相关的教程或文章解释如何正确执行此操作(也许我使用了错误的关键字)

Do you have an idea?你有想法吗?

I believe the best way to go with is the interceptor where you can handle different errors.我相信 go 的最佳方法是拦截器,您可以在其中处理不同的错误。 For instance:例如:

@Injectable()
export class RequestInterceptor implements HttpInterceptor {
  intercept (request: HttpRequest<any>, next: HttpHandler) : Observable<HttpEvent<any>> {
    return next.handle(request).pipe(
      catchError(error => {
        if (error.status === 0) {
          // Handle unreachable server
        }

        if (error.status === 401) {
          // Handle unauthenticated user
        }

        if (error.status === 500) {
          // Handle server error
        }

        return throwError(error);
      })
    );
  }
}

Checking in loop if server is reachable would cause a lot of unwanted traffic on your backend and I'm not sure if this is the solution you want to go with since you can have scaling problems if application takes off.如果服务器可访问,则循环检查会导致后端出现大量不需要的流量,我不确定这是否是您想要 go 的解决方案,因为如果应用程序起飞,您可能会遇到扩展问题。

You can implement "healthcheck" before redirection, but I don't see a reason for this check in the loop.您可以在重定向之前实施“健康检查”,但我没有在循环中看到此检查的原因。 You can do it once, before actual redirect only.您只能在实际重定向之前执行一次。 But that is still naive test, because there can be error during user request handling and you want be able to cover it from the SPA.但这仍然是一个幼稚的测试,因为在用户请求处理期间可能会出现错误,并且您希望能够从 SPA 中覆盖它。

401 is not a problem with unavailability. 401不是不可用的问题。 That is an info that request is Unauthorized .这是请求Unauthorized的信息。 You will get this info from the webservice, when you use expired token usually.当您通常使用过期令牌时,您将从 Web 服务获取此信息。 It is not right to redirect user to Keycloak login page in this case.在这种情况下,将用户重定向到 Keycloak 登录页面是不对的。 There should be already in the place silent renew, which is watching expiration and it will "refresh" token before expiration.应该已经有静默更新,它正在观察到期,它将在到期前“刷新”令牌。 Standard OIDC angular libraries offer this feature out of the box ( my favorite lib as well: )标准 OIDC angular 库提供开箱即用的此功能( 我最喜欢的库也是:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在角度2(Beta)中将一项服务注入另一项服务的最佳方法是什么? - What's the best way to inject one service into another in angular 2 (Beta)? 格式化SPA消耗的JSON数据的最佳方法是什么? - What would be the best way to format JSON data consumed by a SPA? 在 SPA 中管理身份验证令牌的最佳做法是什么? - What is the best practice to manage authentication tokens in SPA's? 处理承诺错误的最佳方法是什么? - What is the best way to handle errors for promises? 处理竞争条件的最佳方法是什么? - What is the best way to handle a race condition? 在 Angular 4 中处理对象的最佳方法是什么 - What is the best way to handle an object of objects in Angular 4 在Angular 2中调用服务的最佳方法是什么 - What is the best way to call a service in Angular 2 以编程方式将类型传递给服务的最佳方法是什么? - what is the best way to programmatically pass a type to a service? 在 Angular 10 ASP.NET Core 3.1 SPA 应用程序中处理浏览器重新加载的正确方法是什么? - What is the proper way to handle browser reload in Angular 10 ASP.NET Core 3.1 SPA application? Typescript:处理服务调用onInit之间的依赖关系的最佳方法 - Typescript: best way to handle dependencys between service calls onInit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM