public class CallCredentialsHelper extends Object
Basic-Auth
.
Note: If you have exactly one CallCredentials
bean in your application context then it will be used
for all AbstractStub
that are annotation with GrpcClient
. If you have none or multiple
CallCredentials
in the application context or use Channel
s, then you have to configure the
credentials yourself (See GrpcClientSecurityAutoConfiguration
).
Currently the following CallCredentials
are supported by this class:
Basic-Auth
Require privacy for the connection
(Wrapper)Include credentials only if connection is private
(Wrapper)Usage:
@Bean
CallCredentials myCallCredentials() {
return CallCredentialsHelper#basicAuth("user", "password")}
}
StubTransformer
to select a CallCredential based on the client name instead.
@Bean
StubTransformer myCallCredentialsTransformer() {
return CallCredentialsHelper#mappedCredentialsStubTransformer(Map.of(
"myService1", basicAuth("user1", "password1"),
"theService2", basicAuth("foo", "bar"),
"publicApi", null // No credentials needed
));
}
stub.withCallCredentials(CallCredentialsHelper#basicAuth("user", "password")).doStuff(request);
Modifier and Type | Method and Description |
---|---|
static CallCredentials2 |
basicAuth(String username,
String password)
Creates a new call credential with the given username and password for basic auth.
|
static CallCredentials2 |
bearerAuth(String token)
Creates a new call credential with the given token for bearer auth.
|
static String |
encodeBasicAuth(String username,
String password)
Encodes the given username and password as basic auth.
|
static StubTransformer |
fixedCredentialsStubTransformer(CallCredentials credentials)
Creates a new
StubTransformer that will assign the given credentials to the given AbstractStub . |
static CallCredentials |
includeWhenPrivate(CallCredentials2 callCredentials)
Wraps the given call credentials in a new layer, that will only include the credentials if the connection
guarantees privacy.
|
static boolean |
isPrivacyGuaranteed(SecurityLevel securityLevel)
Checks whether the given security level provides privacy for all data being send on the connection.
|
static StubTransformer |
mappedCredentialsStubTransformer(Map<String,CallCredentials> credentialsByName)
Creates a new
StubTransformer that will assign credentials to the given AbstractStub based on the
name. |
static StubTransformer |
mappedCredentialsStubTransformer(Map<String,CallCredentials> credentialsByName,
CallCredentials fallback)
Creates a new
StubTransformer that will assign credentials to the given AbstractStub based on the
name. |
static CallCredentials |
requirePrivacy(CallCredentials2 callCredentials)
Wraps the given call credentials in a new layer, which ensures that the credentials are only send, if the
connection guarantees privacy.
|
public static StubTransformer fixedCredentialsStubTransformer(CallCredentials credentials)
StubTransformer
that will assign the given credentials to the given AbstractStub
.credentials
- The call credentials to assign.AbstractStub.withCallCredentials(CallCredentials)
public static StubTransformer mappedCredentialsStubTransformer(Map<String,CallCredentials> credentialsByName)
StubTransformer
that will assign credentials to the given AbstractStub
based on the
name. If the given map does not contain a value for the given name, then the call credentials will be omitted.credentialsByName
- The map that contains the call credentials.mappedCredentialsStubTransformer(Map, CallCredentials)
,
AbstractStub.withCallCredentials(CallCredentials)
public static StubTransformer mappedCredentialsStubTransformer(Map<String,CallCredentials> credentialsByName, @Nullable CallCredentials fallback)
StubTransformer
that will assign credentials to the given AbstractStub
based on the
name. If the given map does not contain a value for the given name, then the optional fallback will be used
otherwise the call credentials will be omitted.credentialsByName
- The map that contains the call credentials.fallback
- The optional fallback to use.AbstractStub.withCallCredentials(CallCredentials)
public static CallCredentials2 bearerAuth(String token)
Note: This method uses experimental grpc-java-API features.
token
- the bearer token to usepublic static CallCredentials2 basicAuth(String username, String password)
Note: This method uses experimental grpc-java-API features.
username
- The username to use.password
- The password to use.public static String encodeBasicAuth(String username, String password)
UTF_8
.username
- The username to use.password
- The password to use.public static boolean isPrivacyGuaranteed(SecurityLevel securityLevel)
Note: This method uses experimental grpc-java-API features.
securityLevel
- The security level to check.public static CallCredentials requirePrivacy(CallCredentials2 callCredentials)
Note: This method uses experimental grpc-java-API features.
callCredentials
- The call credentials to wrap.public static CallCredentials includeWhenPrivate(CallCredentials2 callCredentials)
Note: This method uses experimental grpc-java-API features.
callCredentials
- The call credentials to wrap.