• Spring mvc Session拦截器


    2013-04-17
     
    未登录,不允许访问项目文件夹内的页面,那如何判断是否登录呢?
    下面向大家详细介绍spring mvc Session拦截器的使用,本文以登录为例,未登录无权限访问项目内页面,session为空时返回项目首页。
    代码如下:
     
    [html]
    package com.sundear.sundearemm.interceptor; 
     
    import com.sundear.sundearemm.model.User; 
    import java.util.logging.Level; 
    import java.util.logging.Logger; 
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 
    import org.springframework.web.servlet.HandlerInterceptor; 
    import org.springframework.web.servlet.ModelAndView; 
     
    /** 
     * 
     * @author yh 
     */ 
    public class SessionInterceptor implements HandlerInterceptor { 
     
        private Logger logger = Logger.getLogger(SessionInterceptor.class.getName()); 
     
        @Override 
        public boolean preHandle(HttpServletRequest hsr, HttpServletResponse hsr1, Object o) throws Exception { 
            User user=(User) hsr.getSession().getAttribute("LoginUser"); 
            if(user==null){ 
                logger.log(Level.INFO, "user not login"); 
                hsr1.sendRedirect("/SundearEmm"); 
                return false; 
            } 
            return true; 
        } 
     
        @Override 
        public void postHandle(HttpServletRequest hsr, HttpServletResponse hsr1, Object o, ModelAndView mav) throws Exception { 
        } 
     
        @Override 
        public void afterCompletion(HttpServletRequest hsr, HttpServletResponse hsr1, Object o, Exception excptn) throws Exception { 
        } 
    package com.sundear.sundearemm.interceptor;
    import com.sundear.sundearemm.model.User;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    /**
     *
     * @author yh
     */
    public class SessionInterceptor implements HandlerInterceptor {
        private Logger logger = Logger.getLogger(SessionInterceptor.class.getName());
        @Override
        public boolean preHandle(HttpServletRequest hsr, HttpServletResponse hsr1, Object o) throws Exception {
            User user=(User) hsr.getSession().getAttribute("LoginUser");
            if(user==null){
                logger.log(Level.INFO, "user not login");
                hsr1.sendRedirect("/SundearEmm");
                return false;
            }
            return true;
        }
        @Override
        public void postHandle(HttpServletRequest hsr, HttpServletResponse hsr1, Object o, ModelAndView mav) throws Exception {
        }
        @Override
        public void afterCompletion(HttpServletRequest hsr, HttpServletResponse hsr1, Object o, Exception excptn) throws Exception {
        }
    }仅这些还不够,我们还需要修改spring mvc的配置文件dispatcher-servlet.xml,加入拦截器代码如下:
     
    [html]
    <!-- interceptors --> 
        <mvc:interceptors >     
            <mvc:interceptor>     
                <mvc:mapping path="/group/*" /> 
                <mvc:mapping path="/user/*"/> 
                <mvc:mapping path="/subscribe/*"/> 
                <mvc:mapping path="/recipient/*"/> 
                <mvc:mapping path="/mail/*"/> 
                <bean class="com.sundear.sundearemm.interceptor.SessionInterceptor"></bean>     
            </mvc:interceptor>     
        </mvc:interceptors> 
    <!-- interceptors -->
        <mvc:interceptors >   
            <mvc:interceptor>   
                <mvc:mapping path="/group/*" />
                <mvc:mapping path="/user/*"/>
                <mvc:mapping path="/subscribe/*"/>
                <mvc:mapping path="/recipient/*"/>
                <mvc:mapping path="/mail/*"/>
                <bean class="com.sundear.sundearemm.interceptor.SessionInterceptor"></bean>   
            </mvc:interceptor>   
        </mvc:interceptors>如有访客不明白的可以给我留言,我会给大家满意的解决方法……
  • 相关阅读:
    Delphi TStringList的用法
    Android Studio使用教程(一)
    如何在win7下安装和配置Android Studio
    python基础字符串单引号双引号和三引号
    同步和互斥
    posix多线程有感线程高级编程(条件变量)
    VMware网络配置详解
    posix多线程有感POSIX 线程间的内存可视性
    进程及相关API
    POSIX线程属性
  • 原文地址:https://www.cnblogs.com/huapox/p/3516063.html
Copyright © 2020-2023  润新知