我有一个spring-boot应用程序,正在使用keycloak对用户进行身份验证。我计划在我的应用程序中创建一个等待用户名和密码的终结点,它登录到密钥斗篷中,并且如果用户名/密码有效,则要求访问令牌并将其返回给用户。
我该如何登录到keycloak?
如我所见,我需要一个public
客户端来使我的密钥斗篷使用用户名/密码,但这不包含任何内容,resource_id
因此我需要access_token
来自confidential
客户端的。
我是否应该要求access_token
公共客户端提供一个,并且响应代码200
是否成功(因此登录成功),然后向confidential
客户端发送另一个请求以获取access_token
实际有效的信息?
提前致谢。
我计划在我的应用程序中创建一个等待用户名和密码的终结点,它登录到密钥斗篷中,并且如果用户名/密码有效,则要求访问令牌并将其返回给用户。
如果您正确配置了Spring App,并且将对您的用户使用的Keycloak客户端进行身份验证Authorization Code Flow
(即,在Keycloak上启用了标准流),则用户将登录到您的应用程序,重定向到Keycloak并进行身份验证。如果用户成功通过身份验证,则该用户将(可能)被重定向回您的应用,并且该应用将获得访问令牌(除其他外)。然后可以使用该访问令牌代表已认证的用户执行操作。
如我所见,我需要有一个公共客户端才能使我的密钥库使用用户名/密码,但这不包含任何resource_id,因此我需要来自机密客户端的access_token。
从RFC 6749 OAuth 2.0规范中,可以看到:
机密
Clients capable of maintaining the confidentiality of their credentials (e.g., client implemented on a secure server with restricted access to the client credentials), or capable of secure client authentication using other means.
上市
Clients incapable of maintaining the confidentiality of their credentials (e.g., clients executing on the device used by the resource owner, such as an installed native application or a web browser-based application), and incapable of secure client
由于您使用的不是纯Web浏览器应用程序或移动电话,而是使用具有安全后端的Spring Boot应用程序,因此应使用confidential client
。
如果您要使用“资源所有者密码凭据授予”(即,在Keycloak中启用了“直接访问授予”),那么您就混在一起了。您仍然可以与我们一起使用,而无需客户保密。随着public client
从Keycloak令牌请求:
POST -d "client_id=<client_id>"
-d "username=<username>"
-d "password=user_password"
-d "grant_type=password"
<KEYCLOAK_HOST>/auth/realms/<REALM_NAME>/protocol/openid-connect/token>
与 confidential client:
POST -d "client_id=<client_id>"
-d "client_secret=<client_secret>"
-d "username=<username>"
-d "password=user_password"
-d "grant_type=password"
<KEYCLOAK_HOST>/auth/realms/<REALM_NAME>/protocol/openid-connect/token>
你有额外的领域-d "client_secret=<client_secret>"
。
但是请记住:
资源所有者密码凭据授予类型适用于资源所有者与客户端具有信任关系的情况,例如设备操作系统或高特权应用程序。授权服务器在启用此授予类型时应格外小心,仅在其他流程不可行时才允许它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句