我无法在servlet过滤器中重定向用户。
@WebFilter(value = "/*", initParams = { @WebInitParam(name = "applicationClassName", value = "com.sample.Application"),
@WebInitParam(name="filterMappingUrlPattern", value="/*") })
public class CertFilter extends WicketFilter {
@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
if(USER_MATCHES_CONDITION){
httpServletResponse.setHeader("Location", "https://google.com");
httpServletResponse.sendRedirect("https://google.com");
}
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
}
google.com的请求已完成,但用户未被重定向到那里。截获的请求意味着在请求样式表时满足USER_MATCHES_CONDITION。因此,重定向请求标头为Accept:text/css,*/*;q=0.1
。所以我得到一个警告资源解释为样式表,但转移MIME类型text / html。可能这就是它没有重定向的原因。
如何拦截随机请求并将其重定向到外部网址?
编辑:即使我这样做:
@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
httpServletResponse.sendRedirect("https://google.com");
return;
}
它没有重定向用户...我猜wicket以某种方式覆盖它。