繁体   English   中英

从 Rest API 获得 400 错误请求,用于 Keycloak,需要同意

[英]Getting 400 Bad Request from Rest API for Keycloak with Consent Required ON

我收到HTTP 400 Bad Request from key cloak java 代码,而Consent Required设置为ON 我正在使用以下代码:

String realm = authenticationData.getRealm();
String clientName = authenticationData.getClientName();
String userName = authenticationData.getUserName();
String secret = authenticationData.getSecret();
String password = authenticationData.getPassword();
String authServerURL = authenticationData.getAuthServerURL();
ResteasyClient resteasyClient = new ResteasyClientBuilder().connectionPoolSize(10).register(new CustomJacksonProvider()).build();
Keycloak keycloak = KeycloakBuilder.builder()
        .serverUrl(authServerURL)
        .realm(realm)
        .grantType(OAuth2Constants.PASSWORD)
        .clientId(clientName)
        .clientSecret(secret)
        .username(userName)
        .password(password)
        .resteasyClient(resteasyClient)
        .build();
String authToken = keycloak.tokenManager().getAccessToken().getToken();

我在 Gradle 文件中使用了以下库。

compile 'org.keycloak:keycloak-adapter-core:7.0.0'
compile 'org.keycloak:keycloak-servlet-filter-adapter:7.0.0'
compile 'org.keycloak:keycloak-authz-client:7.0.0'
compile 'org.keycloak:keycloak-admin-client:7.0.0'
compile 'org.keycloak:keycloak-core:7.0.0'

服务器作为Server Version 7.0.0

注意:我尝试了相同的代码,但Consent Required设置为OFF ,它可以工作。

“需要同意”是指,用户首先必须同意使用这些数据。 只要用户不同意,你就会得到“400”。 IDP 服务器说“我不能给你数据(因为用户不想要这个)。”。 在正常的基于屏幕的登录中,IDP 会向用户显示一个网站,以表示同意。 但是对于REST,这显然是不可能的。

“同意”也不属于登录的 scope。 它属于用户帐户。 用户一旦同意,则同意将保存到他的帐户中。 这就是为什么您不能在登录时设置同意。 但是您可以通过 REST-Api 更改同意。 看package

org.keycloak.services.resources.account.AccountRestService

一个方法

AccountRestService.updateConsent(String clientId, ConsentRepresentation consent)

(有关 API,请参见此处) 这可能是你想要的。

也可以在这里查看文档https://issues.redhat.com/browse/KEYCLOAK-10653

并且不要忘记您需要以 admin(-like) 身份登录才能更改用户数据。

暂无
暂无

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

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