1、创建过滤器包,创建LoginFilter类
2.在LoginFilter类中写过滤代码
//白名单
List<String> whitelist = Arrays.asList("/login.jsp");
//用来存放配置文件中Action节点的属性
List<Action> actionList = new ArrayList<>();
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String path = request.getServletPath();
Object username = request.getSession().getAttribute("user");
if (username != null) {
//有登录,直接放行
filterChain.doFilter(request, response);
return;
} else if (whitelist.contains(path)) {//在白名单中
//放行
filterChain.doFilter(servletRequest, servletResponse);
return;
} else {
//没有有登录,重定向登录页面
response.sendRedirect("login.jsp");
}
}
Filter层中的LoginFilter类
Servlet层中的user (从登录页面获取的用户值,并存入Session中)
完成以上操作就可以实现不管进入那个页面都会跳到登录页面! ! !