SecurityFilter
public class SecurityFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
//强制转换
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
//是否登录判断逻辑
//先判断有无session对象存在
HttpSession session = request.getSession(false);
if(session==null){
//没有登录
response.sendRedirect(request.getContextPath()+"/noAuth.html");
return;
}else{
String user = (String)session.getAttribute("user");
if(user==null){
//没有登录成功
response.sendRedirect(request.getContextPath()+"/noAuth.html");
return;
}
}
//如果已经登录成功了,则放行!
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
问题:
现在有
login.jsp
modify.jsp
index.html
noAuth.html
几个页面,很明显login.jsp和noAuth.html没有必要拦截,因为这些本来就是拦截后的结果,是要接着做出处理的,那怎么针对性的对其他页面做出拦截配置呢?
- filter配置文件中
<url-pattern>/modify.jsp</url-pattern>
<url-pattern>/noAuth.html</url-pattern>
- 可以把要拦截的页面统一放到一个文件夹下
<url-pattern>/文件夹/*</url-pattern>
Paste_Image.png