@@ -12,6 +12,9 @@ This page describes how you connect to a grpc server and authenticate yourself.
12
12
- [ Trusting a Server] ( #trusting-a-server )
13
13
- [ Mutual Certificate Authentication] ( #mutual-certificate-authentication )
14
14
- [ Authentication] ( #authentication )
15
+ - [ Creating CallCredentials] ( #creating-callcredentials )
16
+ - [ Using CallCredentials] ( #using-callcredentials )
17
+ - [ Retry with new Authentication] ( #retry-with-new-authentication )
15
18
16
19
## Additional Topics <!-- omit in toc -->
17
20
@@ -97,6 +100,8 @@ grpc.client.__name__.security.privateKey=file:certificates/client.key
97
100
98
101
## Authentication
99
102
103
+ ### Creating CallCredentials
104
+
100
105
In addition to mutual certificate authentication, there are several other ways to authenticate yourself, such as
101
106
`BasicAuth`.
102
107
@@ -106,6 +111,20 @@ various libraries out there that provide implementations for grpc's
106
111
`CallCredentials ` are potentially active components because they can authenticate the request using a (third party)
107
112
service and can manage and renew session tokens themselves.
108
113
114
+ ````java
115
+ @Bean
116
+ CallCredentials basicAuthCredentials() {
117
+ return CallCredentialsHelper . basicAuth(" user" , " password" );
118
+ }
119
+
120
+ @Bean
121
+ CallCredentials bearerAuthForwardingCredentials() {
122
+ return CallCredentialsHelper . bearerAuth(() - > KeycloakSecurityContext . getTokenString());
123
+ }
124
+ ````
125
+
126
+ ### Using CallCredentials
127
+
109
128
If you have exactly one `CallCredentials ` in your application context, we' ll automatically create a `StubTransformer`
110
129
for you and configure all `Stub`s to use it. If you wish to configure different credentials per stub, then you use our
111
130
helper methods in the
@@ -122,6 +141,45 @@ MyServiceBlockingStub myServiceForUser = myService.withCallCredentials(userCrede
122
141
return myServiceForUser.send(request);
123
142
````
124
143
144
+ ### Retry with new Authentication
145
+
146
+ If you want to retry calls that failed due to an expired token (using grpc' s built- in retry mechanism), you can use the
147
+ following example `ClientInterceptor ` as a guide to automatically report the failure to the token store.
148
+ Please note that many popular token- based authentication systems (such as OAuth ) also provide a token TTL that can be
149
+ used to automatically update the token before the call is even sent for the first time, rendering this obsolete.
150
+
151
+ ````java
152
+ @Override
153
+ public < ReqT , RespT > ClientCall<ReqT , RespT > interceptCall(
154
+ MethodDescriptor<ReqT , RespT > method, CallOptions callOptions, Channel next) {
155
+
156
+ callOptions = callOptions
157
+ .withCallCredentials(this . credentials)
158
+ .withStreamTracerFactory(new ClientStreamTracer .Factory () {
159
+
160
+ @Override
161
+ public ClientStreamTracer newClientStreamTracer (
162
+ ClientStreamTracer .StreamInfo info , Metadata headers ) {
163
+
164
+ // Make sure your implementations do _not_ block and return _immediately_
165
+ final Object authToken = headers. get(AUTH_TOKEN_KEY );
166
+ return new ClientStreamTracer () {
167
+
168
+ @Override
169
+ public void streamClosed (final Status status ) {
170
+ this . credentials. invalidate(authToken);
171
+ }
172
+ };
173
+
174
+ }
175
+ });
176
+
177
+ return next. newCall(method, callOptions);
178
+ }
179
+ ````
180
+
181
+ For more details refer to [How to retry with new auth token using builtin retry?](https: // github.com/grpc/grpc-java/issues/7345#issuecomment-679295003)
182
+
125
183
## Additional Topics < ! -- omit in toc -- >
126
184
127
185
- [Getting Started ](getting- started. md)
0 commit comments