java - 使用角色注释进行保护不起作用( Jersey )

标签 java web-services rest jersey jax-rs

我是新来的,尽管我之前已经在这里找到了许多问题的答案。

现在我正在寻求这方面的帮助:我的小 REST API 上有这个小示例资源:

@Path("/greeting")
@PermitAll
public class HelloResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("all")
    public String sayHelloToAll() {
        return "Hello, everybody!";
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @RolesAllowed("admin")
    @Path("admin")
    public String sayHelloToAdmin() {
        return "Hello, admin!";
    }
}

为了过滤角色,我有这样的 SecurityContext 实现:

public class Authorizer implements SecurityContext {

    @Override
    public String getAuthenticationScheme() {
        return null;
    }

    @Override
    public Principal getUserPrincipal() {
        return null;
    }

    @Override
    public boolean isSecure() {
        return false;
    }

    @Override
    public boolean isUserInRole(String role) {
        return true;
    }
}

ContainerRequestFilter 的实现:

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

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        requestContext.setSecurityContext(new Authorizer());
    }
}

这是我的应用程序类:

@ApplicationPath("/")
public class Application extends ResourceConfig {

    public Application() {
        super(HelloResource.class);
        register(AuthorizationFilter.class);
        register(RolesAllowedDynamicFeature.class);        
    }
}

有了这一切,当我请求 URIgreeting/all 时,一切正常,字符串“Hello, everything!”显示。但是,当我请求 URIgreeting/admin 时(当具有管理员角色的用户请求它时应该调用它),即使我的 isUserInRole 方法始终返回 true,也永远不会调用它。事实上,我的过滤器方法总是被调用,但我的 isUserInRole 方法从未被调用。

我遵循了很多建议:

SecurityContext doesn't work with @RolesAllowed

Authorization with RolesAllowedDynamicFeature and Jersey

How to access Jersey resource secured by @RolesAllowed

Best practice for REST token-based authentication with JAX-RS and Jersey

但它似乎对任何东西都不起作用。

有人可以帮我吗?我不知道我是否缺少一些东西

提前谢谢大家。

编辑:当我请求 URI 问候/管理时,我顺便得到 403 Forbiden(我忘了说)

最佳答案

查看 RoleAllowedRequestFilter 的源代码。当用户通过身份验证时,预计会有一个关联的 Principal。过滤器检查它 here

if (rolesAllowed.length > 0 && !isAuthenticated(requestContext)) {
    throw new ForbiddenException(LocalizationMessages.USER_NOT_AUTHORIZED());
}
...
private static boolean isAuthenticated(final ContainerRequestContext requestContext) {
    return requestContext.getSecurityContext().getUserPrincipal() != null;
}

所以你需要在SecurityContextgetUserPrincipal中返回一个Principal

@Override
public Principal getUserPrincipal() {
    return new Principal() {
        @Override
        public String getName() {
            return "Some Name";
        }
    };
}

关于java - 使用角色注释进行保护不起作用( Jersey ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34497071/

相关文章:

java - K-ary树递归打印方法到非递归

java - 使用回收器 View android studio访问其他 View

java - Resttemplate 404 未找到

java - "Error type 3 Error: Activity class {} does not exist",启动 Activity 时错误类型 3 Activity 类不存在

c++ - 在 C/C++ 中实现 JSON RESTful 服务的方法

java - 如何在面向 JavaEE 开发人员的 Eclipse 4.4 版中禁用 APT Web 服务验证

python - 在 HUG 上更改端口

rest - 如何使用 oauth 保护 Restful Web 服务?

rest - 根据参数呈现输出格式(HTML、JSON、XML)?

java - 如何获取 Apache Ignite 中给定缓存的配置?