Keycloak SSO - 重定向太多

Keycloak SSO - too many redirects

在 Angular4 中使用 Keycloak-js(版本:4.0.0)创建会话时,SSO 不工作

以下是重新创建这个的步骤

  1. 将 Keycloak 与 Angular 4 应用程序集成(例如:https://github.com/mauriciovigolo/keycloak-angular or https://github.com/cternes/slackspace-angular2-spring-keycloak/tree/master/frontend
  2. 当用户尝试登录时,他将被自动重定向到 Keycloak 登录页面
  3. 创建会话后,尝试使用 Keycloak 或 Jenkins 通过另一个应用程序(如 Grafana)登录
  4. 重定向太多(Keycloak 运行 在端口 8081 中,Grafana 在 3000 中)

Request url :

http://localhost:8081/auth/realms/angular_keycloak/protocol/openid-connect/auth?access_type=online&client_id=client-ui&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Flogin%2Fgeneric_oauth&response_type=code&scope=read+write&state=wWXu1iyWXtSevSxwCFzWHPZ7oPM63Dbu5AoMBTMdjHE%3D

Response URL:

http://localhost:3000/login/generic_oauth#state=wWXu1iyWXtSevSxwCFzWHPZ7oPM63Dbu5AoMBTMdjHE%3D&session_state=6ec8255b-ed4c-4399-951c-0241ce7bebad&code=eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0..lvjHquloACY0fTMxmOLeYQ.82mSmzkn4HJBSnokMEpqnZw-xkhUrKy9icZAUwVOrh8b4MP9F-8qmH42rrg0O_axTZVJYlozwWA4x9V2dQAIbi2cUgKJlsiNfJllcN8luK4PSwqOe2bp6WtMszvIeU30UW8RXVqf46hstf1dTxWZp-wocChwLaqATNqZD61u-AURLz6ItY8DQxd3hwScR1kJhfu8bJBR_Pcnbt8LIGl_nKOdaGfceoDFpBfOqGuy1AtQ-3QUwvNkBMZCSGVBYQLB.fSMESQYQKVWZfpbR1Rw47A

尝试过的选项:

  1. Angular4 & 詹金斯
  2. Angular4 & Grafana

而以下步骤有效

  1. 直接登录 Grafana 或 Jenkins 或 KeyCloak
  2. 然后登录 Angular 4 ,就可以了

由于我能够分别登录到这些应用程序中的每一个,并且 SSO 在 Grafana 和 Jenkins 之间工作,我倾向于认为问题可能出在 Keycloak-Js 适配器上。

以下是我用来创建会话的参数

  const keycloakAuth: any = Keycloak({
            url: environment.KEYCLOAK_URL,
            realm: environment.KEYCLOAK_REALM,
            clientId: environment.KEYCLOAK_CLIENTID,
            'ssl-required': 'external',
            'public-client': true,
        });

最终,我使用 Angular-OIDC 库(隐式流)实现了 SSO 功能 https://github.com/manfredsteyer/angular-oauth2-oidc

 private configureWithNewConfigApi() {
 // URL of the SPA to redirect the user to after login
    this.oauthService.redirectUri = window.location.origin + "/index.html";

    // set the scope for the permissions the client should request
    this.oauthService.scope = "openid profile email";

    // set to true, to receive also an id_token via OpenId Connect (OIDC) in addition to the
    // OAuth2-based access_token
    this.oauthService.oidc = true;

    // Use setStorage to use sessionStorage or another implementation of the TS-type Storage
    // instead of localStorage
    this.oauthService.setStorage(sessionStorage);

    this.oauthService.clientId = "<<clientId>>";
    let url = 'https://<<keycloakhost>>:<<port>>/auth/realms/<<realmsname>>/.well-known/openid-configuration';
    this.oauthService.loadDiscoveryDocument(url).then((doc) => {
        // This method just tries to parse the token within the url when
        // the auth-server redirects the user back to the web-app
        // It dosn't initiate the login
        this.oauthService.tryLogin({});      
        console.debug('discovery succeeded', doc);

    });

感谢 Jan Garag 的