-
-
Save Zerek-Cheng/b6cc7a92a3ac64e6be852d1cb089e64b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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