日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

springmvc拦截器配置

發(fā)布時(shí)間:2025/3/17 c/c++ 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springmvc拦截器配置 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、目的:攔截器的配置主要是對(duì)請(qǐng)求進(jìn)行相應(yīng)的處理(在登錄和session過(guò)時(shí)的時(shí)候是一個(gè)很好的方式)

2、由于Spring容器的優(yōu)越性,在配置的時(shí)候全部交給容器管理是一個(gè)很不錯(cuò)的方式

3、配置攔截器:

  1)在對(duì)應(yīng)的方式類(lèi)中實(shí)現(xiàn)HandlerInterceptor接口,會(huì)要求實(shí)現(xiàn)3個(gè)方法

  

@Overridepublic void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)throws Exception {// TODO Auto-generated method stub}@Overridepublic void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)throws Exception {// TODO Auto-generated method stub}public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object obj) throws Exception {return true;}

  2)所有的請(qǐng)求都會(huì)在攔截器里面經(jīng)過(guò),這樣在做登錄攔截的時(shí)候會(huì)起到數(shù)據(jù)的保護(hù)和相關(guān)的展示限權(quán)

  在preHandle方法里面加入自己的邏輯

public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object obj) throws Exception {//獲取判定登陸的session是否存在String token = (String) request.getSession().getAttribute("token");String postId = (String) request.getSession().getAttribute("postId");if(token == null || token == ""){String XRequested =request.getHeader("X-Requested-With");if("XMLHttpRequest".equals(XRequested)){response.getWriter().write("IsAjax");}else{response.sendRedirect("/m-web/");}return false;}if(postId == null || postId == ""){String XRequested =request.getHeader("X-Requested-With");if("XMLHttpRequest".equals(XRequested)){response.getWriter().write("IsAjax");}else{response.sendRedirect("/m-web/");}return false;}return true;}

  里面存在ajax請(qǐng)求攔截的處理詳情見(jiàn):http://www.cnblogs.com/ll409546297/p/6203403.html

3、在Spring的配置文件里面加入

<mvc:interceptors><mvc:interceptor><mvc:mapping path="/**"/><mvc:exclude-mapping path="/user/login"/><mvc:exclude-mapping path="/user/loginOut"/><mvc:exclude-mapping path="/user/setPostId"/><mvc:exclude-mapping path="/user/getPostId"/><mvc:exclude-mapping path="/resources/**"/><mvc:exclude-mapping path="/assets/**"/><mvc:exclude-mapping path="/css/**"/><mvc:exclude-mapping path="/fonts/**"/><mvc:exclude-mapping path="/images/**"/><mvc:exclude-mapping path="/img/**"/><mvc:exclude-mapping path="/js/**"/><mvc:exclude-mapping path="/pic/**"/><mvc:exclude-mapping path="/plugins/**"/><mvc:exclude-mapping path="/static/**"/><mvc:exclude-mapping path="/ui/**"/><bean class="com.troy.ai.web.Interceptors.LoginInterceptor"/></mvc:interceptor></mvc:interceptors>

解釋:

<mvc:mapping path="/**"/> //是對(duì)所有路徑進(jìn)行過(guò)濾 <mvc:exclude-mapping path="/user/login"/> //是對(duì)該路徑進(jìn)行放行 <mvc:exclude-mapping path="/ui/**"/> //是對(duì)靜態(tài)資源加載放行 <bean class="com.troy.ai.web.Interceptors.LoginInterceptor"/> //指定處理攔截的相關(guān)類(lèi)

4、攔截器的配置相對(duì)簡(jiǎn)單,但是在實(shí)際的應(yīng)用過(guò)程卻非常常見(jiàn),邏輯處理方面需要根據(jù)實(shí)際來(lái)處理

總結(jié)

以上是生活随笔為你收集整理的springmvc拦截器配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。