我有一个客户端需要使用一个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();
}
&kc_idp_hint=google
并按下回车键后,您会被重定向回重定向URL吗?如果所选领域中没有配置给定名称的IDP,则会发生这种情况。 - Vadim Ashikhman