RedirectAttributes 如何在过滤器中使用
大侠们, 下午好,小弟有一Java 问题,想请教一下。
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain
)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpSession session = request.getSession();
HttpServletResponse reponse = (HttpServletResponse) servletResponse;
logger.info("*****Requested URL: " + request.getRequestURL() + "|" + df.format(session.getCreationTime()) + "|"
+ reponse.getStatus());
session.setAttribute("referer", request.getRequestURL().toString());
// Do Authentication
Object username = session.getAttribute("username");
if (username == null) {
logger.info("*****Unauthenticated.");
redirectAttr.addFlashAttribute("alert", "You don't have the permission to access this page, Please Login!");
reponse.sendRedirect("/portal/login");
return;
}
// Do Authorization
logger.info("*******" + username.toString() + "|" + request.getRequestURI());
if (!auth.authorize(username.toString(), request.getRequestURI(), authorizesvc, applicationname)) {
logger.info("*****Unauthorized for URL: " + request.getRequestURI());
redirectAttr.addFlashAttribute("alert", "You don't have the permission to access this page, Please Login!");
reponse.sendRedirect("/portal/login");
return;
}
}
我想要使用 RedirectAttributes 过滤器,该如何使用啊, 在线等 --------------------编程问答-------------------- 你的意思是:如果没有登录,就跳转到登陆页面? --------------------编程问答--------------------
public class SessionCheckFilter extends OncePerRequestFilter {--------------------编程问答-------------------- 没动楼主的需求 --------------------编程问答-------------------- 这个session 其实我也会; 就是不会用:RedirectAttributes redirectAttr 来实现显示错误信息;像这样:
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String uri = request.getRequestURI();
String ctxpath = request.getContextPath();
HttpSession session = ((HttpServletRequest) request).getSession();
UserVO user = (UserVO) session.getAttribute(FrontContants.SESSION_USER);
if ((uri.indexOf("/jsp/login.jsp") > -1)
|| (uri.indexOf("/loginAction/login.do") > -1)
|| (uri.equals(ctxpath + "/"))) {
filterChain.doFilter(request, response);
} else {
if (user == null ) {
response.sendRedirect(ctxpath + "/jsp/login.jsp");
} else {
filterChain.doFilter(request, response);
}
}
}
}
redirectAttr.addFlashAttribute("alert", "You don't have the permission to access this page)
这样可以实现嘛?
补充:Java , Java EE