SecurityContext 不适用于 @RolesAllowed

2024-01-22

我目前正在 Tomcat 7 中使用 Jersey 2.5.1 创建后端服务器。为了安全起见,我使用@RolesAllowed, @PermitAll等注释,我已经创建了我的自定义ContainerRequestFilter and SecurityContext.

我的问题是当我的@RolesAllowed请求带注释的资源它总是拒绝许可,即使我强迫我的isUserInRole(role)返回方法true。然而,我的filter方法被调用。你有什么建议吗?我将在下面粘贴一些相关代码。

My ContainerRequestFilter执行:

public class AuthorizationFilter implements ContainerRequestFilter
{
    @Override
    public void filter(ContainerRequestContext request) throws IOException
    {
        request.setSecurityContext(new Authorizer());
    }
}

My 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;
    }

}

我的资源:

@Path("/secure")
public class TestSecureResource {

    @GET
    @PermitAll
    @Path("/nonsec_test/{text}")
    public Response nonSecureTest(
            @PathParam("text") String text){

        return Response.status(200).entity(text).build();
    }

    @GET
    @RolesAllowed("admin")
    @Path("/sec_test/{text}")
    public Response secureTest(
            @PathParam("text") String text){

        return Response.status(200).entity(text).build();
    }
}

My ResourceConfig:

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

    public MyApplication() {
        super(TestSecureResource.class);
        register(RolesAllowedDynamicFeature.class);
        register(AuthorizationFilter.class);
    }
}

我的相关部分web.xml:

<servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>pkg.backend</param-value>
        </init-param>

        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>pkg.backend.MyApplication</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>

在这种具体情况下,我可以访问secureTest总是被拒绝。澄清事情;我收到 HTTP 状态代码 403 - 禁止。

提前谢谢你们


确保你有你的AuthorizationFilter要么注册在您的MyApplication (see 在 Jersey 2 中注册资源和提供者 http://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/) 或注释为@提供者 https://docs.oracle.com/javaee/7/api/javax/ws/rs/ext/Providers.html(使其可以通过包扫描发现)。

为了使用安全注释(包javax.annotation.security)要限制对您需要注册的资源的访问允许的角色动态功能 https://jersey.github.io/apidocs/latest/jersey/org/glassfish/jersey/server/filter/RolesAllowedDynamicFeature.html.

EDIT 1

Your AuthorizationFilter还必须注释为@PreMatching这意味着过滤器在匹配阶段(uri -> 资源)之前被调用。否则过滤器注册RolesAllowedDynamicFeature(在此阶段调用)将看不到自定义SecurityContext.

EDIT 2

泽西岛用户指南 -授权——保护资源 https://jersey.github.io/documentation/latest/security.html#d0e12428

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SecurityContext 不适用于 @RolesAllowed 的相关文章

随机推荐