更新時間:2021-12-09 12:28:42 來源:動力節(jié)點 瀏覽1826次
在本教程中,我們將了解如何使用 SpringMVC 創(chuàng)建 Web 過濾器。
Spring MVC 會將任何擴展HttpFilter 的bean 注冊 為 web 過濾器。當我們以這種方式創(chuàng)建過濾器時,默認 URL 模式變?yōu)?/*。
@Slf4j
@Component
public class DateLoggingFilter extends HttpFilter {
@Override
protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
log.info("New request at date: {}", new Date());
chain.doFilter(request, response);
}
}
在這里,我們將DateLoggingFilter聲明 為 Spring bean。因此,Spring MVC 會將此 bean 注冊為 Web 過濾器。
訂購
通常,過濾器的順序沒有區(qū)別。但是如果一個過濾器的處理依賴于另一個過濾器,我們必須修改它們在過濾器鏈中的順序。有兩種方法可以實現(xiàn)這一點。
首先,我們可以使用@Order注釋對過濾器進行排序:
@Component
@Order(Ordered.LOWEST_PRECEDENCE - 1)
public class DateLoggingFilter extends HttpFilter {
// Implementation details...
}
在這里,我們有先前定義的 DateLoggingFilter,我們在@Order注釋中將其順序設(shè)置為 Ordered.LOWEST_PRECEDENCE - 1 。
或者,我們可以實現(xiàn)Ordered接口:
@Slf4j
@Component
public class TimeLoggingFilter extends HttpFilter implements Ordered {
@Override
protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
log.info("New request at time: {}", new Time(System.currentTimeMillis()));
chain.doFilter(request, response);
}
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE - 99;
}
}
在這里,TimeLoggingFilter的順序為 Ordered.LOWEST_PRECEDENCE - 99。
現(xiàn)在我們有兩個不同順序的過濾器,我們將調(diào)查它們的調(diào)用順序。
首先,Ordered.LOWEST_PRECEDENCE持有Integer.MAX_VALUE的值,而 Ordered.HIGHEST_PRECEDENCE是Integer.MIN_VALUE。因此,當訂單號減少時,優(yōu)先級會增加。
此外,具有更高優(yōu)先級的過濾器將在過濾器鏈中運行得更早。
因此,TimeLoggingFilter 具有更高的優(yōu)先級,并且會在DateLoggingFilter之前運行。
當我們使用 Spring bean 創(chuàng)建 web 過濾器時,它會獲取默認的 URL 模式,我們對其他注冊屬性沒有太多控制。 FilterRegistrationBean 允許我們定義 Web 過濾器的不同注冊屬性。
@Configuration
public class FilterConfiguration {
private final DateLoggingFilter dateLoggingFilter;
@Autowired
public FilterConfiguration(DateLoggingFilter dateLoggingFilter) {
this.dateLoggingFilter = dateLoggingFilter;
}
@Bean
public FilterRegistrationBean<DateLoggingFilter> dateLoggingFilterRegistration() {
FilterRegistrationBean<DateLoggingFilter> filterRegistrationBean = new FilterRegistrationBean<>();
filterRegistrationBean.setFilter(dateLoggingFilter);
filterRegistrationBean.setUrlPatterns(Collections.singletonList("/*"));
filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST);
filterRegistrationBean.setOrder(Ordered.LOWEST_PRECEDENCE - 1);
return filterRegistrationBean;
}
}
在這里,我們使用DateLoggingFilter bean,但我們通過FilterRegistrationBean bean顯式定義過濾器注冊 。除了 URL 模式之外,我們還設(shè)置了過濾器的調(diào)度程序類型和順序。
使用 Servlet 3.0 規(guī)范,我們可以使用 @WebFilter注釋等。Spring MVC 特別支持掃描用這些注釋注釋的類。
我們將首先使用@WebFilter創(chuàng)建一個過濾器:
@Slf4j
@WebFilter(urlPatterns = "/*", dispatcherTypes = {DispatcherType.REQUEST})
public class MethodLoggingFilter extends HttpFilter {
@Override
protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
log.info("New request with {}", request.getMethod());
chain.doFilter(request, response);
}
}
請注意,我們在注釋中指定了 URL 模式和調(diào)度程序類型。
創(chuàng)建過濾器后,我們將使用@ServletComponentScan使 Spring 掃描這些類:
@Configuration
@ServletComponentScan
public class FilterConfiguration {
// Bean definitions
}
當 Web 應(yīng)用程序啟動時,我們的MethodLoggingFilter 將在過濾器鏈中處于活動狀態(tài)。
在本教程中,我們研究了如何使用 Spring MVC 創(chuàng)建 Web 過濾器。如果大家想了解更相關(guān)知識,可以關(guān)注一下動力節(jié)點的Java在線學習,里面還有更豐富的知識等著大家去學習,希望對大家能夠有所幫助。