package com.trsmedia.interceptor; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * * @author : qinyi * date : 2019年7月10日,下午1:58:30 */ public class MySessionListener implements HttpSessionListener { public void sessionCreated(final HttpSessionEvent se) { Timer t = new Timer(); t.schedule(new TimerTask() { public void run() { HttpSession session = se.getSession(); System.out.print("del..."+new Date()); session.invalidate(); } }, 1000*60*10, 1000*60*10); } public void sessionDestroyed(HttpSessionEvent se) { // TODO Auto-generated method stub } }
package com.trsmedia.interceptor; import java.util.Timer; import java.util.TimerTask; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.http.HttpServletRequest; /** * * @author : qinyi * date : 2019年7月10日,下午4:28:11 */ public class MyRequestContextListener implements ServletRequestListener { @Override public void requestDestroyed(final ServletRequestEvent sre) { // TODO Auto-generated method stub Timer t = new Timer(); t.schedule(new TimerTask() { public void run() { HttpServletRequest request = (HttpServletRequest) sre.getServletRequest(); javax.servlet.http.Cookie[]cookies=request.getCookies(); if(cookies!=null){ for(javax.servlet.http.Cookie c:cookies){ System.out.println("正再删除"+c.getName()); c.setMaxAge(0); } } } }, 1000*60*10, 1000*60*10); } @Override public void requestInitialized(ServletRequestEvent sre) { // TODO Auto-generated method stub } }
web.xml
<listener> <listener-class>com.trsmedia.interceptor.MyRequestContextListener</listener-class> </listener> <listener> <listener-class>com.trsmedia.interceptor.MySessionListener</listener-class> </listener>
package com.trsmedia.interceptor; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; /** * * @author : qinyi * date : 2019年7月9日,下午10:59:49 */ public class LoginInterceptor extends HandlerInterceptorAdapter { private List<String> IGNORE_URI; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession(); if(session != null && session.getAttribute("UserInfo") != null){ return true; }else{ response.sendRedirect("main.jsp"); return false; } } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { super.postHandle(request, response, handler, modelAndView); } public List<String> getIGNORE_URI() { return IGNORE_URI; } public void setIGNORE_URI(List<String> IGNORE_URI) { this.IGNORE_URI = IGNORE_URI; } }
spring.xml
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <mvc:exclude-mapping path="/static/**"/> <mvc:exclude-mapping path="/nosso.do"/> <mvc:exclude-mapping path="/nosso.jsp"/> <mvc:exclude-mapping path="/main.jsp"/> <mvc:exclude-mapping path="/login.do"/> <mvc:exclude-mapping path="/logout.do"/> <mvc:exclude-mapping path="/loginChoose.do"/> <bean id="loginInterceptor" class="com.trsmedia.interceptor.LoginInterceptor"> </bean> </mvc:interceptor> </mvc:interceptors>