|
37 | 37 | import org.springframework.security.config.test.SpringTestContextExtension;
|
38 | 38 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
39 | 39 | import org.springframework.security.web.SecurityFilterChain;
|
| 40 | +import org.springframework.security.web.access.expression.WebExpressionAuthorizationManager; |
40 | 41 | import org.springframework.security.web.access.intercept.AuthorizationFilter;
|
41 | 42 | import org.springframework.security.web.access.intercept.RequestAuthorizationContext;
|
42 | 43 | import org.springframework.security.web.access.intercept.RequestMatcherDelegatingAuthorizationManager;
|
43 | 44 | import org.springframework.test.web.servlet.MockMvc;
|
44 | 45 | import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
| 46 | +import org.springframework.test.web.servlet.request.RequestPostProcessor; |
45 | 47 | import org.springframework.web.bind.annotation.GetMapping;
|
46 | 48 | import org.springframework.web.bind.annotation.PostMapping;
|
47 | 49 | import org.springframework.web.bind.annotation.RestController;
|
@@ -395,6 +397,90 @@ public void getWhenAnyRequestAuthenticatedConfiguredAndUserLoggedInThenRespondsW
|
395 | 397 | this.mvc.perform(requestWithUser).andExpect(status().isOk());
|
396 | 398 | }
|
397 | 399 |
|
| 400 | + @Test |
| 401 | + public void getWhenExpressionHasRoleUserConfiguredAndRoleIsUserThenRespondsWithOk() throws Exception { |
| 402 | + this.spring.register(ExpressionRoleUserConfig.class, BasicController.class).autowire(); |
| 403 | + // @formatter:off |
| 404 | + MockHttpServletRequestBuilder requestWithUser = get("/") |
| 405 | + .with(user("user") |
| 406 | + .roles("USER")); |
| 407 | + // @formatter:on |
| 408 | + this.mvc.perform(requestWithUser).andExpect(status().isOk()); |
| 409 | + } |
| 410 | + |
| 411 | + @Test |
| 412 | + public void getWhenExpressionHasRoleUserConfiguredAndRoleIsAdminThenRespondsWithForbidden() throws Exception { |
| 413 | + this.spring.register(ExpressionRoleUserConfig.class, BasicController.class).autowire(); |
| 414 | + // @formatter:off |
| 415 | + MockHttpServletRequestBuilder requestWithAdmin = get("/") |
| 416 | + .with(user("user") |
| 417 | + .roles("ADMIN")); |
| 418 | + // @formatter:on |
| 419 | + this.mvc.perform(requestWithAdmin).andExpect(status().isForbidden()); |
| 420 | + } |
| 421 | + |
| 422 | + @Test |
| 423 | + public void getWhenExpressionRoleUserOrAdminConfiguredAndRoleIsUserThenRespondsWithOk() throws Exception { |
| 424 | + this.spring.register(ExpressionRoleUserOrAdminConfig.class, BasicController.class).autowire(); |
| 425 | + // @formatter:off |
| 426 | + MockHttpServletRequestBuilder requestWithUser = get("/") |
| 427 | + .with(user("user") |
| 428 | + .roles("USER")); |
| 429 | + // @formatter:on |
| 430 | + this.mvc.perform(requestWithUser).andExpect(status().isOk()); |
| 431 | + } |
| 432 | + |
| 433 | + @Test |
| 434 | + public void getWhenExpressionRoleUserOrAdminConfiguredAndRoleIsAdminThenRespondsWithOk() throws Exception { |
| 435 | + this.spring.register(ExpressionRoleUserOrAdminConfig.class, BasicController.class).autowire(); |
| 436 | + // @formatter:off |
| 437 | + MockHttpServletRequestBuilder requestWithAdmin = get("/") |
| 438 | + .with(user("user") |
| 439 | + .roles("ADMIN")); |
| 440 | + // @formatter:on |
| 441 | + this.mvc.perform(requestWithAdmin).andExpect(status().isOk()); |
| 442 | + } |
| 443 | + |
| 444 | + @Test |
| 445 | + public void getWhenExpressionRoleUserOrAdminConfiguredAndRoleIsOtherThenRespondsWithForbidden() throws Exception { |
| 446 | + this.spring.register(ExpressionRoleUserOrAdminConfig.class, BasicController.class).autowire(); |
| 447 | + // @formatter:off |
| 448 | + MockHttpServletRequestBuilder requestWithRoleOther = get("/") |
| 449 | + .with(user("user") |
| 450 | + .roles("OTHER")); |
| 451 | + // @formatter:on |
| 452 | + this.mvc.perform(requestWithRoleOther).andExpect(status().isForbidden()); |
| 453 | + } |
| 454 | + |
| 455 | + @Test |
| 456 | + public void getWhenExpressionHasIpAddressLocalhostConfiguredIpAddressIsLocalhostThenRespondsWithOk() |
| 457 | + throws Exception { |
| 458 | + this.spring.register(ExpressionIpAddressLocalhostConfig.class, BasicController.class).autowire(); |
| 459 | + // @formatter:off |
| 460 | + MockHttpServletRequestBuilder requestFromLocalhost = get("/") |
| 461 | + .with(remoteAddress("127.0.0.1")); |
| 462 | + // @formatter:on |
| 463 | + this.mvc.perform(requestFromLocalhost).andExpect(status().isOk()); |
| 464 | + } |
| 465 | + |
| 466 | + @Test |
| 467 | + public void getWhenExpressionHasIpAddressLocalhostConfiguredIpAddressIsOtherThenRespondsWithForbidden() |
| 468 | + throws Exception { |
| 469 | + this.spring.register(ExpressionIpAddressLocalhostConfig.class, BasicController.class).autowire(); |
| 470 | + // @formatter:off |
| 471 | + MockHttpServletRequestBuilder requestFromOtherHost = get("/") |
| 472 | + .with(remoteAddress("192.168.0.1")); |
| 473 | + // @formatter:on |
| 474 | + this.mvc.perform(requestFromOtherHost).andExpect(status().isForbidden()); |
| 475 | + } |
| 476 | + |
| 477 | + private static RequestPostProcessor remoteAddress(String remoteAddress) { |
| 478 | + return (request) -> { |
| 479 | + request.setRemoteAddr(remoteAddress); |
| 480 | + return request; |
| 481 | + }; |
| 482 | + } |
| 483 | + |
398 | 484 | @EnableWebSecurity
|
399 | 485 | static class NoRequestsConfig {
|
400 | 486 |
|
@@ -713,6 +799,54 @@ SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
713 | 799 |
|
714 | 800 | }
|
715 | 801 |
|
| 802 | + @EnableWebSecurity |
| 803 | + static class ExpressionRoleUserConfig { |
| 804 | + |
| 805 | + @Bean |
| 806 | + SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 807 | + // @formatter:off |
| 808 | + return http |
| 809 | + .authorizeHttpRequests((requests) -> requests |
| 810 | + .anyRequest().access(new WebExpressionAuthorizationManager("hasRole('USER')")) |
| 811 | + ) |
| 812 | + .build(); |
| 813 | + // @formatter:on |
| 814 | + } |
| 815 | + |
| 816 | + } |
| 817 | + |
| 818 | + @EnableWebSecurity |
| 819 | + static class ExpressionRoleUserOrAdminConfig { |
| 820 | + |
| 821 | + @Bean |
| 822 | + SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 823 | + // @formatter:off |
| 824 | + return http |
| 825 | + .authorizeHttpRequests((requests) -> requests |
| 826 | + .anyRequest().access(new WebExpressionAuthorizationManager("hasRole('USER') or hasRole('ADMIN')")) |
| 827 | + ) |
| 828 | + .build(); |
| 829 | + // @formatter:on |
| 830 | + } |
| 831 | + |
| 832 | + } |
| 833 | + |
| 834 | + @EnableWebSecurity |
| 835 | + static class ExpressionIpAddressLocalhostConfig { |
| 836 | + |
| 837 | + @Bean |
| 838 | + SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 839 | + // @formatter:off |
| 840 | + return http |
| 841 | + .authorizeHttpRequests((requests) -> requests |
| 842 | + .anyRequest().access(new WebExpressionAuthorizationManager("hasIpAddress('127.0.0.1')")) |
| 843 | + ) |
| 844 | + .build(); |
| 845 | + // @formatter:on |
| 846 | + } |
| 847 | + |
| 848 | + } |
| 849 | + |
716 | 850 | @Configuration
|
717 | 851 | static class AuthorizationEventPublisherConfig {
|
718 | 852 |
|
|
0 commit comments