有一種方法(在Spring中)定義必須針對所有請求調用的方法,類似於@modelAttribute ???spring - 所有請求的方法
我想定義進行檢查的方法,並把在會話對象對於每個請求,方法是相同的
在該方法中,我將收到「主要」(空,認證之前和使用用戶名... ecc之後)
有一種方法(在Spring中)定義必須針對所有請求調用的方法,類似於@modelAttribute ???spring - 所有請求的方法
我想定義進行檢查的方法,並把在會話對象對於每個請求,方法是相同的
在該方法中,我將收到「主要」(空,認證之前和使用用戶名... ecc之後)
一種方法是使用org.springframework.web.servlet.handler.HandlerInterceptorAdapter。
看看這篇文章:
只需創建一個擴展它的類,使它成爲一個組成部分。然後,您可以將攔截器映射到applicatioContext.xml中的特定url patterm。
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*.svc"/>
<bean class="com.netsoft.skydive.controllers.SecuredApiEndpoint"></bean>
</mvc:interceptor>
</mvc:interceptors>
正如在另一個答案中提到的 - 只需使用HandlerInterceptor
並繼承例如, HandlerInterceptorAdapter
。最方便的方法去可能是使用Spring的Java配置如下面的代碼示例:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="my.base.package")
public class MyWebApplicationConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new HandlerInterceptorAdapter() {
@Override
public boolean preHandle(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse, final Object o) throws Exception {
// Do stuff with your interceptor
return true;
}
}).addPathPatterns("/**");
}
}
有關此更多信息,檢查出優秀JavaDoc。