Skip to content

Commit 23a7c30

Browse files
sandmannnjzheaux
authored andcommitted
Added jwt injection for reactive test mocks
Added new implementation of jwt() method that makes it possible to directly provide a previously prepared JWT token to WebTestClient mutator. Fixes: gh-6896
1 parent c0f64aa commit 23a7c30

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityMockServerConfigurers.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,21 @@ public static JwtMutator mockJwt(Consumer<Jwt.Builder> jwtBuilderConsumer) {
152152
return new JwtMutator(jwtBuilder.build());
153153
}
154154

155+
/**
156+
* Updates the ServerWebExchange to establish a {@link SecurityContext} that has a
157+
* {@link JwtAuthenticationToken} for the
158+
* {@link Authentication} and a {@link Jwt} for the
159+
* {@link Authentication#getPrincipal()}. All details are
160+
* declarative and do not require the JWT to be valid.
161+
*
162+
* @param jwt The preliminary constructed {@link Jwt}
163+
* @return the {@link JwtMutator} to further configure or use
164+
* @since 5.2
165+
*/
166+
public static JwtMutator mockJwt(Jwt jwt) {
167+
return new JwtMutator(jwt);
168+
}
169+
155170
public static CsrfMutator csrf() {
156171
return new CsrfMutator();
157172
}

test/src/test/java/org/springframework/security/test/web/reactive/server/SecurityMockServerConfigurersJwtTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
*/
1616
package org.springframework.security.test.web.reactive.server;
1717

18+
import java.time.Instant;
1819
import java.util.Arrays;
1920
import java.util.List;
21+
import java.util.Map;
22+
import java.util.HashMap;
23+
import java.util.Collections;
2024

2125
import org.junit.Test;
2226
import org.junit.runner.RunWith;
@@ -29,6 +33,8 @@
2933
import org.springframework.security.core.GrantedAuthority;
3034
import org.springframework.security.core.authority.SimpleGrantedAuthority;
3135
import org.springframework.security.core.context.SecurityContext;
36+
import org.springframework.security.oauth2.core.oidc.IdTokenClaimNames;
37+
import org.springframework.security.oauth2.jwt.Jwt;
3238
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
3339
import org.springframework.security.web.reactive.result.method.annotation.CurrentSecurityContextArgumentResolver;
3440
import org.springframework.security.web.server.context.SecurityContextServerWebExchangeWebFilter;
@@ -136,4 +142,25 @@ public void mockJwtWhenProvidingGrantedAuthoritiesThenProducesJwtAuthentication(
136142
assertThat((List<GrantedAuthority>) context.getAuthentication().getAuthorities())
137143
.containsOnly(this.authority1);
138144
}
145+
146+
@Test
147+
public void mockJwtWhenProvidingPreparedJwtThenProducesJwtAuthentication() {
148+
Map<String, Object> claims = new HashMap<>();
149+
claims.put(IdTokenClaimNames.SUB, "some_user");
150+
Jwt originalToken = new Jwt("token123", Instant.now(), Instant.now().plusSeconds(3600),
151+
Collections.singletonMap("header1", "value1"), claims);
152+
client
153+
.mutateWith(mockJwt(originalToken))
154+
.get()
155+
.exchange()
156+
.expectStatus().isOk();
157+
158+
SecurityContext context = securityContextController.removeSecurityContext();
159+
assertThat(context.getAuthentication()).isInstanceOf(
160+
JwtAuthenticationToken.class);
161+
JwtAuthenticationToken retrievedToken = (JwtAuthenticationToken) context.getAuthentication();
162+
assertThat(retrievedToken.getToken().getSubject()).isEqualTo("some_user");
163+
assertThat(retrievedToken.getToken().getTokenValue()).isEqualTo("token123");
164+
assertThat(retrievedToken.getToken().getHeaders().get("header1")).isEqualTo("value1");
165+
}
139166
}

0 commit comments

Comments
 (0)