繁体   English   中英

如何在Spring Web应用程序中预处理所有请求?

[英]How to preprocess all requests in Spring Web application?

我有一个RESTful Spring应用程序,该应用程序可以使用各种控制器和各种@RequestMapping -s处理各种请求。

现在,我希望每个请求还包含一个用户/密码对,用户作为用户GET参数?user=username以及密码POST参数中的密码。

是否可以在处理所有请求之前先对其进行“预处理”,然后再检查凭据并可能返回身份验证错误?

这是调用者“过滤器”与否的机制吗?

有两种方法可以在请求进入控制器之前拦截请求。

  1. 使用Servlet过滤器:
    @WebFilter(urlPatterns = "path-to-intercept", filterName = "your-filter-name")
    public class MyRequestFilter implements Filter {
        init(...);
        doFilter(...);
        destroy(); 
    }
  1. 使用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.
    }

并在您的配置中:

    <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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM