Skip to content

Instantly share code, notes, and snippets.

@romanoffs
Created February 6, 2019 19:02
Show Gist options
  • Save romanoffs/93d3215567b125de366059fe35c55660 to your computer and use it in GitHub Desktop.
Save romanoffs/93d3215567b125de366059fe35c55660 to your computer and use it in GitHub Desktop.
Forward Request for Angular 2x
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class Forward extends OncePerRequestFilter {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
String uri = request.getRequestURI();
String contextPath = request.getContextPath();
if (!uri.startsWith(contextPath + "/api") &&
!uri.startsWith(contextPath + "/assets") &&
!uri.equals(contextPath) &&
// only forward if there's no file extension (exclude *.js, *.css etc)
uri.matches("^([^.]+)$")) {
logger.debug("Forward Request filter '{}'", request.getRequestURL());
RequestDispatcher dispatcher = request.getRequestDispatcher("/");
dispatcher.forward(request, response);
return;
}
chain.doFilter(request, response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment