ch4mpy / spring-addons

Ease spring OAuth2 resource-servers configuration and testing
Apache License 2.0
552 stars 89 forks source link

[Kotlin] Could not autowire. No beans of 'Converter<Map<String, Object>, Collection<GrantedAuthority>>' type found. #97

Closed JayAhn2 closed 1 year ago

JayAhn2 commented 1 year ago

Describe the bug Hi.

I've followed the sample code (samples/webmvc_oidcauthentication) in kotlin, but I've got syntax error in authenticationFactory bean registration as below.

Could not autowire. No beans of 'Converter<Map<String, Object>, Collection<GrantedAuthority>>' type found. 

Could you let me know what is missing here?

image

Code sample Ideally, provide me with :

@Configuration
@EnableMethodSecurity
class SecurityConfig {
  @Bean
  fun authenticationFactory(
    authoritiesConverter: Converter<Map<String, Any>, Collection<GrantedAuthority>>
  ): OAuth2AuthenticationFactory {
    return OAuth2AuthenticationFactory { bearerString, claims ->
      OAuthentication(
        OpenidClaimSet(claims),
        authoritiesConverter.convert(claims), bearerString
      )
    }
  }

  @Bean
  fun expressionInterceptUrlRegistryPostProcessor(): ExpressionInterceptUrlRegistryPostProcessor {
    // @formatter:off
    return ExpressionInterceptUrlRegistryPostProcessor { registry ->
      registry
        .requestMatchers("/secured-route").hasRole("AUTHORIZED_PERSONNEL")
        .anyRequest().authenticated()
    }
    // @formatter:on
  }
}

Expected behavior No syntax error

Additional context Add any other context about the problem here.

ch4mpy commented 1 year ago

I will not install IntelliJ to debug Kotlin code. Java with Lombok is good enough and works with all IDEs.

You might use OAuth2AuthoritiesConverter which is an alias for Converter<Map<String, Object>, Collection<? extends GrantedAuthority>>. Because of generics erasure, the context factory gets lost when there is more than one Converter bean and using such alias is viable workaround.

JayAhn2 commented 1 year ago

@ch4mpy Yeah I just found that it works with OAuth2AuthoritiesConverter. Probably it would be clearer if you change the sample codes with OAuth2AuthoritiesConverter :)