gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:03:07 25 4
gpt4 key购买 nike

我已经使用 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 应该包含一个有效的 token (由应用程序生成)。

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

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

所以我的要求是这样的。

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

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

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

最佳答案

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

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

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

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

ContainerRequestContext帮助您从 HTTP 请求中提取信息(有关更多详细信息,请查看 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()如果用户未通过身份验证/授权,方法是中止请求的好地方。为此,您可以使用 ContainerRequestContext#abortWith()或抛出异常。

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

要将过滤器绑定(bind)到您的端点方法或类,请使用上面创建的 @Secured 注释对其进行注释。对于被注释的方法和/或类,过滤器将被执行。

@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 中定义的值之一类(将使用以下顺序):

如果您的过滤器未使用 @Priority 进行注释,过滤器将使用 USER 执行优先事项。

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

另外,你可以注入(inject)ResourceInfo在你的ContainerRequestFilter :

    @Context
private ResourceInfo resourceInfo;

可用于获取MethodClass与请求的 URL 匹配:

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

并从中提取注释:

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

关于java - 如何使用 Java 中的 Jersey 安全注释绕过 servlet 过滤器中的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33649337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com