Servlet 过滤器和 Jersey 过滤器有什么区别? [英] What is the difference between a Servlet filter and a Jersey filter?

查看:44
本文介绍了Servlet 过滤器和 Jersey 过滤器有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了很多教程,但我不明白 Servlet 过滤器和 Jersey 过滤器之间的区别.谁能给我解释一下这些区别?

I read a lot of tutorials but I don't understand what the differences are between Servlet filters and Jersey filters. Can anyone explain the differences to me?

推荐答案

在 Servlet 容器中,有 Servlet 和 Servlet 过滤器.通常,Servlet 处理请求,而 Servlet Filter 处理请求的 pre an post 处理.所以请求流看起来像

In a Servlet container, you have Servlets and you have Servlet Filters. Generally, the Servlets handle processing of the request, and the Servlet Filter handles pre an post processing of the request. So a request flow looks like

Request --> Filter --> Servlet --> Filter --> Response

Jersey 应用程序实现为 Servlet1.所以在上述流程中,只需替换Servlet"即可.与泽西岛.

The Jersey application is implemented as a Servlet1. So in the above flow, just replace "Servlet" with Jersey.

Request --> Filter --> Jersey-Servlet --> Filter --> Response

Jersey 也有自己的过滤器,即 ContainerRequestFilterContainerResponseFilter.它们的用途与 Servlet 过滤器相同,只是在 Jersey 应用程序的上下文中;它们用于预处理和后处理.以下流程在 Jersey 请求中.

Jersey also has its own filters, namely ContainerRequestFilter and ContainerResponseFilter. They serve the same same purpose as the Servlet Filter, just in the context of a Jersey application; they are for pre-processing and post-processing. The below flow is inside of a Jersey request.

Req --> ContainerRequestFilter --> Resource --> ContainerResponseFilter --> Res

因此,这些过滤器具有相同的用途,即对请求进行预处理和后处理.主要区别在于它们连接的级别.Servlet 过滤器绑定在 servlet 级别,而 Jersey 过滤器绑定在 Jersey 级别.

So these filters serve the same purpose, pre and post processing of the request. The major difference is the level in which they are connected. Servlet filters are tied at the servlet level, while Jersey filters are tied at the Jersey level.

这取决于您希望何时调用它以及您需要访问哪些信息.以安全为例.在考虑保护您的应用程序时,您可能希望您的安全门尽可能远离数据.因此,您可以使用 Servlet 过滤器来实现您的安全性.但是您需要的信息只能在 Jersey 应用程序中获得.然后你需要使用 Jersey 过滤器;例如,您需要知道调用了哪个资源方法.您只能从 Jersey 过滤器中的 ResourceInfo 获取此信息

Well it depends on when you want it called and what information you need access to. Take security for example. When thinking about securing your application, you might want your security gates as far from the data as possible. So you might implement your security using Servlet filters. But you need information that can only be obtained inside the Jersey application. Then you would need to use the Jersey filters; for example, you need to know which resource method is called. You could only get this information from the ResourceInfo inside a Jersey filter

class MyResource {
    @RolesAllowed("ADMIN")
    public Response get() {}
}

class AuthorizationFilter implements ContainerRequestFilter {
    @Context
    private ResourceInfo resourceInfo;

    @Override
    public void filter(ContainerRequestContext request) {
        Method method = resourceInfo.getResourceMethod();
        RolesAllowed rolesAllowed = method.getAnnotation(RolesAllowed.class);
    }
}

您无法在 Servlet 过滤器中执行上述操作.此信息只能从 Jersey 应用程序的上下文中访问.也许您可能希望在 Servlet 过滤器中处理 身份验证,并在身份验证后将结果存储在 HttpServletRequest 属性中.然后在 authorization 中,您可以在 Jersey 级别处理它,如上所示.您可以将 HttpServletRequest 注入 Jersey 过滤器,并从那里获取属性.

You couldn't do the above in a Servlet filter. This information is only accessible from the context of a Jersey application. Maybe you might want to handle the authentication in a Servlet Filter, and after authentication, store the results in the HttpServletRequest attributes. Then in the authorization, you could take care of it at the Jersey level, as shown above. You could inject the HttpServletRequest into the Jersey filter, and get the attributes from there.

这只是一个例子.有很多用例.您只需要确定最适合您的应用程序的过滤器类型即可.大多数情况下,您可以使用 Jersey 过滤器,但有时您需要在请求中尽早调用过滤器,在这种情况下,您可能需要使用 Servlet 过滤器.在其他情况下,您需要访问只能在 Jersey 应用程序中获得的信息.为此,您应该使用 Jersey 过滤器.

This is just an example. There are so many use cases. You just need to decide what would be the best for your application, as far as which filter type to implement. Most of the time you could get away with using a Jersey filter, but sometimes you need to filter to be called as early in the request as possible, in which case you might want to use the Servlet Filter. And in other cases, you need access to information that can only be obtained inside a Jersey application. For this, you should use a Jersey filter.

<子>1.Jersey 应用程序也可以配置为作为 Servlet 过滤器而不是 Servlet 运行.我相信他们之所以提供此选项,是因为某些 Jersey 功能需要一些仅存在于过滤器中的功能.

这篇关于Servlet 过滤器和 Jersey 过滤器有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆