我在项目中使用day CQ开发过滤器时遇到问题。我的代码正在使用requestDispatcher的forward方法。但是使用response.sendRedirect(),它无法正常工作。
if (servletRequest instanceof SlingHttpServletRequest) {
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
String uri = httpRequest.getRequestURI();
if(uri.startsWith("/content")){
Cookie[] cookies = httpRequest.getCookies();
boolean found = false;
for (Cookie cookie : cookies) {
log.info("Cookies in filter: " + cookie.getName());
if("LtpaToken2".equals(cookie.getName())){
log.info("LTPA token found ");
found = true;
break;
}
}
if(found){
filterChain.doFilter(servletRequest, servletResponse);
}
else{
httpResponse.sendRedirect("http://www.google.com");
}
}else{
filterChain.doFilter(servletRequest, servletResponse);
}
log.info("Message URL ::"+httpRequest.getRequestURL().toString());
log.info("URI ::"+uri);
}
所以请解决我的问题,这是非常值得赞赏的。
此致 Narsi p
答案 0 :(得分:0)
我不知道Spring,但是在JEE中你无法像过滤器中那样操纵响应。使用IllegalStateException
或sendRedirect
时,您将获得sendError
。
我的建议是永远不要使用这些抽象。您要做的是指定响应标头并关闭请求。
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", ""http://www.google.com");
return;