Context注入无效在Jersey

Context注入无效在Jersey

本文介绍了@Context注入无效在Jersey ContainerRequestFilter(Dropwizard)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Context注入适用于类,但不能使它与Objects一起使用。

@Context injection works with classes but not able to make it work with Objects.

ContainerRequestFilter中的httpServletRequest产生空指针。

httpServletRequest in ContainerRequestFilter produces null pointer.

Dropwizard版本: - 1.1.0

Dropwizard Version:- 1.1.0

ContainerRequestFilter

ContainerRequestFilter


public class ApplicationIPAuthFilter implements ContainerRequestFilter {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationIPAuthFilter.class);
    private HerculesRestAccessor restAccessor;
    private String applicationName;

    @Context
    private HttpServletRequest httpServletRequest;

    public ApplicationIPAuthFilter(){
    }

    public ApplicationIPAuthFilter(HerculesRestAccessor accessor, String applicationName) {
        restAccessor = accessor;
        this.applicationName = applicationName;
    }

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        // need remote Ip
        String remoteIp = this.httpServletRequest.getRemoteAddr();
        ....
    }
}

DynamicFeature

DynamicFeature



@Provider
public class ApplicationIPAuthFeature implements DynamicFeature {

    private final HerculesRestAccessor accessor;
    private final String applicationName;

    public ApplicationIPAuthFeature(HerculesRestAccessor accessor, String applicationName) {

        this.accessor = accessor;
        this.applicationName = applicationName;
    }

    @Override
    public void configure(ResourceInfo resourceInfo, FeatureContext context) {
        if (resourceInfo.getResourceMethod().getAnnotation(ApplicationIPAuthRequired.class) != null) {

            // not working
            context.register(new ApplicationIPAuthFilter(accessor, applicationName));

             //@Context injection works in ContainerRequestFilter for classes
            //context.register(ApplicationIPAuthFilter.class);
        }
    }
}

我正在尝试获取远程ip in ContainerRequestFilter基于ip验证请求。

I'm trying to fetch remote ip in ContainerRequestFilter to authenticate request based on ip.

如何解决这个问题..需要帮助吗?

How do I fix this.. need help?

推荐答案

。注册为对象时注入不起作用。

It's a known issue with DynamicFeature. Injections don't work when registering as an object.

您可以做的一件事是手动注入它,如。

One thing you can do is manually inject it, like mentioned in this post.

你可以做的另一件事是做 HerculesRestAccessor applicationName 注入。或者更确切地说,使应用程序名称可注入,您可以使其成为配置属性并。如果你这样做,那么你可以将过滤器注册为一个类,所有的注入都应该有效。

Another thing you can do is make the HerculesRestAccessor and the applicationName injectable. Or rather then making the application name injectable, you can make it a configuration property and inject the configuration. If you do this, then you can register the filter as a class, and all the injections should work.

这篇关于@Context注入无效在Jersey ContainerRequestFilter(Dropwizard)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:46