新建一个过滤器
package com.autumn.filter; import com.autumn.pojo.Users; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 是否登录判断 */ public class CheckLoginFilter implements Filter { private String USER =""; private String ROOTPATH; @Override public void init(FilterConfig filterConfig) throws ServletException { this.USER = filterConfig.getInitParameter("user").trim(); this.ROOTPATH = filterConfig.getInitParameter("rootPath"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { Object userid = null; HttpServletResponse res=(HttpServletResponse)response; HttpServletRequest req=(HttpServletRequest)request; Users users = (Users)req.getSession().getAttribute(USER); if (users!=null){ userid =users.getId(); } System.out.println("req.getRequestURI():"+req.getRequestURI()); if(isShakedown(3018,3,16)) { System.out.println("系统试用期已到,请联系商务!"); res.sendRedirect(ROOTPATH+"?rtnCode=200"); }else if(req.getRequestURI().contains("/loginController/login")){ //当是登录请求时,继续往下执行 filterChain.doFilter(req, res); }else if(userid==null||userid.toString().trim().isEmpty()) //当没有session时,即未登录,跳到登录页 { //System.out.println("CheckLoginFilter userid:"+userid); System.out.println("CheckLoginFilter userId is null"); res.sendRedirect(ROOTPATH); }else{ //继续往下执行 //System.out.println("CheckLoginFilter chain.doFilter"); filterChain.doFilter(req, res); } } @Override public void destroy() { if(this.USER !=null&&this.USER.trim().isEmpty()) { this.USER =""; } if(this.ROOTPATH!=null&&this.ROOTPATH.trim().isEmpty()) { this.ROOTPATH=""; } } //试用期判断 public static boolean isShakedown(int year,int month,int day) { Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance();// 获取一个Claender实例 cal.set(year, month-1, day); Date endDate; try { endDate = sf.parse(sf.format(cal.getTime())); if (date.getTime() > endDate.getTime()) { return true; } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); return true; } return false; } }
web.xml中加入过滤器配置
<filter> <filter-name>loginFilter</filter-name> <filter-class> com.autumn.filter.CheckLoginFilter </filter-class> <init-param> <param-name>excludedPages</param-name> <param-value>/login.jsp</param-value><!-- 登陆页不做拦截的请求声明,filter中登录的接口也被排除--> </init-param> <init-param> <param-name>user</param-name> <!--配置的session参数--> <param-value>user</param-value> </init-param> <init-param> <param-name>rootPath</param-name> <!-- 配置的登录页 --> <param-value>/Bookkeeping/login.jsp</param-value> </init-param> </filter> <filter-mapping> <!--拦截映射一 --> <filter-name>loginFilter</filter-name> <url-pattern>/manager/*</url-pattern> <!--拦截后台的请求接口--> </filter-mapping> <filter-mapping><!--拦截映射二 --> <filter-name>loginFilter</filter-name> <url-pattern>/jsp/*</url-pattern> <!--拦截直接访问前端页面--> </filter-mapping>