99import org .springframework .security .config .annotation .authentication .configuration .AuthenticationConfiguration ;
1010import org .springframework .security .oauth2 .jwt .JwtDecoder ;
1111
12+ import java .util .Map ;
13+
1214public abstract class GrpcSecurityConfigurerAdapter extends GrpcSecurityConfigurer <GrpcSecurity > {
1315
1416 private AuthenticationConfiguration authenticationConfiguration ;
@@ -29,7 +31,7 @@ public void setApplicationContext(ApplicationContext context) throws Exception {
2931 this .authenticationConfiguration = context .getBean (AuthenticationConfiguration .class );
3032
3133 authenticationManagerBuilder = authenticationConfiguration
32- .authenticationManagerBuilder (objectPostProcessor ,context )
34+ .authenticationManagerBuilder (objectPostProcessor , context )
3335 .parentAuthenticationManager (authenticationConfiguration .getAuthenticationManager ());
3436
3537 this .context = context ;
@@ -38,23 +40,29 @@ public void setApplicationContext(ApplicationContext context) throws Exception {
3840 @ Override
3941 public void init (GrpcSecurity builder ) throws Exception {
4042 builder .apply (new GrpcServiceAuthorizationConfigurer (builder .getApplicationContext ()));
41- builder .setSharedObject (AuthenticationManagerBuilder .class ,authenticationManagerBuilder );
43+ builder .setSharedObject (AuthenticationManagerBuilder .class , authenticationManagerBuilder );
4244 final AuthenticationSchemeService authenticationSchemeService = new AuthenticationSchemeService ();
43- authenticationSchemeService .register (new BasicAuthSchemeSelector ());
44- authenticationSchemeService .register (new BearerTokenAuthSchemeSelector ());
45+ registerSchemaSelectors (authenticationSchemeService );
4546 builder .setSharedObject (AuthenticationSchemeService .class , authenticationSchemeService );
4647
4748 }
4849
50+ protected void registerSchemaSelectors (AuthenticationSchemeService authenticationSchemeService ) {
51+ Map <String , AuthenticationSchemeSelector > schemeSelectorMap = context .getBeansOfType (AuthenticationSchemeSelector .class );
52+ for (AuthenticationSchemeSelector selector : schemeSelectorMap .values ()) {
53+ authenticationSchemeService .register (selector );
54+ }
55+ }
56+
4957 @ Override
5058 public void configure (GrpcSecurity builder ) throws Exception {
5159 try {
5260 final Class <?> jwtDecoderClass = Class .forName ("org.springframework.security.oauth2.jwt.JwtDecoder" );
5361 final String [] beanNames = context .getBeanNamesForType (jwtDecoderClass );
54- if (1 == beanNames .length ){
55- builder .authenticationProvider (JwtAuthProviderFactory .forAuthorities (context .getBean (beanNames [0 ],JwtDecoder .class )));
62+ if (1 == beanNames .length ) {
63+ builder .authenticationProvider (JwtAuthProviderFactory .forAuthorities (context .getBean (beanNames [0 ], JwtDecoder .class )));
5664 }
57- }catch (ClassNotFoundException e ){
65+ } catch (ClassNotFoundException e ) {
5866 //swallow
5967 }
6068 builder .authorizeRequests ()
0 commit comments