Keycloak身份代理API

6

我有一个客户端需要使用一个API进行交互。该API由keycloak进行保护。 用户通常会登录,但是我想允许用户使用其社交媒体账号(如Facebook或Google)登录而无需进入keycloak的登录页面。 我需要一个rest API来实现如何生成URL,以便当用户单击按钮中的此URL时,它将引导用户前往相应的社交登录页面进行登录,同时keycloak仍然作为代理。

以下是我的实现代码,它可以正确生成URL,但无法将用户带到Google页面进行登录。

这是一个REST控制器。

    @Secured("permitAll")
    @GetMapping(path = "/generator")
    public String brokerGenerator(HttpServletRequest httpServletRequest) throws ServletException {
        String provider = "google";
        String authServerRootUrl = "http://localhost:8080/";
        String realm = "realmName";
        String clientId = "clientName";
        String nonce = UUID.randomUUID().toString();
        MessageDigest md = null;

        try {
            md = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }

        String input = nonce + clientId + provider;
        byte[] check = md.digest(input.getBytes(StandardCharsets.UTF_8));
        String hash = Base64Url.encode(check);
        httpServletRequest.getSession().setAttribute("hash", hash);

        String redirectUri = "http://localhost:4200/dashboard"; 

        return KeycloakUriBuilder.fromUri(authServerRootUrl)
                .path("auth/realms/realmName/google/link")
                .queryParam("nonce", nonce)
                .queryParam("hash", hash)
                .queryParam("client_id", clientId)
                .queryParam("redirect_uri", redirectUri).build(realm, provider).toString();

    }
1个回答

11

Keycloak支持此功能。请参见https://www.keycloak.org/docs/latest/server_admin/index.html#_client_suggested_idp

OIDC应用程序可以通过在授权码流授权端点中设置kc_idp_hint查询参数来绕过Keycloak登录页面,指定要使用的身份提供者。

更新

在您的情况下,您应该使用普通的Keycloak Auth Code Flow端点,并除了基本查询参数外,还提供kc_idp_hint参数。这样,用户首先被重定向到Keycloak登录页面,然后Keycloak将其重定向到选择的身份提供程序登录页面(在您的情况下为Google)。

以下是一个示例重定向URL:

https://keycloak-domain/realms/REALM_NAME/protocol/openid-connect/auth?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE&response_type=code&scope=openid&nonce=NONCE&kc_idp_hint=google

按照这个示例编辑您的代码:

return KeycloakUriBuilder.fromUri(authServerRootUrl)
    .path("realms/realmName/protocol/openid-connect/auth") // Url changed
    .queryParam("response_type", "code") // Autherization Code Flow
    .queryParam("scope", "openid") // Add additional scopes if needed
    .queryParam("kc_idp_hint", "google") // This should match IDP name registered in Keycloak
    .queryParam("nonce", nonce)
    .queryParam("hash", hash)
    .queryParam("client_id", clientId)
    .queryParam("redirect_uri", redirectUri).build(realm, provider).toString();

您可以手动启动Keycloak的重定向以进行测试。开始正常的登录流程,当您被重定向到Keycloak登录页面时,请勿输入凭据,而是将kc_idp_hint=google添加到URL中并按ENTER键。然后您将直接重定向到Google登录页面。


1
在添加了 &kc_idp_hint=google 并按下回车键后,您会被重定向回重定向URL吗?如果所选领域中没有配置给定名称的IDP,则会发生这种情况。 - Vadim Ashikhman
@yormen,请在单独的问题中提出此问题,而不是在评论中。 - Vadim Ashikhman
我有,请查看此链接 https://stackoverflow.com/questions/57877569/unlinking-social-provider-from-user-keycloak - yormen
你看到了吗? - yormen
@Claudio 谢谢。我已经更正了链接。 - Vadim Ashikhman
显示剩余13条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接