diff --git a/src/main/resources/generator/server/springboot/mvc/security/oauth2/account/main/infrastructure/secondary/OAuth2AuthenticationReader.java.mustache b/src/main/resources/generator/server/springboot/mvc/security/oauth2/account/main/infrastructure/secondary/OAuth2AuthenticationReader.java.mustache index 6fe58c86170..1a0ada180f1 100644 --- a/src/main/resources/generator/server/springboot/mvc/security/oauth2/account/main/infrastructure/secondary/OAuth2AuthenticationReader.java.mustache +++ b/src/main/resources/generator/server/springboot/mvc/security/oauth2/account/main/infrastructure/secondary/OAuth2AuthenticationReader.java.mustache @@ -1,17 +1,16 @@ package {{packageName}}.account.infrastructure.secondary; +import {{packageName}}.account.domain.Account; import java.util.List; import java.util.Map; import java.util.Optional; - import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; +import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; import org.springframework.stereotype.Service; -import {{packageName}}.account.domain.Account; - @Service class OAuth2AuthenticationReader { @@ -54,7 +53,10 @@ class OAuth2AuthenticationReader { if (authentication instanceof OAuth2AuthenticationToken oauthToken) { return oauthToken.getPrincipal().getAttributes(); } + if (authentication instanceof JwtAuthenticationToken jwtToken) { + return jwtToken.getTokenAttributes(); + } throw new UnknownAuthenticationSchemeException(); } -} \ No newline at end of file +} diff --git a/src/main/resources/generator/server/springboot/mvc/security/oauth2/account/test/infrastructure/OAuth2TokenFixture.java.mustache b/src/main/resources/generator/server/springboot/mvc/security/oauth2/account/test/infrastructure/OAuth2TokenFixture.java.mustache index 93138e396e4..88329534341 100644 --- a/src/main/resources/generator/server/springboot/mvc/security/oauth2/account/test/infrastructure/OAuth2TokenFixture.java.mustache +++ b/src/main/resources/generator/server/springboot/mvc/security/oauth2/account/test/infrastructure/OAuth2TokenFixture.java.mustache @@ -1,6 +1,5 @@ package {{packageName}}.account.infrastructure; -import {{packageName}}.authentication.domain.Role; import java.time.Instant; import java.util.HashMap; import java.util.List; @@ -10,6 +9,10 @@ import org.springframework.security.oauth2.client.authentication.OAuth2Authentic import org.springframework.security.oauth2.core.oidc.OidcIdToken; import org.springframework.security.oauth2.core.oidc.OidcUserInfo; import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser; +import org.springframework.security.oauth2.jose.jws.JwsAlgorithms; +import org.springframework.security.oauth2.jwt.Jwt; +import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; +import {{packageName}}.authentication.domain.Role; public final class OAuth2TokenFixture { @@ -42,9 +45,19 @@ public final class OAuth2TokenFixture { Instant now = Instant.now(); OidcIdToken token = new OidcIdToken(TOKEN_ID, now, now.plusSeconds(300), claims); - List authorities = List.of(new SimpleGrantedAuthority(Role.ADMIN.key())); + List authorities = adminAuthorities(); DefaultOidcUser user = new DefaultOidcUser(authorities, token, new OidcUserInfo(claims), "preferred_username"); return new OAuth2AuthenticationToken(user, authorities, "oidc"); } + + public static JwtAuthenticationToken testJwtAuthenticationToken() { + Jwt.Builder jwt = Jwt.withTokenValue("token-just-for-drinking-beers").header("alg", JwsAlgorithms.RS256).subject("jhipster"); + testAuthenticationClaims().forEach(jwt::claim); + return new JwtAuthenticationToken(jwt.build(), adminAuthorities()); + } + + private static List adminAuthorities() { + return List.of(new SimpleGrantedAuthority(Role.ADMIN.key())); + } } diff --git a/src/main/resources/generator/server/springboot/mvc/security/oauth2/account/test/infrastructure/secondary/OAuth2AuthenticationReaderTest.java.mustache b/src/main/resources/generator/server/springboot/mvc/security/oauth2/account/test/infrastructure/secondary/OAuth2AuthenticationReaderTest.java.mustache index ad627214a43..7c9863e96b5 100644 --- a/src/main/resources/generator/server/springboot/mvc/security/oauth2/account/test/infrastructure/secondary/OAuth2AuthenticationReaderTest.java.mustache +++ b/src/main/resources/generator/server/springboot/mvc/security/oauth2/account/test/infrastructure/secondary/OAuth2AuthenticationReaderTest.java.mustache @@ -32,7 +32,7 @@ class OAuth2AuthenticationReaderTest { context.setAuthentication(authentication); - assertThatThrownBy(() -> tokens.authenticatedUserAccount()).isExactlyInstanceOf(UnknownAuthenticationSchemeException.class); + assertThatThrownBy(tokens::authenticatedUserAccount).isExactlyInstanceOf(UnknownAuthenticationSchemeException.class); } @Test @@ -52,4 +52,11 @@ class OAuth2AuthenticationReaderTest { assertThat(tokens.authenticatedUserAccount().get()).usingRecursiveComparison().isEqualTo(account()); } + + @Test + void shouldReadJwtAuthentication() { + context.setAuthentication(testJwtAuthenticationToken()); + + assertThat(tokens.authenticatedUserAccount().get()).usingRecursiveComparison().isEqualTo(account()); + } }