- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经使用 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;
可用于获取Method
和 Class
与请求的 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/
我有一个关于 Java Servlet 的问题。 假设我在 servlet 网页“somePage”上。我想登录(使用另一个 servlet,“登录”servlet)。所以我点击“somePage”上
如何将变量数组从一个 servlet 传递到另一个 servlet? 最佳答案 如果您要将当前请求传递给另一个servlet,则只需将其设置为请求属性即可。 request.setAttribute(
什么可能导致此错误? Caused by: jakarta.servlet.UnavailableException: Servlet class org.restlet.ext.servle
我的maven依赖树是这样的 我想问我maven如何解决这个冲突,有两个servlet-api.jar?提前谢谢你。 最佳答案 如果您想从 Velocity 工具中删除 servlet-api,您可以
config ProcessReg ProcessReg text HelloWorld1 public class config implements Serv
您好,我有一个关于 servlet 调用另一个 servlet 的问题 我有一个名为 Relay 的主 servlet,它将负责控制其他 servlet 用户将点击并将转发到 Relay servle
在我的 REST API 项目中,我已将 /* 映射到 RESTServlet,并且需要在同一 WAR 中托管静态内容。我更愿意将 /static/* 映射到 WAS liberty 提供的默认 se
响应映射在 Servlet 中如何工作? 每个响应如何知道清除特定 HTML 或 Handlebars 上的输出? 最佳答案 有一个 ember-java带有 Jersey REST 服务 的 git
有一个 @WebServlet(urlPatterns = "/myServlet/") .如果用户转到 myapp/myServlet/other ,我仍然希望我的 servlet 能够捕获。也就是
我正在使用 Filter 在我的所有页面中插入反点击劫持 header - 这工作正常,除了 JBoss EAP 6.3 容器管理的登录页面,这是更重要的页面之一拥有它。 登录页面根本不调用过滤器,登
我正在尝试使用 RequestDispatcher 将数据从一个 servlet 传递到另一个 servlet。这是我的调度程序代码。 String address; address = "/Java
我刚刚开始使用 Servlet,并设法让一些 Servlet 充当单独的 URL,用于填充数据库以进行一些虚拟测试。某种形式: public class Populate_ServletName ex
我是否需要同时配置app.servlet.version 和 grails.servlet.version? 前者在application.properties中,后者在BuildConfig.gro
在Myeclipse中我创建了一个名为web1的Web项目,并添加了一个名为servlet1的servlet,web.xml如下: servlet1 servlet1
这个问题在这里已经有了答案: How to run a background task in a servlet based web application? (5 个回答) 6年前关闭。 是否可以在
在我的 ManagedBean 中,如果我将范围从 @RequestScoped 更改为 @ViewScoped,我将收到以下错误堆栈。我该如何解决这个问题?不过,当我运行应用程序时,我可以在页面中看
这个问题已经有答案了: How do I execute multiple servlets in sequence? (2 个回答) 已关闭 3 年前。 我已经构建了jdbc-Servlet的代码,
我一直在徒劳地尝试实现 tomcat 9 的 jakarta servlet,而不是以前的 javax.servlet 实现(因为我的理解是 jakarta 包是前进的方向)。问题是,当我将浏览器指向
我是 Spring 新手,正在尝试使用 Maven 部署和运行我的第一个 Spring Web 应用程序。有关更多详细信息,请参阅 here (我昨天发布的一个问题): 现在,我遇到的问题是:当我尝试
我正在尝试学习如何使用 JSP。我遇到了一些毫无意义的事情,至少在我看来是这样: 当我尝试运行时: response.getWriter().println(m.getDb().printAll())
我是一名优秀的程序员,十分优秀!