简体   繁体   English

跳过球衣中特定资源的请求过滤器

[英]Skip request filter for specific resource in jersey

Iam working on a jersey project and Iam using token authentication and am using ContainerRequestFilter for filtering all the request and checking whether it has a token with it, but the requests includes Login and registration request, but we need to skip these request.. How i can skip the filtering for login and registration requests? Iam在球衣项目上工作,Iam使用令牌身份验证,并在使用ContainerRequestFilter过滤所有请求并检查它是否带有令牌,但是请求包括登录和注册请求,但是我们需要跳过这些请求。可以跳过对登录和注册请求的过滤? Is there any mechanism in jersey for achieving this? 球衣中是否有任何机制可以实现这一目标?

thank you 谢谢

As far as I know, there is no facility fo such a behavior using a raw deployment descriptor ( web.xml ). 据我所知,没有使用原始部署描述符( web.xml )来实现这种行为的工具。

However, if that was a custom filter, you can get it skip the excluded paths using a simple check on request url in your doFilter() method. 但是,如果这是一个自定义过滤器,则可以使用doFilter()方法中的简单检查请求url使其跳过排除的路径 But since you are using a third party filter, that won't be the way to go with but still can have this functionality achieved: 但是,由于您使用的是第三方过滤器,因此这不是首选,但仍可以实现以下功能:

  1. Change your third party filter ( ContainerRequestFilter ) mapping to another path rather than a wildcard one: 将您的第三方过滤器( ContainerRequestFilter )映射更改为另一路径,而不是通配符:

     <filter-mapping> <filter-name>containerRequestFilter</filter-name> <url-pattern>/tokenizedpaths/*</url-pattern> </filter-mapping> 
  2. Declare a new filter (you will see in a moment what it looks like), that will be mapped to a wildcard path to filter all requests and be delegated to dispatch requests to your containerRequestFilter only if the request path does not match the excluded path(s) (I've choosen register as a sample): 声明一个新的过滤器(稍后您会看到它的外观),它将被映射到通配符路径以过滤所有请求,并仅在请求路径与排除的路径不匹配时才委托将其分派给您的containerRequestFilter ( s)(我选择注册为样本):

     <filter> <filter-name>filteringFilter</filter-name> <filter-class>com.sample.FilteringServletFilter</filter-class> <init-param> <param-name>excludedPaths</param-name> <param-value>/register</param-value> </init-param> </filter> 
  3. The FilteringServletFilter will look like somthing like the following: FilteringServletFilter看起来如下所示:

     public class FilteringServletFilter implements Filter { private List<String> excludedPaths = new ArrayList<String>(); public void init(FilterConfig config) throws ServletException { // You can declare a comma separated list to hold your excluded paths this.excludedPaths = Arrays.asList(config.getInitParameter("excludedPaths").split(",")); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String path = ((HttpServletRequest) request).getRequestURI(); // If the url is one of excluded paths, then just continue with next filter if (this.excludedPaths.contains(path)) { chain.doFilter(request, response); return; } // Otherwilse, forward the request to the needed filter else { request.getRequestDispatcher("/tokenizedpaths" + path).forward(request, response); } } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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