Hi @devmsaleh,
Great question! Your current approach works, but there are indeed better ways to accomplish this without completely disabling the default CamundaClient bean creation.
Better Approaches
Option 1: Use Configuration Properties (Recommended)
Instead of disabling the default bean, you can leverage the Spring Zeebe SDK’s configuration properties to customize the client. You can configure authentication through application.properties or application.yml:
camunda:
client:
mode: self-managed # or saas
auth:
client-id: your-client-id
client-secret: your-client-secret
token-url: your-token-endpoint
However, since you need to pass a dynamic bearer token from your security context, this approach might not be sufficient for your specific use case.
Option 2: Custom Bean with Configuration Properties
You can create a custom CamundaClient bean that leverages the existing configuration while only overriding the credentials provider:
@Configuration
public class CamundaClientConfig {
@Bean
@Primary
public CamundaClient camundaClient(CamundaClientProperties properties) {
return CamundaClient.newClientBuilder()
.gatewayAddress(properties.getZeebe().getGrpcAddress())
.credentialsProvider(new BearerTokenCredentialsProvider())
// Add other properties from the configuration as needed
.build();
}
}
This way, you’re not completely bypassing the Spring Boot configuration but rather extending it.
Option 3: Conditional Bean Override
You can also use @ConditionalOnProperty to conditionally override the default bean:
@Configuration
public class CamundaClientConfig {
@Bean
@ConditionalOnProperty(name = "camunda.client.custom-auth", havingValue = "true")
@Primary
public CamundaClient customCamundaClient() {
return CamundaClient.newClientBuilder()
.credentialsProvider(new BearerTokenCredentialsProvider())
.build();
}
}
Then set camunda.client.custom-auth=true in your properties.
Your Current Implementation
Your current BearerTokenCredentialsProvider implementation looks correct:
public class BearerTokenCredentialsProvider implements CredentialsProvider {
@Override
public void applyCredentials(final CredentialsApplier applier) {
applier.put("Authorization", SecurityUtils.getBearerToken());
}
@Override
public boolean shouldRetryRequest(final StatusCode status) {
return status.isUnauthorized();
}
}
This follows the standard pattern for custom credentials providers as documented in the Client Authorization documentation.
Recommendation
For your specific use case where you need to dynamically pass bearer tokens from your security context, Option 2 would be the most elegant solution. It allows you to:
- Keep the Spring Boot configuration benefits
- Only override the authentication mechanism
- Maintain clean separation of concerns
The key insight is that you don’t need to completely disable the default bean creation - you can create a @Primary bean that takes precedence while still leveraging the configuration infrastructure.
References:
Let me know if you’d like me to elaborate on any of these approaches!