不幸的是,我无法理解如何使用用户数据(信息、角色 ecc)生成有效且受信任的 x509 证书,以便从 IdP 获取访问令牌。
看看这篇文章来生成证书How to create a self-signed certificate with OpenSSL
IdP 如何检查和验证此客户端证书?
Keycloak 文档是一个很好的起点,如果您需要用户密钥的整个 DN,请检查“将 X.509 客户端证书身份验证添加到浏览器流”和“将 X.509 客户端证书身份验证添加到直接授予流”,您可以使用配置 X509 上的这个正则表达式:
- 设置“提取用户身份的正则表达式”:(.*)
- 例如,添加一个名为“usercertificate”的用户属性并在其中复制 DN。
是否有必要在 Keycloak 上的某个地方安装服务器证书副本?
不是所有的证书,您可以只在名为“usercertificate”的用户用户属性中添加 DN。
将客户端证书传递给 keycloak 的正确形式是什么(例如 CURL)?
网址:https://localhost:9445/auth/realms//protocol/openid-connect/token
打开 Keycloak TLS 会话的代码示例:
public String openSession(
File p12File,
String passphrase,
String clientId)
throws IOException, GeneralSecurityException {
try (FileInputStream fis = new FileInputStream(p12File);) {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(new FileInputStream(p12File), passphrase.toCharArray());
HttpClient httpClient = HttpClients
.custom()
.setSSLContext(new SSLContextBuilder()
.loadTrustMaterial(null, TrustAllStrategy.INSTANCE)
.loadKeyMaterial(keyStore, passphrase.toCharArray())
.build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
HttpPost httpPost = new HttpPost(tokenEndpoint);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
String data = "grant_type=password" + "&client_id="+ clientId;
StringEntity input = new StringEntity(data);
httpPost.setEntity(input);
HttpResponse response = httpClient.execute(httpPost);
return IOUtils.toString(response.getEntity().getContent(), "UTF-8");
}
}
是否也需要传递私钥,为什么?
不