@Context 注入在 Jersey ContainerRequestFilter (Dropwizard) 中不起作用
@Context injection not working in Jersey ContainerRequestFilter (Dropwizard)
@上下文注入适用于 类 但无法使其适用于对象。
ContainerRequestFilter 中的 httpServletRequest 产生空指针。
Dropwizard 版本:- 1.1.0
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();
....
}
}
动态特征
@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);
}
}
}
我正在尝试在 ContainerRequestFilter 中获取远程 ip 以根据 ip 对请求进行身份验证。
我该如何解决这个问题..需要帮助吗?
It's a known issue with DynamicFeature。注册为对象时注入不起作用。
您可以做的一件事是手动注入它,如 中所述。
您可以做的另一件事是使 HerculesRestAccessor
和 applicationName
可注射。或者更确切地说,让应用程序名称可注入,您可以将其配置为 属性 和 。如果这样做,那么您可以将过滤器注册为 class,并且所有注入都应该有效。
@上下文注入适用于 类 但无法使其适用于对象。
ContainerRequestFilter 中的 httpServletRequest 产生空指针。
Dropwizard 版本:- 1.1.0
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(); .... } }
动态特征
@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); } } }
我正在尝试在 ContainerRequestFilter 中获取远程 ip 以根据 ip 对请求进行身份验证。
我该如何解决这个问题..需要帮助吗?
It's a known issue with DynamicFeature。注册为对象时注入不起作用。
您可以做的一件事是手动注入它,如
您可以做的另一件事是使 HerculesRestAccessor
和 applicationName
可注射。或者更确切地说,让应用程序名称可注入,您可以将其配置为 属性 和