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>