熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Spring mvc Session攔截器

2013-11-23 20:40:07  來源: Java開源技術 

  未登錄不允許訪問項目文件夾內的頁面那如何判斷是否登錄呢?

  下面向大家詳細介紹spring mvc Session攔截器的使用本文以登錄為例未登錄無權限訪問項目內頁面session為空時返回項目首頁

  代碼如下

  [html]

  package comsundearsundearemminterceptor;

  import comsundearsundearemmmodelUser;

  import javautilloggingLevel;

  import javautilloggingLogger;

  import javaxservlethttpHttpServletRequest;

  import javaxservlethttpHttpServletResponse;

  import orgspringframeworkwebservletHandlerInterceptor;

  import orgspringframeworkwebservletModelAndView;

  /**

  *

  * @author yh

  */

  public class SessionInterceptor implements HandlerInterceptor {

  private Logger logger = LoggergetLogger(SessionInterceptorclassgetName());

  @Override

  public boolean preHandle(HttpServletRequest hsr HttpServletResponse hsr Object o) throws Exception {

  User user=(User) hsrgetSession()getAttribute(LoginUser);

  if(user==null){

  loggerlog(LevelINFO user not login);

  hsrsendRedirect(/SundearEmm);

  return false;

  }

  return true;

  }

  @Override

  public void postHandle(HttpServletRequest hsr HttpServletResponse hsr Object o ModelAndView mav) throws Exception {

  }

  @Override

  public void afterCompletion(HttpServletRequest hsr HttpServletResponse hsr Object o Exception excptn) throws Exception {

  }

  }

  package comsundearsundearemminterceptor;

  import comsundearsundearemmmodelUser;

  import javautilloggingLevel;

  import javautilloggingLogger;

  import javaxservlethttpHttpServletRequest;

  import javaxservlethttpHttpServletResponse;

  import orgspringframeworkwebservletHandlerInterceptor;

  import orgspringframeworkwebservletModelAndView;

  /**

  *

  * @author yh

  */

  public class SessionInterceptor implements HandlerInterceptor {

  private Logger logger = LoggergetLogger(SessionInterceptorclassgetName());

  @Override

  public boolean preHandle(HttpServletRequest hsr HttpServletResponse hsr Object o) throws Exception {

  User user=(User) hsrgetSession()getAttribute(LoginUser);

  if(user==null){

  loggerlog(LevelINFO user not login);

  hsrsendRedirect(/SundearEmm);

  return false;

  }

  return true;

  }

  @Override

  public void postHandle(HttpServletRequest hsr HttpServletResponse hsr Object o ModelAndView mav) throws Exception {

  }

  @Override

  public void afterCompletion(HttpServletRequest hsr HttpServletResponse hsr Object o Exception excptn) throws Exception {

  }

  }

  僅這些還不夠我們還需要修改spring mvc的配置文件dispatcherservletxml加入攔截器代碼如下

  [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=comsundearsundearemminterceptorSessionInterceptor></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=comsundearsundearemminterceptorSessionInterceptor></bean>

  </mvc:interceptor>

  </mvc:interceptors>如有訪客不明白的可以給我留言我會給大家滿意的解決方法……


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28855.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.