Skip to content

Commit

Permalink
Make ErrorPageSecurityFilter Servlet 3.1 compatible
Browse files Browse the repository at this point in the history
Restore Servlet 3.1 compatibly by implementing `Filter` rather
than extending the 4.0 `HttpFilter` type.

Fixes gh-28790
  • Loading branch information
mbhave authored and philwebb committed Nov 24, 2021
1 parent f621937 commit e6b5be9
Showing 1 changed file with 20 additions and 10 deletions.
Expand Up @@ -19,10 +19,12 @@
import java.io.IOException;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpFilter;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Expand All @@ -33,14 +35,14 @@
import org.springframework.security.web.access.WebInvocationPrivilegeEvaluator;

/**
* {@link HttpFilter} that intercepts error dispatches to ensure authorized access to the
* {@link Filter} that intercepts error dispatches to ensure authorized access to the
* error page.
*
* @author Madhura Bhave
* @author Andy Wilkinson
* @since 2.6.0
*/
public class ErrorPageSecurityFilter extends HttpFilter {
public class ErrorPageSecurityFilter implements Filter {

private static final WebInvocationPrivilegeEvaluator ALWAYS = new AlwaysAllowWebInvocationPrivilegeEvaluator();

Expand All @@ -53,18 +55,26 @@ public ErrorPageSecurityFilter(ApplicationContext context) {
}

@Override
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (DispatcherType.ERROR.equals(request.getDispatcherType())) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!getPrivilegeEvaluator().isAllowed(request.getRequestURI(), authentication)) {
sendError(request, response);
return;
}
doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain);
}

private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (DispatcherType.ERROR.equals(request.getDispatcherType()) && !isAllowed(request)) {
sendError(request, response);
return;
}
chain.doFilter(request, response);
}

private boolean isAllowed(HttpServletRequest request) {
String uri = request.getRequestURI();
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return getPrivilegeEvaluator().isAllowed(uri, authentication);
}

private WebInvocationPrivilegeEvaluator getPrivilegeEvaluator() {
WebInvocationPrivilegeEvaluator privilegeEvaluator = this.privilegeEvaluator;
if (privilegeEvaluator == null) {
Expand Down

1 comment on commit e6b5be9

@alexandreJavaDeveloper

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. This will help me on my current situation "java.lang.NoClassDefFoundError: javax/servlet/http/HttpFilter"

Please sign in to comment.