在 java 中使用 Keycloak 以编程方式对用户进行身份验证
Programmatically authenticate user with Keycloak in java
我一直在查看 Keycloak 文档,但看不到如何执行此操作。使用 Java,我想获取一个有效的用户 ID 和密码,然后生成一个令牌。我怎样才能做到这一点?
--编辑 2018-08-31--
您可以使用Authorization Client Java API. Once you have created an AuthzClient object, you can pass the username and password to the AuthzClient#authorization(username, password) or AuthzClient#obtainAccessToken(username, password)方法验证用户并获取访问令牌(and/or第一种情况下的ID令牌):
// create a new instance based on the configuration defined in keycloak-authz.json
AuthzClient authzClient = AuthzClient.create();
// send the authorization request to the server in order to
// obtain an access token granted to the user
AccessTokenResponse response = authzClient.obtainAccessToken("alice", "alice");
附带说明一下,如果可能的话,您宁愿重复使用 Keycloak Java Adapters 之一来涵盖更多功能,例如其他身份验证方法(用户通常被重定向到 Keycloack WUI,您可以在其中强制执行非常灵活的身份验证和授权策略)。
我一直在查看 Keycloak 文档,但看不到如何执行此操作。使用 Java,我想获取一个有效的用户 ID 和密码,然后生成一个令牌。我怎样才能做到这一点?
--编辑 2018-08-31--
您可以使用Authorization Client Java API. Once you have created an AuthzClient object, you can pass the username and password to the AuthzClient#authorization(username, password) or AuthzClient#obtainAccessToken(username, password)方法验证用户并获取访问令牌(and/or第一种情况下的ID令牌):
// create a new instance based on the configuration defined in keycloak-authz.json
AuthzClient authzClient = AuthzClient.create();
// send the authorization request to the server in order to
// obtain an access token granted to the user
AccessTokenResponse response = authzClient.obtainAccessToken("alice", "alice");
附带说明一下,如果可能的话,您宁愿重复使用 Keycloak Java Adapters 之一来涵盖更多功能,例如其他身份验证方法(用户通常被重定向到 Keycloack WUI,您可以在其中强制执行非常灵活的身份验证和授权策略)。