北大青鸟武汉宏鹏鲁广校区老师解读: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自学好还是报培训班?看看老师怎么样推荐的
- 武汉哪家java培训机构好?教你快速选择靠谱的培训机构
- 如何选择靠谱的Java培训机构?
- 教你如何正确的选择java培训机构
- 武汉有短期Java培训吗?
- 武汉有短期Java培训吗?
- 武汉北大青鸟学员心得:我学java的一些小感想
- 武汉北大青鸟力荐Java学校,专注职业Java培训!
- 女生适合学Java吗?女生学Java会不会很难学不会?
- 在北大青鸟学习完java后可以找什么工作?
看过该Java开发的还看过
本文标题:#Java培训:Spring mvc Session拦截器#,宏鹏发布于北大青鸟鲁广校区。北大青鸟武汉宏鹏鲁广校区老师解读:Spring mvc Session拦截器