|
32 | 32 | import org.springframework.beans.factory.ObjectProvider; |
33 | 33 | import org.springframework.beans.factory.annotation.Autowired; |
34 | 34 | import org.springframework.beans.factory.config.BeanPostProcessor; |
| 35 | +import org.springframework.context.ApplicationEventPublisher; |
35 | 36 | import org.springframework.context.annotation.Bean; |
36 | 37 | import org.springframework.context.annotation.Configuration; |
| 38 | +import org.springframework.context.event.EventListener; |
37 | 39 | import org.springframework.security.access.hierarchicalroles.RoleHierarchy; |
38 | 40 | import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl; |
39 | 41 | import org.springframework.security.authentication.RememberMeAuthenticationToken; |
|
43 | 45 | import org.springframework.security.authorization.AuthorizationManager; |
44 | 46 | import org.springframework.security.authorization.AuthorizationObservationContext; |
45 | 47 | import org.springframework.security.authorization.AuthorizationResult; |
| 48 | +import org.springframework.security.authorization.SpringAuthorizationEventPublisher; |
| 49 | +import org.springframework.security.authorization.event.AuthorizationDeniedEvent; |
46 | 50 | import org.springframework.security.config.ObjectPostProcessor; |
47 | 51 | import org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry; |
48 | 52 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
|
66 | 70 | import org.springframework.security.web.access.intercept.RequestMatcherDelegatingAuthorizationManager; |
67 | 71 | import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher; |
68 | 72 | import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher; |
| 73 | +import org.springframework.stereotype.Component; |
69 | 74 | import org.springframework.test.web.servlet.MockMvc; |
70 | 75 | import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; |
71 | 76 | import org.springframework.test.web.servlet.request.RequestPostProcessor; |
@@ -670,6 +675,14 @@ public void getWhenExcludeAuthorizationObservationsThenUnobserved() throws Excep |
670 | 675 | verifyNoInteractions(handler); |
671 | 676 | } |
672 | 677 |
|
| 678 | + @Test |
| 679 | + public void getWhenDeniedThenParameterizedAuthorizationDeniedEventIsPublished() throws Exception { |
| 680 | + this.spring.register(DenyAllConfig.class, EventPublisherConfig.class, AuthorizationDeniedListener.class) |
| 681 | + .autowire(); |
| 682 | + this.mvc.perform(get("/").with(user("user"))); |
| 683 | + assertThat(this.spring.getContext().getBean(AuthorizationDeniedListener.class).invocations).isEqualTo(1); |
| 684 | + } |
| 685 | + |
673 | 686 | @Test |
674 | 687 | public void requestMatchersWhenMultipleDispatcherServletsAndPathBeanThenAllows() throws Exception { |
675 | 688 | this.spring.register(MvcRequestMatcherBuilderConfig.class, BasicController.class) |
@@ -1390,4 +1403,26 @@ PathPatternRequestMatcherBuilderFactoryBean pathPatternFactoryBean() { |
1390 | 1403 |
|
1391 | 1404 | } |
1392 | 1405 |
|
| 1406 | + @Configuration |
| 1407 | + static class EventPublisherConfig { |
| 1408 | + |
| 1409 | + @Bean |
| 1410 | + static AuthorizationEventPublisher eventPublisher(ApplicationEventPublisher publisher) { |
| 1411 | + return new SpringAuthorizationEventPublisher(publisher); |
| 1412 | + } |
| 1413 | + |
| 1414 | + } |
| 1415 | + |
| 1416 | + @Component |
| 1417 | + static class AuthorizationDeniedListener { |
| 1418 | + |
| 1419 | + int invocations; |
| 1420 | + |
| 1421 | + @EventListener |
| 1422 | + void onRequestDenied(AuthorizationDeniedEvent<? extends HttpServletRequest> denied) { |
| 1423 | + this.invocations++; |
| 1424 | + } |
| 1425 | + |
| 1426 | + } |
| 1427 | + |
1393 | 1428 | } |
0 commit comments