淺談SpringMVC的攔截器(Interceptor)和Servlet 的過(guò)濾器(Filter)的區(qū)別與聯(lián)系 及SpringMVC 的配置文件
1.過(guò)濾器:
依賴于servlet容器。在實(shí)現(xiàn)上基于函數(shù)回調(diào),可以對(duì)幾乎所有請(qǐng)求進(jìn)行過(guò)濾,但是缺點(diǎn)是一個(gè)過(guò)濾器實(shí)例只能在容器初始化時(shí)調(diào)用一次。使用過(guò)濾器的目的是用來(lái)做一些過(guò)濾操作,獲取我們想要獲取的數(shù)據(jù).
比如:在過(guò)濾器中修改字符編碼;在過(guò)濾器中修改 HttpServletRequest的一些參數(shù),包括:過(guò)濾低俗文字、危險(xiǎn)字符等
關(guān)于過(guò)濾器的一些用法可以參考我寫過(guò)的這些文章:
繼承HttpServletRequestWrapper以實(shí)現(xiàn)在Filter中修改HttpServletRequest的參數(shù):https://www.zifangsky.cn/677.html
在SpringMVC中使用過(guò)濾器(Filter)過(guò)濾容易引發(fā)XSS的危險(xiǎn)字符:https://www.zifangsky.cn/683.html
2.攔截器:
依賴于web框架,在SpringMVC中就是依賴于SpringMVC框架。在實(shí)現(xiàn)上基于Java的反射機(jī)制,屬于面向切面編程(AOP)的一種運(yùn)用。由于攔截器是基于web框架的調(diào)用.
因此可以使用spring的依賴注入(DI)進(jìn)行一些業(yè)務(wù)操作,同時(shí)一個(gè)攔截器實(shí)例在一個(gè)controller生命周期之內(nèi)可以多次調(diào)用。但是缺點(diǎn)是只能對(duì)controller請(qǐng)求進(jìn)行攔截,對(duì)其他的一些比如直接訪問靜態(tài)資源的請(qǐng)求則沒辦法進(jìn)行攔截處理。
關(guān)于攔截器的一些用法可以參考我寫過(guò)的這些文章:
SpringMVC中使用攔截器(interceptor)攔截CSRF攻擊(修):https://www.zifangsky.cn/671.html
SpringMVC中使用Interceptor+cookie實(shí)現(xiàn)在一定天數(shù)之內(nèi)自動(dòng)登錄:https://www.zifangsky.cn/700.html
3.執(zhí)行順序
過(guò)濾器的運(yùn)行是依賴于servlet容器的,跟springmvc等框架并沒有關(guān)系。并且多個(gè)過(guò)濾器的執(zhí)行順序跟web.xml文件中定義的先后關(guān)系有關(guān)。
攔截器的執(zhí)行順序跟在SpringMVC的配置文件中定義的先后順序有關(guān)。
SpringMVC的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans .......>
<mvc:default-servlet-handler />
<context:component-scan base-package="com.netease.mmc.demo.web">
<!-- enable controller advice -->
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<context:component-scan base-package="com.netease.mmc.demo.imdemo.web"/>
<context:annotation-config />
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.netease.mmc.demo.common.spring.interceptor.DDOSInterceptor">
<property name="close" value="false"/>
<property name="maxRequest" value="2000"/>
<property name="ttlSeconds" value="10"/>
</bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.netease.mmc.demo.common.spring.interceptor.WebContextHolderInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/api/**"/>
<bean class="com.netease.mmc.demo.web.interceptor.ValidateAppKeyInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<mvc:annotation-driven ignore-default-model-on-redirect="true">
<!-- message converters -->
<mvc:message-converters>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="objectMapper"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- view resolver -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager" ref="contentNegotiationManager"/>
<!-- api server has no page view resolvers, add viewResolvers config here if required -->
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
<property name="objectMapper" ref="objectMapper"/>
<property name="contentType" value="application/json;charset=UTF-8"/>
<property name="modelKeys">
<set>
<value>res</value>
<value>msg</value>
<value>errmsg</value>
</set>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller" ref="xstreamMarshaller"/>
<property name="modelKey" value="data"/>
<property name="contentType" value="application/xml"/>
</bean>
</list>
</property>
</bean>
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json;charset=UTF-8"/>
<entry key="xml" value="application/xml;charset=UTF-8"/>
<entry key="html" value="text/html;charset=UTF-8"/>
</map>
</property>
<property name="defaultContentType" value="application/json;charset=UTF-8"/>
</bean>
<bean id="objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="serializationInclusion" value="NON_NULL" />
</bean>
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="autodetectAnnotations" value="true" />
<property name="streamDriver">
<bean class="com.thoughtworks.xstream.io.xml.DomDriver" />
</property>
</bean>
<aop:aspectj-autoproxy/>
</beans>
到此這篇關(guān)于淺談SpringMVC的攔截器(Interceptor)和Servlet 的過(guò)濾器(Filter)的區(qū)別與聯(lián)系 及SpringMVC 的配置文件的文章就介紹到這了,更多相關(guān)SpringMVC攔截器 Servlet過(guò)濾器 SpringMVC配置文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決SpringBoot內(nèi)嵌Tomcat并發(fā)容量的問題
這篇文章主要介紹了解決SpringBoot內(nèi)嵌Tomcat并發(fā)容量的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
當(dāng)mybatis返回值遇見內(nèi)部類的問題
這篇文章主要介紹了當(dāng)mybatis返回值遇見內(nèi)部類的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Swagger異常定位紀(jì)實(shí)Swagger設(shè)計(jì)問題分析
這篇文章主要為大家介紹了Swagger異常定位紀(jì)實(shí)Swagger設(shè)計(jì)的問題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-02-02
java調(diào)用ffmpeg實(shí)現(xiàn)轉(zhuǎn)換視頻
這篇文章主要為大家詳細(xì)介紹了java調(diào)用ffmpeg實(shí)現(xiàn)轉(zhuǎn)換視頻功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Spring集成Druid連接池及監(jiān)控配置的全過(guò)程
java程序很大一部分要操作數(shù)據(jù)庫(kù),為了提高性能操作數(shù)據(jù)庫(kù)的時(shí)候,有不得不使用數(shù)據(jù)庫(kù)連接池,下面這篇文章主要給大家介紹了關(guān)于Spring集成Druid連接池及監(jiān)控配置的相關(guān)資料,需要的朋友可以參考下2021-09-09

