Last active
May 21, 2023 07:19
-
-
Save sandipchitale/76ed317ec3b8481da711ba9e37efa683 to your computer and use it in GitHub Desktop.
Modify HttpServletResponse #spring-web-mvc #springframework
This file contains hidden or 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
public static class LoginResponseFilter extends OncePerRequestFilter { | |
@Override | |
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, | |
FilterChain filterChain) throws ServletException, IOException { | |
if (request.getRequestURI().equals("/login") && request.getMethod().equals(HttpMethod.GET.name())) { | |
ContentCachingResponseWrapper contentCachingResponseWrapper = new ContentCachingResponseWrapper(response); | |
filterChain.doFilter(request, contentCachingResponseWrapper); | |
byte[] content = contentCachingResponseWrapper.getContentAsByteArray(); | |
String html = new String(content, StandardCharsets.UTF_8); | |
// Fix favicon.ico | |
html = html.replaceFirst("<head>", | |
"<head>\n <link rel=\"shortcut icon\" href=\"data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" type=\"image/x-icon\">"); | |
// Hack title | |
html = html.replaceFirst("<title>.*</title>", "<title>Got you!</title>"); | |
response.getWriter().write(html); | |
return; | |
} | |
filterChain.doFilter(request, response); | |
} | |
} | |
@Bean | |
FilterRegistrationBean<LoginResponseFilter> loginResponseFilter() { | |
FilterRegistrationBean<LoginResponseFilter> registrationBean = new FilterRegistrationBean<>(); | |
registrationBean.setFilter(new LoginResponseFilter()); | |
registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE); | |
return registrationBean; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment