Spring Security
MockMvc jwt()
Create mock JWT authentication; does not exercise real signature checking. Boot 4.0.10 / Security 7.0.9 companion project; source-reviewed, not executed.
Formal Syntax & Structure
java syntaxjava
jwt().jwt(j -> j.subject("alice"))Working Example
Example Snippetjava
package example.tenant;import org.junit.jupiter.api.*;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.bean.override.mockito.MockitoBean;import org.springframework.security.core.authority.SimpleGrantedAuthority;import org.springframework.security.oauth2.jwt.BadJwtException;import org.springframework.security.oauth2.jwt.JwtDecoder;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import org.springframework.web.context.WebApplicationContext;import static org.mockito.Mockito.when;import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;@SpringBootTest(properties="lab.browser-password=synthetic-test-only")class ApiSecurityTest { @Autowired WebApplicationContext context; @MockitoBean JwtDecoder decoder; MockMvc mvc; @BeforeEach void setup() { mvc=MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build(); } @Test void anonymousDenied() throws Exception { mvc.perform(get("/api/tenants/north/report")).andExpect(status().isUnauthorized()); } @Test void invalidBearerDenied() throws Exception { when(decoder.decode("invalid-fixture")).thenThrow(new BadJwtException("fixture rejected")); mvc.perform(get("/api/tenants/north/report").header("Authorization","Bearer invalid-fixture")) .andExpect(status().isUnauthorized()); } @Test void missingScopeDenied() throws Exception { mvc.perform(get("/api/tenants/north/report").with(jwt().jwt(j->j.subject("alice")) .authorities(new SimpleGrantedAuthority("SCOPE_other")))).andExpect(status().isForbidden()); } @Test void crossTenantDenied() throws Exception { mvc.perform(get("/api/tenants/south/report").with(jwt().jwt(j->j.subject("alice")) .authorities(new SimpleGrantedAuthority("SCOPE_report.read")))).andExpect(status().isForbidden()); } @Test void permittedTenant() throws Exception { mvc.perform(get("/api/tenants/north/report").with(jwt().jwt(j->j.subject("alice")) .authorities(new SimpleGrantedAuthority("SCOPE_report.read")))) .andExpect(status().isOk()).andExpect(content().string("report for north")); } @Test void corsPreflight() throws Exception { mvc.perform(options("/api/tenants/north/report").header("Origin","https://ui.example.invalid") .header("Access-Control-Request-Method","GET").header("Access-Control-Request-Headers","Authorization")) .andExpect(status().isOk()).andExpect(header().string("Access-Control-Allow-Origin","https://ui.example.invalid")); mvc.perform(options("/api/tenants/north/report").header("Origin","https://other.example.invalid") .header("Access-Control-Request-Method","GET")).andExpect(status().isForbidden()); }}