SpringMVC如何使用攔截器控制登錄

SpringMVC如何使用攔截器控制登錄?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

目前創(chuàng)新互聯(lián)已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、九原網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

SpringMVC讀取Cookie判斷用戶是否登錄,對每一個action都要進(jìn)行判斷。之前使用jstl標(biāo)簽在頁面上判斷session如果沒有登錄就使用如下代碼跳轉(zhuǎn)到登錄頁面。

<c:if test="${sessionScope.login == null || sessionScope.login == false}">
 <!-- 未登錄 -->
 <c:redirect url="/login"/>
</c:if>
<c:if test="${sessionScope.login}">
 <!-- 已登錄 -->
</c:if>

但是測試發(fā)現(xiàn)如果session過期,頁面渲染就會無故中斷并且不會跳轉(zhuǎn)到登錄頁面。故嘗試使用攔截器來進(jìn)行登錄判斷。

攔截器配置文件如下

<!-- <mvc:mapping path="/**" /> 如果只寫一個*,則不能攔截類似/*/*的請求。靜態(tài)資源的請求需要判斷不進(jìn)行攔截 -->
<mvc:interceptors>
 <mvc:interceptor>
  <mvc:mapping path="/**" />
  <bean class="com.ts.settle.tools.interceptor.LoginInterceptor">
   <property name="excludedUrls">
    <list>
     <value>/login</value>
     <value>/static/</value>
    </list>
   </property>
  </bean>
 </mvc:interceptor>
</mvc:interceptors>

攔截器實(shí)現(xiàn)類如下

public class LoginInterceptor implements HandlerInterceptor {
 private AvatarLogger logger = AvatarLoggerFactory.getLogger(this.getClass());

 private List<String> excludedUrls;

 /**
  * 在DispatcherServlet完全處理完請求后被調(diào)用
  * 當(dāng)攔截器拋出異常時,依然會從當(dāng)前攔截器往回執(zhí)行所的攔截器的afterCompletion()
  */
 public void afterCompletion(HttpServletRequest request,
        HttpServletResponse response, Object handler, Exception exception)
   throws Exception {

 }

 //在業(yè)務(wù)處理器處理請求執(zhí)行完成后,生成視圖之前執(zhí)行的動作
 public void postHandle(HttpServletRequest request, HttpServletResponse response,
       Object handler, ModelAndView modelAndView) throws Exception {

 }

 /**
  * 在業(yè)務(wù)處理器處理請求之前被調(diào)用
  * 如果返回false 則退出本攔截器,本攔截器后面的postHandle與afterCompletion不再執(zhí)行
  */
 public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
        Object handler) throws Exception {

  String requestUri = request.getRequestURI();
  for (String url : excludedUrls) {
   if (requestUri.contains(url)) {
    return true;
   }
  }

  HttpSession session = request.getSession();
  Boolean login = (Boolean) session.getAttribute("login");
  if (login == null || !login) {
   //System.out.println(request.getContextPath());
   logger.info("Pedirect to login page");
   response.sendRedirect(request.getContextPath() + "/login");
  }
  return true;
 }

 public void setExcludedUrls(List<String> excludedUrls) {
  this.excludedUrls = excludedUrls;
 }
}

關(guān)于SpringMVC如何使用攔截器控制登錄問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

分享文章:SpringMVC如何使用攔截器控制登錄
網(wǎng)站網(wǎng)址:http://muchs.cn/article8/jopiop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、搜索引擎優(yōu)化、微信公眾號全網(wǎng)營銷推廣、小程序開發(fā)、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站