|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import java.util.function.Supplier;
|
20 | 20 |
|
| 21 | +import javax.servlet.DispatcherType; |
21 | 22 | import javax.servlet.FilterChain;
|
22 | 23 | import javax.servlet.http.HttpServletRequest;
|
23 | 24 |
|
|
38 | 39 | import org.springframework.security.core.context.SecurityContext;
|
39 | 40 | import org.springframework.security.core.context.SecurityContextHolder;
|
40 | 41 | import org.springframework.security.core.context.SecurityContextImpl;
|
| 42 | +import org.springframework.web.util.WebUtils; |
41 | 43 |
|
42 | 44 | import static org.assertj.core.api.Assertions.assertThat;
|
43 | 45 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
@@ -165,4 +167,67 @@ public void doFilterWhenAuthorizationEventPublisherThenUses() throws Exception {
|
165 | 167 | any(AuthorizationDecision.class));
|
166 | 168 | }
|
167 | 169 |
|
| 170 | + @Test |
| 171 | + public void doFilterWhenErrorThenDoNotFilter() throws Exception { |
| 172 | + AuthorizationManager<HttpServletRequest> authorizationManager = mock(AuthorizationManager.class); |
| 173 | + AuthorizationFilter authorizationFilter = new AuthorizationFilter(authorizationManager); |
| 174 | + MockHttpServletRequest mockRequest = new MockHttpServletRequest(null, "/path"); |
| 175 | + mockRequest.setDispatcherType(DispatcherType.ERROR); |
| 176 | + mockRequest.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error"); |
| 177 | + MockHttpServletResponse mockResponse = new MockHttpServletResponse(); |
| 178 | + FilterChain mockFilterChain = mock(FilterChain.class); |
| 179 | + |
| 180 | + authorizationFilter.doFilter(mockRequest, mockResponse, mockFilterChain); |
| 181 | + verifyNoInteractions(authorizationManager); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void doFilterWhenErrorAndShouldFilterAllDispatcherTypesThenFilter() throws Exception { |
| 186 | + AuthorizationManager<HttpServletRequest> authorizationManager = mock(AuthorizationManager.class); |
| 187 | + AuthorizationFilter authorizationFilter = new AuthorizationFilter(authorizationManager); |
| 188 | + authorizationFilter.setShouldFilterAllDispatcherTypes(true); |
| 189 | + MockHttpServletRequest mockRequest = new MockHttpServletRequest(null, "/path"); |
| 190 | + mockRequest.setDispatcherType(DispatcherType.ERROR); |
| 191 | + mockRequest.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error"); |
| 192 | + MockHttpServletResponse mockResponse = new MockHttpServletResponse(); |
| 193 | + FilterChain mockFilterChain = mock(FilterChain.class); |
| 194 | + |
| 195 | + authorizationFilter.doFilter(mockRequest, mockResponse, mockFilterChain); |
| 196 | + verify(authorizationManager).check(any(Supplier.class), any(HttpServletRequest.class)); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + public void doFilterNestedErrorDispatchWhenAuthorizationManagerThenUses() throws Exception { |
| 201 | + AuthorizationManager<HttpServletRequest> authorizationManager = mock(AuthorizationManager.class); |
| 202 | + AuthorizationFilter authorizationFilter = new AuthorizationFilter(authorizationManager); |
| 203 | + authorizationFilter.setShouldFilterAllDispatcherTypes(true); |
| 204 | + MockHttpServletRequest mockRequest = new MockHttpServletRequest(null, "/path"); |
| 205 | + mockRequest.setDispatcherType(DispatcherType.ERROR); |
| 206 | + mockRequest.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error"); |
| 207 | + MockHttpServletResponse mockResponse = new MockHttpServletResponse(); |
| 208 | + FilterChain mockFilterChain = mock(FilterChain.class); |
| 209 | + |
| 210 | + authorizationFilter.doFilterNestedErrorDispatch(mockRequest, mockResponse, mockFilterChain); |
| 211 | + verify(authorizationManager).check(any(Supplier.class), any(HttpServletRequest.class)); |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + public void doFilterNestedErrorDispatchWhenAuthorizationEventPublisherThenUses() throws Exception { |
| 216 | + AuthorizationFilter authorizationFilter = new AuthorizationFilter( |
| 217 | + AuthenticatedAuthorizationManager.authenticated()); |
| 218 | + MockHttpServletRequest mockRequest = new MockHttpServletRequest(null, "/path"); |
| 219 | + MockHttpServletResponse mockResponse = new MockHttpServletResponse(); |
| 220 | + FilterChain mockFilterChain = mock(FilterChain.class); |
| 221 | + |
| 222 | + SecurityContext securityContext = new SecurityContextImpl(); |
| 223 | + securityContext.setAuthentication(new TestingAuthenticationToken("user", "password", "ROLE_USER")); |
| 224 | + SecurityContextHolder.setContext(securityContext); |
| 225 | + |
| 226 | + AuthorizationEventPublisher eventPublisher = mock(AuthorizationEventPublisher.class); |
| 227 | + authorizationFilter.setAuthorizationEventPublisher(eventPublisher); |
| 228 | + authorizationFilter.doFilterNestedErrorDispatch(mockRequest, mockResponse, mockFilterChain); |
| 229 | + verify(eventPublisher).publishAuthorizationEvent(any(Supplier.class), any(HttpServletRequest.class), |
| 230 | + any(AuthorizationDecision.class)); |
| 231 | + } |
| 232 | + |
168 | 233 | }
|
0 commit comments