Skip to content

Commit bafe281

Browse files
author
Kostiantyn Shchepanovskyi
committed
176: Fix exception GrpcSecurityConfigurerAdapter initialization
`GrpcSecurityConfigurerAdapter` should not fail if oauth2 dependencies are not on classpath. `BasicAuthSchemeSelector` and `BearerTokenAuthSchemeSelector` are now Spring beans, `GrpcSecurityConfigurerAdapter` registers them to `authenticationSchemeService` automatically if they are in context. `BearerTokenAuthSchemeSelector` has condition for instantiation: ```java @bean @ConditionalOnClass(name = { "org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken", "org.springframework.security.oauth2.core.OAuth2AuthenticationException"}) public BearerTokenAuthSchemeSelector bearerTokenAuthSchemeSelector() { return new BearerTokenAuthSchemeSelector(); } ```
1 parent 1e0efe0 commit bafe281

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/security/GrpcSecurityConfiguration.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.lognet.springboot.grpc.GRpcGlobalInterceptor;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
78
import org.springframework.context.annotation.Bean;
89
import org.springframework.context.annotation.Configuration;
910
import org.springframework.security.config.annotation.ObjectPostProcessor;
@@ -38,6 +39,18 @@ public ServerInterceptor springGrpcSecurityInterceptor() throws Exception {
3839

3940
}
4041

42+
@Bean
43+
public BasicAuthSchemeSelector basicAuthSchemeSelector() {
44+
return new BasicAuthSchemeSelector();
45+
}
46+
47+
@Bean
48+
@ConditionalOnClass(name = {
49+
"org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken",
50+
"org.springframework.security.oauth2.core.OAuth2AuthenticationException"})
51+
public BearerTokenAuthSchemeSelector bearerTokenAuthSchemeSelector() {
52+
return new BearerTokenAuthSchemeSelector();
53+
}
4154

4255
@Autowired(required = false)
4356
@SuppressWarnings({ "rawtypes", "unchecked" })

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/security/GrpcSecurityConfigurerAdapter.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
1010
import org.springframework.security.oauth2.jwt.JwtDecoder;
1111

12+
import java.util.Map;
13+
1214
public abstract class GrpcSecurityConfigurerAdapter extends GrpcSecurityConfigurer<GrpcSecurity> {
1315

1416
private AuthenticationConfiguration authenticationConfiguration;
@@ -28,7 +30,7 @@ public void setApplicationContext(ApplicationContext context) throws Exception {
2830
this.authenticationConfiguration = context.getBean(AuthenticationConfiguration.class);
2931

3032
authenticationManagerBuilder = authenticationConfiguration
31-
.authenticationManagerBuilder(objectPostProcessor,context)
33+
.authenticationManagerBuilder(objectPostProcessor, context)
3234
.parentAuthenticationManager(authenticationConfiguration.getAuthenticationManager());
3335

3436
this.context = context;
@@ -37,23 +39,29 @@ public void setApplicationContext(ApplicationContext context) throws Exception {
3739
@Override
3840
public void init(GrpcSecurity builder) throws Exception {
3941
builder.apply(new GrpcServiceAuthorizationConfigurer(builder.getApplicationContext()));
40-
builder.setSharedObject(AuthenticationManagerBuilder.class,authenticationManagerBuilder);
42+
builder.setSharedObject(AuthenticationManagerBuilder.class, authenticationManagerBuilder);
4143
final AuthenticationSchemeService authenticationSchemeService = new AuthenticationSchemeService();
42-
authenticationSchemeService.register(new BasicAuthSchemeSelector());
43-
authenticationSchemeService.register(new BearerTokenAuthSchemeSelector());
44+
registerSchemaSelectors(authenticationSchemeService);
4445
builder.setSharedObject(AuthenticationSchemeService.class, authenticationSchemeService);
4546

4647
}
4748

49+
protected void registerSchemaSelectors(AuthenticationSchemeService authenticationSchemeService) {
50+
Map<String, AuthenticationSchemeSelector> schemeSelectorMap = context.getBeansOfType(AuthenticationSchemeSelector.class);
51+
for (AuthenticationSchemeSelector selector : schemeSelectorMap.values()) {
52+
authenticationSchemeService.register(selector);
53+
}
54+
}
55+
4856
@Override
4957
public void configure(GrpcSecurity builder) throws Exception {
5058
try {
5159
final Class<?> jwtDecoderClass = Class.forName("org.springframework.security.oauth2.jwt.JwtDecoder");
5260
final String[] beanNames = context.getBeanNamesForType(jwtDecoderClass);
53-
if (1==beanNames.length){
54-
builder.authenticationProvider(JwtAuthProviderFactory.forAuthorities(context.getBean(beanNames[0],JwtDecoder.class)));
61+
if (1 == beanNames.length) {
62+
builder.authenticationProvider(JwtAuthProviderFactory.forAuthorities(context.getBean(beanNames[0], JwtDecoder.class)));
5563
}
56-
}catch (ClassNotFoundException e){
64+
} catch (ClassNotFoundException e) {
5765
//swallow
5866
}
5967
builder.authorizeRequests()

0 commit comments

Comments
 (0)