Skip to content

Instantly share code, notes, and snippets.

@Zerek-Cheng
Created July 25, 2023 15:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zerek-Cheng/b6cc7a92a3ac64e6be852d1cb089e64b to your computer and use it in GitHub Desktop.
Save Zerek-Cheng/b6cc7a92a3ac64e6be852d1cb089e64b to your computer and use it in GitHub Desktop.
@WebFilter(filterName = "HttpMethodOverrideHeaderFilter")
public class HttpMethodOverrideHeaderFilter extends OncePerRequestFilter {
private static final String X_HTTP_METHOD_OVERRIDE_HEADER = "X-HTTP-Method-Override";
@Override
protected void doFilterInternal(HttpServletRequest request, @NotNull HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String headerValue = request.getHeader(X_HTTP_METHOD_OVERRIDE_HEADER);
if (RequestMethod.POST.name().equals(request.getMethod()) && StringUtils.hasLength(headerValue)) {
String method = headerValue.toUpperCase(Locale.ENGLISH);
HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);
filterChain.doFilter(wrapper, response);
} else {
filterChain.doFilter(request, response);
}
}
private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {
private final String method;
public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
super(request);
this.method = method;
}
@Override
public String getMethod() {
return this.method;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment