简体   繁体   English

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

[英]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. 有两种方法可以在请求进入控制器之前拦截请求。

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

相关问题 如何将Spring应用程序转换为Spring Web应用程序? - How to convert a Spring Application to a Spring Web Application? Spring Security如何在跨Web应用程序请求的线程中管理SecurityContext? - How does Spring Security manage SecurityContext in a thread across web application requests? 如何在更多HTTP请求中在Spring MVC Web应用程序中缓存数据? - How to cache data in Spring MVC web application within more HTTP requests? 如何配置Spring Context作为独立Java应用程序中所有请求的公共上下文? - How to Configure Spring Context to serve as a common context across all requests in a standalone java application? 如何从Spring应用程序每秒限制3个URL(包括所有3个)的10个请求 - How to limit 10 requests per second for 3 urls(including all 3) from Spring application 使用 spring 重新映射 web 应用程序 (tomcat) 中的图像请求以获得绝对路径 - Remap image requests in a web application (tomcat) with spring for an absolute path 如何拦截Spring REST控制器中的所有请求? - how to intercept all requests in spring REST controllers? 如何使用 Spring 安全性允许所有和任何请求? - How to allow all and any requests with Spring Security? 防止 Spring Boot 应用程序关闭,直到所有当前请求完成 - Prevent Spring Boot application closing until all current requests are finished Spring 引导应用程序中所有请求的超时异常 - Time Out Exception for all requests in Spring Boot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM