0

HTTP 400 Bad Request从 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 Requiredset as尝试了相同的代码OFF,它可以工作。

4

1 回答 1

1

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

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

org.keycloak.services.resources.account.AccountRestService

一个方法

AccountRestService.updateConsent(String clientId, ConsentRepresentation consent)

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

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

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

于 2020-07-11T12:48:46.213 回答