Skip to content

Commit 11d7bb1

Browse files
eleftheriaskostya05983
authored andcommitted
Allow configuration of servlet api through nested builder
Issue: spring-projectsgh-5557
1 parent 8aa56ab commit 11d7bb1

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/builders/HttpSecurity.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,36 @@ public ServletApiConfigurer<HttpSecurity> servletApi() throws Exception {
12941294
return getOrApply(new ServletApiConfigurer<>());
12951295
}
12961296

1297+
/**
1298+
* Integrates the {@link HttpServletRequest} methods with the values found on the
1299+
* {@link SecurityContext}. This is automatically applied when using
1300+
* {@link WebSecurityConfigurerAdapter}. You can disable it using:
1301+
*
1302+
* <pre>
1303+
* &#064;Configuration
1304+
* &#064;EnableWebSecurity
1305+
* public class ServletApiSecurityConfig extends WebSecurityConfigurerAdapter {
1306+
*
1307+
* &#064;Override
1308+
* protected void configure(HttpSecurity http) throws Exception {
1309+
* http
1310+
* .servletApi(servletApi ->
1311+
* servletApi.disable()
1312+
* );
1313+
* }
1314+
* }
1315+
* </pre>
1316+
*
1317+
* @param servletApiCustomizer the {@link Customizer} to provide more options for
1318+
* the {@link ServletApiConfigurer}
1319+
* @return the {@link HttpSecurity} for further customizations
1320+
* @throws Exception
1321+
*/
1322+
public HttpSecurity servletApi(Customizer<ServletApiConfigurer<HttpSecurity>> servletApiCustomizer) throws Exception {
1323+
servletApiCustomizer.customize(getOrApply(new ServletApiConfigurer<>()));
1324+
return HttpSecurity.this;
1325+
}
1326+
12971327
/**
12981328
* Adds CSRF support. This is activated by default when using
12991329
* {@link WebSecurityConfigurerAdapter}'s default constructor. You can disable it

config/src/test/java/org/springframework/security/config/annotation/web/configurers/ServletApiConfigurerTests.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import static org.mockito.Mockito.atLeastOnce;
4848
import static org.mockito.Mockito.spy;
4949
import static org.mockito.Mockito.verify;
50+
import static org.springframework.security.config.Customizer.withDefaults;
5051
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
5152
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
5253
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
@@ -230,6 +231,53 @@ protected void configure(HttpSecurity http) throws Exception {
230231
}
231232
}
232233

234+
@Test
235+
public void requestWhenServletApiWithDefaultsInLambdaThenUsesDefaultRolePrefix() throws Exception {
236+
this.spring.register(ServletApiWithDefaultsInLambdaConfig.class, AdminController.class).autowire();
237+
238+
this.mvc.perform(get("/admin")
239+
.with(user("user").authorities(AuthorityUtils.createAuthorityList("ROLE_ADMIN"))))
240+
.andExpect(status().isOk());
241+
}
242+
243+
@EnableWebSecurity
244+
static class ServletApiWithDefaultsInLambdaConfig extends WebSecurityConfigurerAdapter {
245+
@Override
246+
protected void configure(HttpSecurity http) throws Exception {
247+
// @formatter:off
248+
http
249+
.servletApi(withDefaults());
250+
// @formatter:on
251+
}
252+
}
253+
254+
@Test
255+
public void requestWhenRolePrefixInLambdaThenUsesCustomRolePrefix() throws Exception {
256+
this.spring.register(RolePrefixInLambdaConfig.class, AdminController.class).autowire();
257+
258+
this.mvc.perform(get("/admin")
259+
.with(user("user").authorities(AuthorityUtils.createAuthorityList("PERMISSION_ADMIN"))))
260+
.andExpect(status().isOk());
261+
262+
this.mvc.perform(get("/admin")
263+
.with(user("user").authorities(AuthorityUtils.createAuthorityList("ROLE_ADMIN"))))
264+
.andExpect(status().isForbidden());
265+
}
266+
267+
@EnableWebSecurity
268+
static class RolePrefixInLambdaConfig extends WebSecurityConfigurerAdapter {
269+
@Override
270+
protected void configure(HttpSecurity http) throws Exception {
271+
// @formatter:off
272+
http
273+
.servletApi(servletApi ->
274+
servletApi
275+
.rolePrefix("PERMISSION_")
276+
);
277+
// @formatter:on
278+
}
279+
}
280+
233281
@RestController
234282
static class AdminController {
235283
@GetMapping("/admin")

0 commit comments

Comments
 (0)