当登录页面在另一个系统上时使用 ember-simple-auth

Using ember-simple-auth when the login page is on another system

登录我们应用程序的页面是 jsp 在另一台机器上托管的。如果路由以 http.

开头,我已经设法通过允许调用 window.location.replace 来修改 authenticated-route-mixin 来触发对这台机器的请求。
  beforeModel(transition) {
    if (!this.get('session.isAuthenticated')) {
      Ember.assert('The route configured as Configuration.authenticationRoute cannot implement the AuthenticatedRouteMixin mixin as that leads to an infinite transitioning loop!', this.get('routeName') !== Configuration.authenticationRoute);

      transition.abort();
      this.set('session.attemptedTransition', transition);
      debugger;
      if (Configuration.authenticationRoute.startsWith('http')) {
        window.location.replace(Configuration.authenticationRoute);
      } else {
        this.transitionTo(Configuration.authenticationRoute);
      }
    } else {
      return this._super(...arguments);
    }
  }

这是有效的,但是当我被重定向回我的应用程序时,ember-simple-auth 认为我不再登录并重定向回远程机器,然后将我发送回无限循环中的应用程序。

显然我需要设置一些东西让 ember-simple-auth 知道它实际上已经登录了。为什么它不自动执行此操作?我做错了什么?

我是 oAuth 的新手,所以我可能会在这里遗漏一些基本设置。

这里是 URL.

  ENV['ember-simple-auth'] = {
    authenticationRoute: 'https://our-server.com/opensso/oauth2/authorize?client_id=test-client-1&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A4200%2Fsecure'
  };

与其修改 AuthenticatedRouteMixin,我建议在 Authenticator 中处理您的应用程序特定登录——Ember Simple Auth provides as part of its public API 的关键配置原语.

据我所知,在第一次加载应用程序并检查用户是否通过身份验证时,Ember Simple Auth 将使用 restore 方法,定义为验证器 API.

您可以 return 来自 restore 的承诺解决或拒绝表明用户是否已通过身份验证。您如何检查这是您的身份验证系统的实现细节。

我不知道您是如何在客户端上存储凭据的(如果您能提供更多详细信息会更好),但这里有一个示例流程,使用 cookie 进行身份验证:

  1. Ember 启动,ESA 尝试 restore 会话。
  2. restore 向您的 Java 服务器上的安全 "dummy" 资源发出简单的 AJAX 请求——并检查它是否返回 200 或 401 .
  3. 我们收到 401 回复。用户未通过身份验证,因此在来自 restore 的 Promise return 中拒绝。
  4. 让 ESA 将用户重定向到您的身份验证路由。理想情况下,不要覆盖 AuthenticatedRouteMixin-- 相反,在身份验证路由中使用 beforeModel 挂钩将用户发送到您的 JSP 登录页面。
  5. 用户根据 JSP 表单正确验证。
  6. 在其响应中,您的 Java 服务器设置某种加密的、签名的会话 cookie(这是它通常与 Rails 一起工作的方式)作为凭证。此外,它还会将重定向发送回您的 Ember 应用。
  7. Ember 再次启动,ESA 再次调用 restore
  8. restore 再次 ping 你的 Java 服务器,返回 200(多亏了 cookie),从而解决了它的 Promise。
  9. ESA 得知用户已通过身份验证,并重定向到 'route after authentication'。

请记住,从本质上讲,ESA 只能向客户端指示后端是否考虑它 'authenticated'。 ESA 永远不能用于拒绝对资源的访问-- 只能根据它从后端听到的最后消息在客户端上显示一些不同的东西。

如果有任何帮助,请告诉我。