ch4mpy / spring-addons

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

`@WithJwt` does not convert claims using `ConfigurableClaimSetAuthoritiesConverter` and path from configuration #171

Closed andreasgerner closed 7 months ago

andreasgerner commented 7 months ago

Describe the bug When using @WithJwt, the roles of my JWT-JSON-File are not picked up according to the configured JSON-Path in my configuration. Further debugging showed me, that ConfigurableClaimSetAuthoritiesConverter::convert doesn't get called.

Code sample Test Class:

@AutoConfigureAddonsWebmvcResourceServerSecurity
public class UserServiceTest { 
    @Test
    @WithJwt("student.json")
    void shouldSaveUser() { ... }
}

student.json:

{
    "resource_access": {
        "client-name": {
            "roles": [
                "some-role"
            ]
        }
    },
}

application.yml (in test resources):

com:
  c4-soft:
    springaddons:
      oidc:
        ops:
          - iss: https://some-issuer.com/auth/realms/master
            authorities:
              - path: $.resource_access.client-name.roles

Expected behavior Authentication set in test has authority 'some-role'. Instead, authorities only include scopes.

ch4mpy commented 7 months ago

@andreasgerner can you share your project or a reproducer? What you are doing seems pretty close to what is done at several places in this repo (and others that I author), so it should work (this can be due to your security config or whatever)...

andreasgerner commented 7 months ago

I setup a sample containing everything that should be needed to reproduce.

ch4mpy commented 7 months ago

@andreasgerner I found a few things to fix in your reproducer:

andreasgerner commented 7 months ago

Migrating from pure unit tests to @SpringBootTest integration tests resolved my issue, thank you!

ch4mpy commented 7 months ago

@andreasgerner you're welcome. thank you for taking time to provide with reproducer. Please note that @SpringBootTest should be used with care as it can slow down tests execution by loading more of the application context than what is actually needed. Use @WebMvcTest when testing a controller and specify the minimum configuration classes to load when using @SpringBootTest.