简体   繁体   English

使用Java中的Keycloak以编程方式验证用户身份

[英]Programmatically authenticate user with Keycloak in java

I have been looking through the Keycloak documentation but cannot see how to do this. 我一直在浏览Keycloak文档,但看不到如何做到这一点。 With Java, I'd like to take a valid userid and password and then generate a token. 使用Java,我想获取有效的用户ID和密码,然后生成令牌。 How can I do this? 我怎样才能做到这一点?

--EDIT 2018-08-31-- --EDIT 2018-08-31--

You can use the Authorization Client Java API . 您可以使用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) method to authenticate the user and get the access token (and/or ID token in the first case): 创建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");

On a side note, if possible, you'd rather reuse one of the Keycloak Java Adapters to cover more features, such as other authentication methods (the user is typically redirected to Keycloack WUI where you can enforce very flexible authentication and authorization policies). 另外,如果可能的话,您宁可重复使用其中一个Keycloak Java Adapters来覆盖更多功能,例如其他身份验证方法(用户通常会重定向到Keycloack WUI,您可以在其中强制执行非常灵活的身份验证和授权策略)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM