@RolesAllowed在Jersey中不使用自定义SecurityContext?

Tin*_*Sky 3 java rest jersey

我有一个简单的球衣2.4资源:

@RolesAllowed("admin")
public List<Folder> list(){}
Run Code Online (Sandbox Code Playgroud)

我还有一个ContainerRequestFilter,它设置自定义securitycontext:

public void filter(ContainerRequestContext requestContext) throws IOException {
requestContext.setSecurityContext(new MySecurityContext(...));
}
Run Code Online (Sandbox Code Playgroud)

在list()函数中,我确实得到了正确的securitycontext:MySecurityContext.并且调用"securityContext.isUserInRole("admin")"有效.

但注释@RolesAllowed似乎没有做任何事情,永远不会调用MySecurityContext的函数isUserInRole.

我需要做一些特别的事情来让@RolesAllowed工作吗?

Tin*_*Sky 8

找到了 :-)

@RolesAllowed("admin")不是@RolesAllowed("{admin}")

而最重要的一个:

寄存器(RolesAllowedDynamicFeature.class);

  • 谢谢!我从Jersey 1.x迁移到2.x时错过了添加*RolesAllowedDynamicFeature*,并且花了很多时间来寻找这个.对于任何想知道`register()`调用在哪里的人,它都在你的`javax.ws.rs.Application`实现中,通过使用它来传递给ServletContainer(例如`org.glassfish.jersey.servlet.ServletContainer`).在`web.xml`里面``init-param>`. (3认同)