• 监听器、拦截器完成对session、cookie的会话控制


    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>
    

      

  • 相关阅读:
    正则表达式数据验证
    .NET开发工具
    (收藏)自己觉得好的学习网站 持续更新... ...
    MSSQL获取表结构通用方法
    第一阶段冲刺个人博客07
    第一阶段冲刺个人博客06
    第8周学习进度
    第一阶段冲刺个人博客05
    ”图书馆助手“典型用户和用户场景
    ”图书馆助手“典型用户和用户场景
  • 原文地址:https://www.cnblogs.com/qinyios/p/11167346.html
Copyright © 2020-2023  润新知