詳解springmvc攔截器攔截靜態(tài)資源
springmvc攔截器interceptors
springmvc攔截器能夠?qū)φ埱蟮馁Y源路徑進行攔截,極大的簡化了攔截器的書寫。但是,千萬千萬要注意一點:靜態(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定義一個Interceptor,直接定義在mvc:interceptors根下面的Interceptor將攔截所有的請求 <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下面的表示是對特定的請求才進行攔截的 --> <beans:bean class="com.myTree.interceptor.LoginInterceptor" > </beans:bean> </mvc:interceptor> </mvc:interceptors>
問題來了,在請求jsp頁面的時候,你的靜態(tài)資源的訪問仍然會被自定義攔截器攔截,這會導(dǎo)致程序運行的效率大大下降,還會不停的跳轉(zhuǎn)到攔截器的邏輯。主要原因是
<mvc:mapping path="/**" />
會對所有的請求資源進行攔截,雖然靜態(tài)資源已經(jīng)排除了,但還是會被攔截到。
如何解決這個bug呢?
主要有三種方式:
1、修改請求的url地址。
如果請求的url地址都是以*.do結(jié)尾,那么攔截器中的配置可以變?yōu)閿r截以do結(jié)尾的資源,靜態(tài)資源自然就不會被攔截到了;
2、在自定義攔截器中對資源進行判斷,如果滿足需要排除的資源,就進行放行。
<!-- 攔截器配置 --> <mvc:interceptors> <!-- session超時 --> <mvc:interceptor> <mvc:mapping path="/*/*"/> <bean class="com.myTree.interceptor.LoginInterceptor"> <property name="allowUrls"> <list> <!-- 如果請求中包含以下路徑,則不進行攔截 --> <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中的默認攔截器,不攔截靜態(tài)資源
在springmvc的Dispatcher中配置<mvc:default-servlet-handler />(一般Web應(yīng)用服務(wù)器默認的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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Mybatis中SQL節(jié)點的深入解析
這篇文章主要給大家介紹了關(guān)于Mybatis中SQL節(jié)點的深入解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-03-03Spring-Security對HTTP相應(yīng)頭的安全支持方式
這篇文章主要介紹了Spring-Security對HTTP相應(yīng)頭的安全支持方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10