Skip error page security filter for non-error dispatch type
Update `ErrorPageSecurityFilter` to defensively check that the `DispatcherType` is `ERROR`. Although this check isn't necessary for regular applications, it is needed if MockMvc is being used. Fixes gh-28759
This commit is contained in:
parent
4eed637481
commit
f621937d3b
|
|
@ -18,6 +18,7 @@ package org.springframework.boot.web.servlet.filter;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.servlet.DispatcherType;
|
||||||
import javax.servlet.FilterChain;
|
import javax.servlet.FilterChain;
|
||||||
import javax.servlet.RequestDispatcher;
|
import javax.servlet.RequestDispatcher;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
|
|
@ -54,10 +55,12 @@ public class ErrorPageSecurityFilter extends HttpFilter {
|
||||||
@Override
|
@Override
|
||||||
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
||||||
throws IOException, ServletException {
|
throws IOException, ServletException {
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
if (DispatcherType.ERROR.equals(request.getDispatcherType())) {
|
||||||
if (!getPrivilegeEvaluator().isAllowed(request.getRequestURI(), authentication)) {
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
sendError(request, response);
|
if (!getPrivilegeEvaluator().isAllowed(request.getRequestURI(), authentication)) {
|
||||||
return;
|
sendError(request, response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
chain.doFilter(request, response);
|
chain.doFilter(request, response);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.boot.web.servlet.filter;
|
package org.springframework.boot.web.servlet.filter;
|
||||||
|
|
||||||
|
import javax.servlet.DispatcherType;
|
||||||
import javax.servlet.FilterChain;
|
import javax.servlet.FilterChain;
|
||||||
import javax.servlet.RequestDispatcher;
|
import javax.servlet.RequestDispatcher;
|
||||||
|
|
||||||
|
|
@ -58,6 +59,7 @@ class ErrorPageSecurityFilterTests {
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void setup() {
|
||||||
|
this.request.setDispatcherType(DispatcherType.ERROR);
|
||||||
given(this.context.getBean(WebInvocationPrivilegeEvaluator.class)).willReturn(this.privilegeEvaluator);
|
given(this.context.getBean(WebInvocationPrivilegeEvaluator.class)).willReturn(this.privilegeEvaluator);
|
||||||
this.securityFilter = new ErrorPageSecurityFilter(this.context);
|
this.securityFilter = new ErrorPageSecurityFilter(this.context);
|
||||||
}
|
}
|
||||||
|
|
@ -95,4 +97,13 @@ class ErrorPageSecurityFilterTests {
|
||||||
verify(this.filterChain).doFilter(this.request, this.response);
|
verify(this.filterChain).doFilter(this.request, this.response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void ignorePrivilegeEvaluationForNonErrorDispatchType() throws Exception {
|
||||||
|
this.request.setDispatcherType(DispatcherType.REQUEST);
|
||||||
|
given(this.privilegeEvaluator.isAllowed(anyString(), any())).willReturn(false);
|
||||||
|
this.securityFilter.doFilter(this.request, this.response, this.filterChain);
|
||||||
|
verifyNoInteractions(this.privilegeEvaluator);
|
||||||
|
verify(this.filterChain).doFilter(this.request, this.response);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue