北大青鸟武汉宏鹏鲁广校区老师解读:Spring mvc Session拦截器
未登录,不允许访问项目文件夹内的页面,那如何判断是否登录呢?
下面向大家详细介绍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>如有访客不明白的可以给我留言,我会给大家满意的解决方法……
本文标题:Java培训:Spring mvc Session拦截器,责任编辑:宏鹏,来源:武汉北大青鸟鲁广校区栏目,于2013-04-20 09:29:28发布于北大青鸟鲁广校区。北大青鸟武汉宏鹏鲁广校区老师解读:Spring mvc Session拦截器
预约将免费领取7天课程体验卡
只为您方便就学
专业老师24小时1对1学习指导
定制专属于你的专属学习方案
微信号:17740513250
微信号:17740513250