繁体   English   中英

SecurityContext不适用于@RolesAllowed

[英]SecurityContext doesn't work with @RolesAllowed

我目前正在Tomcat 7中使用Jersey 2.5.1创建一个后端服务器。为了安全性,我使用了@RolesAllowed@PermitAll等注释,我创建了自定义的ContainerRequestFilterSecurityContext

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

我的ContainerRequestFilter实现:

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

我的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();
    }
}

我的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 - 禁止。

提前谢谢你们

确保在MyApplication注册了AuthorizationFilter (请参阅在Jersey 2中注册资源和提供程序 )或使用@Provider注释(以使程序包扫描可以发现它)。

为了使用安全注释(包javax.annotation.security )来限制对资源的访问,您需要注册RolesAllowedDynamicFeature

编辑1

您的AuthorizationFilter也必须使用@PreMatching进行注释,这意味着在匹配阶段(uri - > resource)之前调用过滤器。 否则, RolesAllowedDynamicFeature注册的过滤器(在此阶段调用)将不会看到自定义的SecurityContext

编辑2

Jersey用户指南 - 授权 - 保护资源

在实际应用程序中,定义自己的ResourceConfig意味着每次添加新资源(类)时都必须对其进行编辑。

避免该问题的一个好方法是将<init-param>RolesAllowedDynamicFeature类注册到web.xml<servlet> ,如下所示:

<servlet>
    <servlet-name>your_servelet_name</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature</param-value>
    </init-param>

如果您这样做,那么您可以将其放入以动态注册特定包中的所有资源:

    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.your-domain.your-packages-1,com.your-domain.your-packages-2</param-value>
    </init-param>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM