Skip to content

Commit c0d2b7f

Browse files
committed
Removes the need for a CSRF token with mock JWT
Changes the JwtRequestPostProcessor to remove the check for a CSRF token in the request. Fixes gh-7170
1 parent 9876b66 commit c0d2b7f

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

samples/boot/oauth2resourceserver/src/test/java/sample/OAuth2ResourceServerControllerTests.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,13 @@
2525
import org.springframework.security.oauth2.jwt.JwtDecoder;
2626
import org.springframework.test.context.junit4.SpringRunner;
2727
import org.springframework.test.web.servlet.MockMvc;
28-
import org.springframework.security.oauth2.jwt.Jwt;
2928

3029
import static org.hamcrest.CoreMatchers.is;
3130
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
3231
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
3332
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
3433
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
3534
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
36-
import static org.mockito.ArgumentMatchers.anyString;
37-
import static org.mockito.Mockito.when;
3835

3936
/**
4037
*
@@ -77,41 +74,32 @@ public void messageCanNotBeReadWithoutScopeMessageReadAuthority() throws Excepti
7774

7875
@Test
7976
public void messageCanNotBeCreatedWithoutAnyScope() throws Exception {
80-
Jwt jwt = Jwt.withTokenValue("token")
81-
.header("alg", "none")
82-
.claim("scope", "")
83-
.build();
84-
when(jwtDecoder.decode(anyString())).thenReturn(jwt);
8577
mockMvc.perform(post("/message")
8678
.content("Hello message")
87-
.header("Authorization", "Bearer " + jwt.getTokenValue()))
79+
.with(jwt(jwt -> jwt.claim("scope", "message:read"))))
8880
.andExpect(status().isForbidden());
8981
}
9082

9183
@Test
9284
public void messageCanNotBeCreatedWithScopeMessageReadAuthority() throws Exception {
93-
Jwt jwt = Jwt.withTokenValue("token")
94-
.header("alg", "none")
95-
.claim("scope", "message:read")
96-
.build();
97-
when(jwtDecoder.decode(anyString())).thenReturn(jwt);
9885
mockMvc.perform(post("/message")
99-
.content("Hello message")
100-
.header("Authorization", "Bearer " + jwt.getTokenValue()))
86+
.with(jwt(jwt -> jwt.claim("scope", "message:read")))
87+
.content("Hello message"))
10188
.andExpect(status().isForbidden());
10289
}
10390

10491
@Test
10592
public void messageCanBeCreatedWithScopeMessageWriteAuthority()
10693
throws Exception {
107-
Jwt jwt = Jwt.withTokenValue("token")
108-
.header("alg", "none")
109-
.claim("scope", "message:write")
110-
.build();
111-
when(jwtDecoder.decode(anyString())).thenReturn(jwt);
11294
mockMvc.perform(post("/message")
113-
.content("Hello message")
114-
.header("Authorization", "Bearer " + jwt.getTokenValue()))
95+
.with(jwt(jwt -> jwt.claim("scope", "message:write")))
96+
.content("Hello message"))
97+
.andExpect(status().isOk())
98+
.andExpect(content().string(is("Message was created. Content: Hello message")));
99+
100+
mockMvc.perform(post("/message")
101+
.with(jwt().authorities(new SimpleGrantedAuthority(("SCOPE_message:write"))))
102+
.content("Hello message"))
115103
.andExpect(status().isOk())
116104
.andExpect(content().string(is("Message was created. Content: Hello message")));
117105
}

test/src/main/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestPostProcessors.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,7 @@ public JwtRequestPostProcessor authorities(Converter<Jwt, Collection<GrantedAuth
10431043

10441044
@Override
10451045
public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
1046+
WebTestUtils.setCsrfRequestMatcher(request, r -> false);
10461047
JwtAuthenticationToken token = new JwtAuthenticationToken(this.jwt, this.authorities);
10471048
return new AuthenticationRequestPostProcessor(token).postProcessRequest(request);
10481049
}

test/src/main/java/org/springframework/security/test/web/support/WebTestUtils.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.springframework.security.web.csrf.CsrfFilter;
3131
import org.springframework.security.web.csrf.CsrfTokenRepository;
3232
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
33+
import org.springframework.security.web.util.matcher.RequestMatcher;
3334
import org.springframework.test.util.ReflectionTestUtils;
3435
import org.springframework.web.context.WebApplicationContext;
3536
import org.springframework.web.context.support.WebApplicationContextUtils;
@@ -114,6 +115,21 @@ public static void setCsrfTokenRepository(HttpServletRequest request,
114115
}
115116
}
116117

118+
/**
119+
* Sets the {@link RequestMatcher} for the specified {@link HttpServletRequest}.
120+
*
121+
* @param request the {@link RequestMatcher} to obtain the
122+
* {@link RequestMatcher}
123+
* @param requestMatcher the {@link RequestMatcher} to set
124+
*/
125+
public static void setCsrfRequestMatcher(HttpServletRequest request,
126+
RequestMatcher requestMatcher) {
127+
CsrfFilter filter = findFilter(request, CsrfFilter.class);
128+
if (filter != null) {
129+
filter.setRequireCsrfProtectionMatcher(requestMatcher);
130+
}
131+
}
132+
117133
@SuppressWarnings("unchecked")
118134
static <T extends Filter> T findFilter(HttpServletRequest request,
119135
Class<T> filterClass) {

0 commit comments

Comments
 (0)