Spring 启动 KeyCloak 覆盖 ClientIdAndSecretCredentialsProvider 不工作

Spring Boot KeyCloak Override ClientIdAndSecretCredentialsProvider not working

我正在尝试在 KeyCloak Spring 引导中覆盖 ClientIdAndSecretCredentialsProvider。下面是我试过的代码。

public class CustomClientCredentialsProvider extends ClientIdAndSecretCredentialsProvider {

    public static final String PROVIDER_ID = CredentialRepresentation.SECRET;

    private String clientSecret;

    @Override
    public String getId() {
        return PROVIDER_ID;
    }

    @Override
    public void init(KeycloakDeployment deployment, Object config) {
        clientSecret = (String) config;
    }

    @Override
    public void setClientCredentials(KeycloakDeployment deployment, Map<String, String> requestHeaders, Map<String, String> formParams) {
        String clientId = deployment.getResourceName();

        if (!deployment.isPublicClient()) {
            if (clientSecret != null) {
                // do something else
            }
        } else {
            formParams.put(OAuth2Constants.CLIENT_ID, clientId);
        }
    }
}

然而即使在覆盖之后,我仍然可以看到控制转到现有 Spring 引导的 ClientIdAndSecretCredentialsProvider 而不是我的。

如何让我的而不是 Spring 引导控制?还有什么需要设置的吗??

我认为您必须将 CustomClientCredentialsProvider 的实例传递给适配器部署。
参见 AdapterDeploymentContext#setClientAuthenticator(ClientCredentialsProvider clientAuthenticator)