如何在 Java 中使用 Jersey 安全注释绕过 servlet 过滤器中的路径

How to bypass path in servlet filter with Jersey security annotations in Java

我已经使用 Jersey 实现了 REST 服务。为了提供更高的安全性,我在 REST 方法中添加了 jersey 安全注释(@PermitAll@DenyAll)。

下面是我的示例 REST 服务:

@GET
@Path("/getall")
@Produces(MediaType.APPLICATION_JSON)
@PermitAll
public String getChartSupportedData(@QueryParam("items") int result) {
    // my code goes here
}

但问题是我以前使用 javax.servlet.Filter 过滤器来验证 URI。

web.xml:

<filter>
    <filter-name>ApplicationFilter</filter-name>
    <filter-class>web.filter.ApplicationFilter</filter-class>
</filter>
<filter-mapping>
       <filter-name>ApplicationFilter</filter-name>
       <url-pattern>/rest/api/*</url-pattern>
       <dispatcher>REQUEST</dispatcher>
       <dispatcher>ASYNC</dispatcher>           
</filter-mapping>

根据访问一些 REST 服务,HttpServletRequest 应该包含一个有效的令牌(由应用程序生成)。

一些 REST 端点不需要令牌来访问服务。在那种情况下,我必须在过滤器实现中绕过它:

private static String[] bypassPaths = { "/data/getall" };

所以我的要求是这样的。

如果我们将某些 REST 端点声明为 @PermitAll,则该路径不应在过滤器中声明为旁路路径,这样任何人都可以在没有有效令牌的情况下访问它。

但问题是当请求进入服务器时,过滤器总是在过滤,如果它不在旁路数组中,即使我声明为 @PermitAll,请求也不会继续。

我想知道是否可以在同一个 Web 应用程序中组合这两个安全选项。

由于您正在执行身份验证 and/or 授权,因此我建议您使用名称绑定过滤器而不是 servlet 过滤器,这样您就可以轻松地将它们绑定到您需要的资源。

为了将过滤器绑定到您的 REST 端点,JAX-RS 提供元注释 @NameBinding 并且可以按如下方式使用:

@NameBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface Secured { }

@Secured 注释将用于装饰过滤器 class,它实现 ContainerRequestFilter,允许您处理请求。

ContainerRequestContext helps you to extract information from the HTTP request (for more details, have a look at the ContainerRequestContext API):

@Secured
@Provider
@Priority(Priorities.AUTHENTICATION)
public class SecurityFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        // Use the ContainerRequestContext to extract information from the HTTP request
        // Information such as the URI, headers and HTTP entity are available
    }
}

ContainerRequestFilter#filter() method is a good place to abort the request if the user is not authenticated/authorized. To do it, you can use ContainerRequestContext#abortWith() 还是抛出异常。

@Provider 注释标记了一个扩展接口的实现,在提供者扫描阶段,它应该可以被 JAX-RS 运行时发现。

要将过滤器绑定到您的端点方法或 classes,请使用上面创建的 @Secured 注释对其进行注释。对于and/or class注解的方法,将执行过滤器。

@Path("/")
public class MyEndpoint {

    @GET
    @Path("{id}")
    @Produces("application/json")
    public Response myUnsecuredMethod(@PathParam("id") Long id) {
        // This method is not annotated with @Secured
        // The security filter won't be executed before invoking this method
        ...
    }

    @DELETE
    @Secured
    @Path("{id}")
    @Produces("application/json")
    public Response mySecuredMethod(@PathParam("id") Long id) {
        // This method is annotated with @Secured
        // The security filter will be executed before invoking this method
        ...
    }
}

在上面的示例中,安全过滤器将仅针对 mySecuredMethod(Long) 执行,因为它被注释为 @Secured

您可以根据需要为 REST 端点设置任意数量的过滤器。为了确保过滤器的执行顺序,用@Priority注释它们。

强烈建议使用 Priorities class 中定义的值之一(将使用以下顺序):

如果您的过滤器未使用 @Priority 注释,则过滤器将以 USER 优先级执行。

您可以将此方法与 Jersey security mechanism 结合使用。

此外,您可以在 ContainerRequestFilter:

中注入 ResourceInfo
    @Context
    private ResourceInfo resourceInfo;

可用于获取与请求URL相匹配的Method and Class:

    Class<?> resourceClass = resourceInfo.getResourceClass();
    Method resourceMethod = resourceInfo.getResourceMethod();

并从中提取注释:

    Annotation[] annotations = resourceClass.getDeclaredAnnotations();
    PermitAll annotation = resourceMethod.getAnnotation(PermitAll.class);