我正在使用 Jersey + Jetty + Dropwizard + Hibernate 创建 Web 服务。
假设我有一个像这样的网络资源:
@Path("/")
@PermitAll
public class Resource {
@RolesAllowed("user")
@GET
public String get() { return "GET"; }
@RolesAllowed("admin")
@POST
public String post(String content) { return content; }
@Path("sub")
public SubResource getSubResource() {
return new SubResource();
}
}
我知道您可以使用HttpServletRequest.isUserInRole
检查用户的角色。
问题是,我如何开始分配角色? Jersey 如何知道 isUserInRole
方法返回什么内容,或者知道如何根据角色过滤无法访问特定资源的人员?
我没有 web.xml
或 webdefault.xml
,因此定义应该在其他地方完成。
最佳答案
你必须提供一个支持@RolesAllowed("admin")注解的提供者类,然后你必须在你的应用程序中注册提供者类。通常它是在web.xml中完成的,但由于你没有使用它,你必须自己提供它。相关的例子可以找到here
关于java - 在 Jersey Web 服务中设置角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39488268/