所以我有一個使用 api 的客戶端。API 由 keycloak 保護。用戶正常登錄,但我希望允許用戶登錄用戶,而不必使用他們的社交媒體帳戶(如 facebook 或 google)訪問 keycloak 的登錄頁面。我需要一個 rest API,其中包含如何生成 url 的實現,因此當用戶在按鈕中單擊此 url 時,它將把用戶帶到相應的社交登錄頁面進行登錄,而 keycloak 仍然充當代理。下面是我的實現,它可以生成一個 url,但不會將用戶帶到 google 頁面進行登錄這是一個休息控制器 @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 回答

泛舟湖上清波郎朗
TA貢獻1818條經驗 獲得超3個贊
Keycloak 開箱即用地支持這一點。請參閱https://www.keycloak.org/docs/latest/server_admin/index.html#_client_suggested_idp
OIDC 應用程序可以通過指定他們想要使用哪個身份提供者的提示來繞過 Keycloak 登錄頁面。
這是通過在授權代碼流授權端點中設置 kc_idp_hint 查詢參數來完成的。
更新
在您的情況下,您應該使用普通的 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 登錄頁面。
添加回答
舉報
0/150