|
47 | 47 | import static org.mockito.Mockito.atLeastOnce;
|
48 | 48 | import static org.mockito.Mockito.spy;
|
49 | 49 | import static org.mockito.Mockito.verify;
|
| 50 | +import static org.springframework.security.config.Customizer.withDefaults; |
50 | 51 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
|
51 | 52 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
|
52 | 53 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
|
@@ -230,6 +231,53 @@ protected void configure(HttpSecurity http) throws Exception {
|
230 | 231 | }
|
231 | 232 | }
|
232 | 233 |
|
| 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 | + |
233 | 281 | @RestController
|
234 | 282 | static class AdminController {
|
235 | 283 | @GetMapping("/admin")
|
|
0 commit comments