Skip to content

Commit e04060b

Browse files
authored
Merge branch 'master' into feature/more-call-credential-helpers
2 parents 7f74c60 + d616ed7 commit e04060b

File tree

26 files changed

+87
-39
lines changed

26 files changed

+87
-39
lines changed

docs/en/client/configuration.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This section describes how you can configure your grpc-spring-boot-starter clien
1111
- [Configuration via Beans](#configuration-via-beans)
1212
- [GrpcChannelConfigurer](#grpcchannelconfigurer)
1313
- [ClientInterceptor](#clientinterceptor)
14+
- [StubFactory](#stubfactory)
1415
- [StubTransformer](#stubtransformer)
1516

1617
## Additional Topics <!-- omit in toc -->
@@ -132,6 +133,45 @@ There are three ways to add a `ClientInterceptor` to your channel.
132133
- Explicitly list them in the `@GrpcClient#interceptors` or `@GrpcClient#interceptorNames` field
133134
- Use a `StubTransformer` and call `stub.withInterceptors(ClientInterceptor... interceptors)`
134135

136+
### StubFactory
137+
138+
A `StubFactory` is used to create a `Stub` of a specific type. The registered stub factories will be checked in order
139+
and the first applicable factory will be used to create the stub.
140+
141+
This library has build in support for the `Stub` types defined in grpc-java:
142+
143+
- [`AsyncStubs`](https://grpc.github.io/grpc-java/javadoc/io/grpc/stub/AbstractAsyncStub.html)
144+
- [`BlockingStubs`](https://grpc.github.io/grpc-java/javadoc/io/grpc/stub/AbstractBlockingStub.html)
145+
- [`FutureStubs`](https://grpc.github.io/grpc-java/javadoc/io/grpc/stub/AbstractFutureStub.html)
146+
147+
But you can easily add support for other `Stub` types by adding a custom `StubFactory` to your application context.
148+
149+
````java
150+
@Component
151+
public class MyCustomStubFactory implements StubFactory {
152+
153+
@Override
154+
public MyCustomStub<?> createStub(Class<? extends AbstractStub<?>> stubType, Channel channel) {
155+
try {
156+
Class<?> enclosingClass = stubType.getEnclosingClass();
157+
Method factoryMethod = enclosingClass.getMethod("newMyBetterFutureStub", Channel.class);
158+
return stubType.cast(factoryMethod.invoke(null, channel));
159+
} catch (Exception e) {
160+
throw new BeanInstantiationException(stubType, "Failed to create gRPC stub", e);
161+
}
162+
}
163+
164+
@Override
165+
public boolean isApplicable(Class<? extends AbstractStub<?>> stubType) {
166+
return AbstractMyCustomStub.class.isAssignableFrom(stubType);
167+
}
168+
169+
}
170+
````
171+
172+
> **Note:** Please report missing stub types (and the corresponding library) in our issue tracker so that we can add
173+
> support if possible.
174+
135175
### StubTransformer
136176

137177
The stub transformer allows you to modify `Stub`s right before they are injected to your beans.

docs/en/client/getting-started.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ This section assumes that you have already defined and generated your [Protobuf
126126
automatically attach it to all `Stub`s (**NOT** `Channel`s). The
127127
[`CallCredentialsHelper`](https://javadoc.io/page/net.devh/grpc-client-spring-boot-autoconfigure/latest/net/devh/boot/grpc/client/security/CallCredentialsHelper.html)
128128
utility class helps you to create commonly used `CallCredentials` types and related `StubTransformer`.
129+
- [`StubFactory`](https://javadoc.io/page/net.devh/grpc-client-spring-boot-autoconfigure/latest/net/devh/boot/grpc/client/stubfactory/StubFactory.html):
130+
A factory that can be used to create a specfic `Stub` type from a `Channel`. Multiple `StubFactory`s can be registered to support different stub types.
131+
See also [Configuration -> StubFactory](configuration.md#stubfactory).
129132
- [`StubTransformer`](https://javadoc.io/page/net.devh/grpc-client-spring-boot-autoconfigure/latest/net/devh/boot/grpc/client/inject/StubTransformer.html):
130133
A transformer that will be applied to all client `Stub`s before they are injected.
131134
See also [Configuration -> StubTransformer](configuration.md#stubtransformer).

examples/cloud-grpc-client/src/main/java/net/devh/boot/grpc/examples/cloud/client/GlobalClientInterceptorConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import net.devh.boot.grpc.client.interceptor.GlobalClientInterceptorConfigurer;
2626

2727
@Order(Ordered.LOWEST_PRECEDENCE)
28-
@Configuration
28+
@Configuration(proxyBeanMethods = false)
2929
public class GlobalClientInterceptorConfiguration {
3030

3131
@Bean

examples/cloud-grpc-server/src/main/java/net/devh/boot/grpc/examples/cloud/server/GlobalInterceptorConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import net.devh.boot.grpc.server.interceptor.GlobalServerInterceptorConfigurer;
2424

25-
@Configuration
25+
@Configuration(proxyBeanMethods = false)
2626
public class GlobalInterceptorConfiguration {
2727

2828
@Bean

examples/local-grpc-client/src/main/java/net/devh/boot/grpc/examples/local/client/GlobalClientInterceptorConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import net.devh.boot.grpc.client.interceptor.GlobalClientInterceptorConfigurer;
2626

2727
@Order(Ordered.LOWEST_PRECEDENCE)
28-
@Configuration
28+
@Configuration(proxyBeanMethods = false)
2929
public class GlobalClientInterceptorConfiguration {
3030

3131
@Bean

examples/local-grpc-server/src/main/java/net/devh/boot/grpc/examples/local/server/GlobalInterceptorConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import net.devh.boot.grpc.server.interceptor.GlobalServerInterceptorConfigurer;
2424
import net.devh.boot.grpc.server.interceptor.GlobalServerInterceptorRegistry;
2525

26-
@Configuration
26+
@Configuration(proxyBeanMethods = false)
2727
public class GlobalInterceptorConfiguration {
2828

2929
@Bean

examples/security-grpc-bearerAuth-client/src/main/java/net/devh/boot/grpc/examples/security/client/SecurityConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*
2929
* @author Gregor Eeckels ([email protected])
3030
*/
31-
@Configuration
31+
@Configuration(proxyBeanMethods = false)
3232
public class SecurityConfiguration {
3333

3434
// This token will usually be created by a login endpoint (e.g. from Keycloak).

examples/security-grpc-bearerAuth-server/src/main/java/net/devh/boot/grpc/examples/security/server/SecurityConfiguration.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
*
4141
* @author Gregor Eeckels ([email protected])
4242
*/
43-
@Configuration
43+
@Configuration(proxyBeanMethods = false)
4444
// proxyTargetClass is required, if you use annotation driven security!
4545
// However, you will receive a warning that GrpcServerService#bindService() method is final.
4646
// You cannot avoid that warning (without massive amount of work), but it is safe to ignore it.
@@ -51,9 +51,11 @@
5151
public class SecurityConfiguration {
5252

5353
@Bean
54-
JwtAuthenticationConverter jwtAuthenticationConverter() {
54+
JwtAuthenticationConverter jwtAuthenticationConverter(
55+
final KeyCloakGrantedAuthoritiesConverter keyCloakGrantedAuthoritiesConverter) {
56+
5557
final JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
56-
converter.setJwtGrantedAuthoritiesConverter(keyCloakGrantedAuthoritiesConverter());
58+
converter.setJwtGrantedAuthoritiesConverter(keyCloakGrantedAuthoritiesConverter);
5759
return converter;
5860
}
5961

@@ -63,26 +65,26 @@ KeyCloakGrantedAuthoritiesConverter keyCloakGrantedAuthoritiesConverter() {
6365
}
6466

6567
@Bean
66-
JwtAuthenticationProvider jwtAuthenticationProvider() {
68+
JwtAuthenticationProvider jwtAuthenticationProvider(final JwtAuthenticationConverter jwtAuthenticationConverter) {
6769
final JwtAuthenticationProvider provider = new JwtAuthenticationProvider(jwtDecoder());
68-
provider.setJwtAuthenticationConverter(jwtAuthenticationConverter());
70+
provider.setJwtAuthenticationConverter(jwtAuthenticationConverter);
6971
return provider;
7072
}
7173

7274
@Bean
7375
/*
7476
* Add the authentication providers to the manager.
7577
*/
76-
AuthenticationManager authenticationManager() {
78+
AuthenticationManager authenticationManager(final JwtAuthenticationProvider jwtAuthenticationProvider) {
7779
final List<AuthenticationProvider> providers = new ArrayList<>();
78-
providers.add(jwtAuthenticationProvider());
80+
providers.add(jwtAuthenticationProvider);
7981
return new ProviderManager(providers);
8082
}
8183

8284
@Bean
8385
// Configure which authentication types you support.
8486
GrpcAuthenticationReader authenticationReader() {
85-
return new BearerAuthenticationReader(accessToken -> new BearerTokenAuthenticationToken(accessToken));
87+
return new BearerAuthenticationReader(BearerTokenAuthenticationToken::new);
8688
}
8789

8890
@Bean

examples/security-grpc-client/src/main/java/net/devh/boot/grpc/examples/security/client/SecurityConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* @author Daniel Theuke ([email protected])
3333
* @see CallCredentialsHelper
3434
*/
35-
@Configuration
35+
@Configuration(proxyBeanMethods = false)
3636
public class SecurityConfiguration {
3737

3838
@Value("${auth.username}")

examples/security-grpc-server/src/main/java/net/devh/boot/grpc/examples/security/server/SecurityConfiguration.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
*
4747
* @author Daniel Theuke ([email protected])
4848
*/
49-
@Configuration
49+
@Configuration(proxyBeanMethods = false)
5050
// proxyTargetClass is required, if you use annotation driven security!
5151
// However, you will receive a warning that GrpcServerService#bindService() method is final.
5252
// You cannot avoid that warning (without massive amount of work), but it is safe to ignore it.
@@ -65,17 +65,17 @@ PasswordEncoder passwordEncoder() {
6565

6666
@Bean
6767
// This could be your database lookup. There are some complete implementations in spring-security-web.
68-
UserDetailsService userDetailsService() {
68+
UserDetailsService userDetailsService(final PasswordEncoder passwordEncoder) {
6969
return username -> {
7070
log.debug("Searching user: {}", username);
7171
switch (username) {
7272
case "guest": {
73-
return new User(username, passwordEncoder().encode(username + "Password"), Collections.emptyList());
73+
return new User(username, passwordEncoder.encode(username + "Password"), Collections.emptyList());
7474
}
7575
case "user": {
7676
final List<SimpleGrantedAuthority> authorities =
7777
Arrays.asList(new SimpleGrantedAuthority("ROLE_GREET"));
78-
return new User(username, passwordEncoder().encode(username + "Password"), authorities);
78+
return new User(username, passwordEncoder.encode(username + "Password"), authorities);
7979
}
8080
default: {
8181
throw new UsernameNotFoundException("Could not find user!");
@@ -87,18 +87,21 @@ UserDetailsService userDetailsService() {
8787
@Bean
8888
// One of your authentication providers.
8989
// They ensure that the credentials are valid and populate the user's authorities.
90-
DaoAuthenticationProvider daoAuthenticationProvider() {
90+
DaoAuthenticationProvider daoAuthenticationProvider(
91+
final UserDetailsService userDetailsService,
92+
final PasswordEncoder passwordEncoder) {
93+
9194
final DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
92-
provider.setUserDetailsService(userDetailsService());
93-
provider.setPasswordEncoder(passwordEncoder());
95+
provider.setUserDetailsService(userDetailsService);
96+
provider.setPasswordEncoder(passwordEncoder);
9497
return provider;
9598
}
9699

97100
@Bean
98101
// Add the authentication providers to the manager.
99-
AuthenticationManager authenticationManager() {
102+
AuthenticationManager authenticationManager(final DaoAuthenticationProvider daoAuthenticationProvider) {
100103
final List<AuthenticationProvider> providers = new ArrayList<>();
101-
providers.add(daoAuthenticationProvider());
104+
providers.add(daoAuthenticationProvider);
102105
return new ProviderManager(providers);
103106
}
104107

0 commit comments

Comments
 (0)