有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java密钥斗篷身份代理API

所以我有一个使用api的客户端。该API由KeyClope保护。 用户正常登录,但我想允许用户登录用户,而不必使用他们的社交媒体帐户(如facebook或谷歌)进入KeyClope的登录页面。 我需要一个RESTAPI,它实现了如何生成一个url,所以当用户在按钮中单击此url时,它会将用户带到相应的社交登录页面进行登录,而KeyClope仍然充当代理

下面是我的实现,它可以生成一个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) 个答案

  1. # 1 楼答案

    KeyClope支持这种开箱即用的功能。见https://www.keycloak.org/docs/6.0/server_admin/#_client_suggested_idp

    OIDC applications can bypass the Keycloak login page by specifying a hint on which identity provider they want to use.

    This is done by setting the kc_idp_hint query parameter in the Authorization Code Flow authorization endpoint.

    更新

    在您的情况下,您应该使用普通的keydape验证代码流端点,并且除了基本查询参数之外,还应该提供kc_idp_hint参数。通过这种方式,用户首先被重定向到keydepot登录页面,然后keydepot将其重定向到所选的身份提供者登录页面(在您的情况下是谷歌)

    以下是重定向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();
    

    您可以手动启动KeyClope重定向以进行测试。启动正常的登录流,当您重定向到keydape登录页面时,不要输入凭据,而是将kc_idp_hint=google添加到URL并按enter键。然后你将被重定向到谷歌登录页面