如下可以解决用户注销时跳出iframe框架的问题
过滤器中的代码:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res= (HttpServletResponse) response;
String currentURI = req.getRequestURI();
String targerURI = currentURI.substring(currentURI.indexOf("/"), currentURI.length());
Cookie[] cook = req.getCookies();
// 获取COOKIE时得先判断是否为空,然后再去获取相关值
String username = "";
if (cook.length != 0) {
for (int i = 0; i < cook.length; i++) {
if (cook[i].getName().equalsIgnoreCase("adminUser")) {
username = cook[i].getValue().toString();
}
}
}
// 判断如果没有取到用户信息,就跳转到登陆页面
if ((targerURI.endsWith("loginIndex.do")) || targerURI.endsWith("valiCode.do")) {
chain.doFilter(request, response);
} else if (username == null || "".equals(username)) {
// 跳转到登陆页面window
res.getWriter().print("<html><script type='text/javascript'> top.location.href='/cloudsync-admin/'</script></html>");
res.setContentType("text/html");
} else {
// 已经登陆,继续此次请求
chain.doFilter(request, response);
}
}
注意: 在res.getWriter().print("<html><script type='text/javascript'> top.location.href='/cloudsync-admin/'</script></html>");
后面一定要加入,此行代码:res.setContentType("text/html");
否则,在火狐中不登陆直接访问登陆后的某个页面的话, res.getWriter().print()中的html及js代码会被浏览器转义,并且直接打印出来