[英]How to preprocess all requests in Spring Web application?
I have a RESTful
Spring application, which can process various request with various controllers and various @RequestMapping
-s. 我有一个RESTful
Spring应用程序,该应用程序可以使用各种控制器和各种@RequestMapping
-s处理各种请求。
Now I wish each request contain additionally a user/password pair, user as user GET
parameter ?user=username
and password in password POST
parameter. 现在,我希望每个请求还包含一个用户/密码对,用户作为用户GET
参数?user=username
以及密码POST
参数中的密码。
Is it possible to "preprocess" all requests before they go to controllers and check credentials and possible return authentication error? 是否可以在处理所有请求之前先对其进行“预处理”,然后再检查凭据并可能返回身份验证错误?
Is this a mechanism, which is caller "filters" or not? 这是调用者“过滤器”与否的机制吗?
There are couple of ways for intercepting the requests before they land into controllers. 有两种方法可以在请求进入控制器之前拦截请求。
- Using Servlet Filters: 使用Servlet过滤器:
@WebFilter(urlPatterns = "path-to-intercept", filterName = "your-filter-name")
public class MyRequestFilter implements Filter {
init(...);
doFilter(...);
destroy();
}
- Using Spring HandlerIntecptors: 使用Spring HandlerIntecptors:
public class MyRequestInterceptor extends HandlerInterceptorAdapter{
preHandle(...); //called before the request lands to the controller.
postHandle(...); //called after the controller method finishes execution.
afterCompletion(...); //called before the response is sent.
}
And in your configuration: 并在您的配置中:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/yourInterceptingPath/" />
<bean class="path-to-your-Interceptor.MyRequestInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.