国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

詳解springmvc攔截器攔截靜態(tài)資源

2019-11-26 13:12:30
字體:
供稿:網(wǎng)友

springmvc攔截器interceptors

springmvc攔截器能夠?qū)φ?qǐng)求的資源路徑進(jìn)行攔截,極大的簡化了攔截器的書寫。但是,千萬千萬要注意一點(diǎn):靜態(tài)資源的放行。

上代碼:

<mvc:resources mapping="/resources/**" location="/static/resources" />   <mvc:resources mapping="/static/css/**" location="/static/css/" />   <mvc:resources mapping="/static/images/**" location="/static/images/" />   <mvc:resources mapping="/static/js/**" location="/static/js/" /> 
<mvc:interceptors>     <!-- 使用bean定義一個(gè)Interceptor,直接定義在mvc:interceptors根下面的Interceptor將攔截所有的請(qǐng)求      <bean class="com.myTree.interceptor.LoginInterceptor" />-->     <mvc:interceptor>       <mvc:mapping path="/**" />       <!-- 需排除攔截的地址 -->        <mvc:exclude-mapping path="/Login"/>        <mvc:exclude-mapping path="/login"/>        <mvc:exclude-mapping path="/sattic/**"/>        <!-- 定義在mvc:interceptor下面的表示是對(duì)特定的請(qǐng)求才進(jìn)行攔截的 -->       <beans:bean class="com.myTree.interceptor.LoginInterceptor" >       </beans:bean>     </mvc:interceptor>   </mvc:interceptors> 

問題來了,在請(qǐng)求jsp頁面的時(shí)候,你的靜態(tài)資源的訪問仍然會(huì)被自定義攔截器攔截,這會(huì)導(dǎo)致程序運(yùn)行的效率大大下降,還會(huì)不停的跳轉(zhuǎn)到攔截器的邏輯。主要原因是

<mvc:mapping path="/**" /> 

會(huì)對(duì)所有的請(qǐng)求資源進(jìn)行攔截,雖然靜態(tài)資源已經(jīng)排除了,但還是會(huì)被攔截到。

如何解決這個(gè)bug呢?

主要有三種方式:

1、修改請(qǐng)求的url地址。

 如果請(qǐng)求的url地址都是以*.do結(jié)尾,那么攔截器中的配置可以變?yōu)閿r截以do結(jié)尾的資源,靜態(tài)資源自然就不會(huì)被攔截到了;
2、在自定義攔截器中對(duì)資源進(jìn)行判斷,如果滿足需要排除的資源,就進(jìn)行放行。

<!-- 攔截器配置 -->    <mvc:interceptors>     <!-- session超時(shí) -->     <mvc:interceptor>      <mvc:mapping path="/*/*"/>      <bean class="com.myTree.interceptor.LoginInterceptor">       <property name="allowUrls">        <list>         <!-- 如果請(qǐng)求中包含以下路徑,則不進(jìn)行攔截 -->         <value>/login</value>         <value>/js</value>         <value>/css</value>         <value>/image</value>         <value>/images</value>        </list>       </property>      </bean>     </mvc:interceptor>    </mvc:interceptors>  

在攔截器中設(shè)定不攔截的屬性: 

/**  * 處理登錄攔截器  */  public class LoginInterceptor implements HandlerInterceptor{        public String[] allowUrls;//還沒發(fā)現(xiàn)可以直接配置不攔截的資源,所以在代碼里面來排除        public void setAllowUrls(String[] allowUrls) {      this.allowUrls = allowUrls;    }      @Override    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,        Object handler) throws Exception {<pre name="code" class="java"> <span style="white-space:pre"> </span>String requestUrl = request.getRequestURI().replace(request.getContextPath(), "");       System.out.println(requestUrl);      if(null != allowUrls && allowUrls.length>=1){        for(String url : allowUrls) {           if(requestUrl.contains(url)) {             return true;           }         } } 

3、設(shè)置web.xml中的默認(rèn)攔截器,不攔截靜態(tài)資源

在springmvc的Dispatcher中配置<mvc:default-servlet-handler />(一般Web應(yīng)用服務(wù)器默認(rèn)的Servlet名稱是"default",所以這里我們激活Tomcat的defaultServlet來處理靜態(tài)文件,在web.xml里配置如下代碼即可:)

<!--該servlet為tomcat,jetty等容器提供,將靜態(tài)資源映射從/改為/static/目錄,如原來訪問http://localhost/foo.css,現(xiàn)在http://localhost/static/foo.css -->  <!-- 不攔截靜態(tài)文件 -->  <servlet-mapping>    <servlet-name>default</servlet-name>    <url-pattern>/js/*</url-pattern>    <url-pattern>/css/*</url-pattern>    <url-pattern>/images/*</url-pattern>    <url-pattern>/fonts/*</url-pattern>  </servlet-mapping>  

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 杭州市| 璧山县| 宁化县| 石嘴山市| 延吉市| 九江市| 满洲里市| 马鞍山市| 南通市| 天峻县| 上犹县| 河源市| 秦皇岛市| 新河县| 湘潭县| 昌都县| 金沙县| 连山| 西城区| 汉源县| 南漳县| 五峰| 勐海县| 奉化市| 凌源市| 台山市| 灵武市| 怀来县| 吴忠市| 泸溪县| 正宁县| 搜索| 肇源县| 陈巴尔虎旗| 柳江县| 通山县| 泾阳县| 满城县| 绥中县| 榆社县| 安图县|