在 Jersey 1.18.1 请求过滤器中获取资源注释 [英] Getting resource annotations in Jersey 1.18.1 request filter

查看:23
本文介绍了在 Jersey 1.18.1 请求过滤器中获取资源注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个用户授权模块,该模块将使用(新)注释应用于资源方法.
为此,我创建了一个 Jersey(请求)过滤器,我需要在其中获取注释以允许/禁止资源操作.

I'm implementing a user authorization module that will be applied on a resource method using a (new) annotation.
In order to do so, I created a Jersey (request) filter in which I need to get the annotation in order to allow / disallow the resource operation.

我使用的是 Dropwizard 0.7.1 和 Jersey 1.18.1

I'm using Dropwizard 0.7.1 with Jersey 1.18.1

资源类:

@Path("/v1/users/registration")
@Produces(MediaType.APPLICATION_JSON)
@Api(value = "/users/registration")
public class UserRegistrationResource {
    @POST
    @AuthorizedFor(Realm.SOCIAL) // The custom annotation class
    public SessionModel register(
            @Valid
            @ApiParam(value = "New user to be registered", required = true)
            NewUser user) throws Exception {

        // Some logic
    ...
    }
}

过滤器类:

@Provider
public class AuthorizationFilter implements ContainerRequestFilter {

    @Context
    AbstractMethod method;

    @Override
    public ContainerRequest filter(ContainerRequest request) {

    // At this point, the method parameter is null :(

    Realm realm = null;
        User user = Context.get(Session.class).getUser();
        for (Annotation annotation : method.getAnnotations()) {
            if (AuthorizedFor.class == annotation.annotationType()) {
                realm = ((AuthorizedFor) annotation).value();
            }
        }
        if (realm != null) {
            for (Realm userRealm : user.getRole().getAllowedRealms()) {
                if (userRealm.equals(realm)) {
                    return request;
                }
            }
        }
        throw new ApiException(ResponseCode.UNAUTHORIZED);
    }
}

提供者类:

@Provider
public class AbstractMethodProvider extends AbstractHttpContextInjectable<AbstractMethod> implements InjectableProvider<Context, Parameter> {

    @Override
    public Injectable<AbstractMethod> getInjectable(ComponentContext ic, Context context, Parameter parameter) {
        if (parameter.getParameterType() == AbstractMethod.class) {
            return this;
        }
        return null;
    }

    @Override
    public ComponentScope getScope() {
        return ComponentScope.PerRequest;
    }

    @Override
    public AbstractMethod getValue(HttpContext context) {
        return context.getUriInfo().getMatchedMethod();
    }
}

过滤器和提供程序初始化代码:

The filter and provider initalization code:

environment.jersey().getResourceConfig().getContainerRequestFilters().add(new AuthorizationFilter());
environment.jersey().register(new AbstractMethodProvider());

我还尝试在过滤器中注入 HttpContext.它不为空,但 getUriInfo().getMatchedMethod() 为空.
有没有更好的方法在 Jersey 请求过滤器中获取资源方法注释?

I've also tried to inject HttpContext in the filter. It wasn't null but getUriInfo().getMatchedMethod() was null.
Is there a better way to get resource method annotations in a Jersey request filter?

推荐答案

您可以实现一个 ResourceFilterFactory 来获取 AbstractMethod.

You can implement a ResourceFilterFactory to get the AbstractMethod.

public class AuthorizationFilterFactory implements ResourceFilterFactory {

    @Override
    public List<ResourceFilter> create(AbstractMethod abstractMethod) {
        return Arrays.asList(this.createAuthorizationFilter(abstractMethod));
    }

    private ResourceFilter createAuthorizationFilter(final AbstractMethod abstractMethod) {
        return new ResourceFilter() {
            @Override
            public ContainerRequestFilter getRequestFilter() {
                return new AuthorizationFilter(abstractMethod);
            }

            @Override
            public ContainerResponseFilter getResponseFilter() {
                return null;
            }
        };
    }
}

因此,您的 AuthorizationFilter 将如下所示:

So, your AuthorizationFilter will look like:

@Provider
public class AuthorizationFilter implements ContainerRequestFilter {

    private final AbstractMethod method;

    public AuthorizationFilter(AbstractMethod method) {
        this.method = method;
    }

    @Override
    public ContainerRequest filter(ContainerRequest request) {
        Realm realm = null;
        User user = Context.get(Session.class).getUser();
        for (Annotation annotation : method.getAnnotations()) {
            if (AuthorizedFor.class == annotation.annotationType()) {
                realm = ((AuthorizedFor) annotation).value();
            }
        }
        if (realm != null) {
            for (Realm userRealm : user.getRole().getAllowedRealms()) {
                if (userRealm.equals(realm)) {
                    return request;
                }
            }
        }
        throw new ApiException(ResponseCode.UNAUTHORIZED);
    }
}

要注册您的工厂:

environment.jersey().getResourceConfig().getResourceFilterFactories().add(new AuthorizationFilterFactory());

这篇关于在 Jersey 1.18.1 请求过滤器中获取资源注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆