簡體   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