作者:妩媚舞乙 | 来源:互联网 | 2022-12-06 11:52
我一直在浏览Keycloak文档,但看不到该怎么做。使用Java,我想输入一个有效的用户名和密码,然后生成一个令牌。我怎样才能做到这一点?
1> Cyril Danger..:
-编辑2018-08-31--
您可以使用Authorization Client Java API。创建AuthzClient对象后,可以将用户名和密码传递给AuthzClient#authorization(用户名,密码)或AuthzClient#obtainAccessToken(用户名,密码)方法,以对用户进行身份验证并获取访问令牌(和/或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适配器来涵盖更多功能,例如其他身份验证方法(通常将用户重定向到Keycloack WUI,您可以在其中执行非常灵活的身份验证和授权策略)。