Jersey:注入在资源方法中有效,但不适用于 EJB 属性

发布于 2024-12-12 13:49:18 字数 4601 浏览 0 评论 0原文

我有一个自定义 InjectionProvider,它从 SecurityContext 检索 User 对象并使其可用。自定义 ResourceFilter 执行 HTTP 基本身份验证、创建 SecurityContext 并使用主体填充它。这是基于此建议

当我从资源方法访问它时,它按预期工作:

@Stateless
@Path("foo")
public class FooResource {

  @GET
  @ResourceFilters(value = {UserAuthenticationFilter.class})
  public Response get(@Context User user) {
    return Response.ok().entity(user).build();
  }
}

但是,当我尝试将对象作为属性/字段注入到 EJB 时,会抛出异常,即使我可以在记录 InjectionProvider 被调用:

@Stateless
@Path("foo")
public class FooResource {

  @Context
  private User user;

  @GET
  @ResourceFilters(value = {UserAuthenticationFilter.class})
  public Response get() {
    return Response.ok().entity(user).build();
  }
}

异常很奇怪:

[#|2011-10-31T13:35:47.691+0000|WARNING|glassfish3.1.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=18;_ThreadName=Thread-3;|StandardWrapperValve[Gateway Servlet]: PWC1406: Servlet.service() for servlet Gateway Servlet threw exception
javax.ejb.CreateException: Could not create stateless EJB
    at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:534)
    at com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:95)
    at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:724)
    at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:247)
    at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:449)
    at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2528)
    at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1895)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
    at $Proxy166.get(Unknown Source)
....
Caused by: com.sun.jersey.spi.inject.Errors$ErrorMessagesException
    at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
    at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
    at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
    at com.sun.jersey.server.impl.application.WebApplicationImpl$ComponentProcessorFactoryImpl.get(WebApplicationImpl.java:499)
    at com.sun.jersey.server.impl.ejb.EJBInjectionInterceptor.get(EJBInjectionInterceptor.java:104)
    at com.sun.jersey.server.impl.ejb.EJBInjectionInterceptor.init(EJBInjectionInterceptor.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCallback(SystemInterceptorProxy.java:133)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.init(SystemInterceptorProxy.java:120)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.ejb.containers.interceptors.CallbackInterceptor.intercept(InterceptorManager.java:964)
    at com.sun.ejb.containers.interceptors.CallbackChainImpl.invokeNext(CallbackChainImpl.java:65)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:393)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:376)
    at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:526)
    ... 55 more

我做错了什么?

编辑:我刚刚注意到在抛出异常之前,server.log中报告了一个错误:

[#|2011-10-31T15:33:01.854+0000|SEVERE|glassfish3.1.1|com.sun.jersey.spi.inject.Errors|_ThreadID=18;_ThreadName=Thread-3;|The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Missing dependency for field: private com.skalio.bonusapp.core.User com.skalio.bonusapp.gateway.FooResource.user|#]

I have a custom InjectionProvider which retrieves a User object from the SecurityContext and makes it available. A custom ResourceFilter performs the HTTP Basic authentication, creates the SecurityContext and populates it with a Principal. This is based on this suggestion.

When I access it from a resource method, it works as expected:

@Stateless
@Path("foo")
public class FooResource {

  @GET
  @ResourceFilters(value = {UserAuthenticationFilter.class})
  public Response get(@Context User user) {
    return Response.ok().entity(user).build();
  }
}

However, when I try to inject the object as an attribute/field to the EJB, exceptions get thrown, even though I can see in the logs that the InjectionProvider gets called:

@Stateless
@Path("foo")
public class FooResource {

  @Context
  private User user;

  @GET
  @ResourceFilters(value = {UserAuthenticationFilter.class})
  public Response get() {
    return Response.ok().entity(user).build();
  }
}

The exception is strange:

[#|2011-10-31T13:35:47.691+0000|WARNING|glassfish3.1.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=18;_ThreadName=Thread-3;|StandardWrapperValve[Gateway Servlet]: PWC1406: Servlet.service() for servlet Gateway Servlet threw exception
javax.ejb.CreateException: Could not create stateless EJB
    at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:534)
    at com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:95)
    at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:724)
    at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:247)
    at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:449)
    at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2528)
    at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1895)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
    at $Proxy166.get(Unknown Source)
....
Caused by: com.sun.jersey.spi.inject.Errors$ErrorMessagesException
    at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
    at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
    at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
    at com.sun.jersey.server.impl.application.WebApplicationImpl$ComponentProcessorFactoryImpl.get(WebApplicationImpl.java:499)
    at com.sun.jersey.server.impl.ejb.EJBInjectionInterceptor.get(EJBInjectionInterceptor.java:104)
    at com.sun.jersey.server.impl.ejb.EJBInjectionInterceptor.init(EJBInjectionInterceptor.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCallback(SystemInterceptorProxy.java:133)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.init(SystemInterceptorProxy.java:120)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.ejb.containers.interceptors.CallbackInterceptor.intercept(InterceptorManager.java:964)
    at com.sun.ejb.containers.interceptors.CallbackChainImpl.invokeNext(CallbackChainImpl.java:65)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:393)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:376)
    at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:526)
    ... 55 more

What am I doing wrong?

EDIT: I just noticed that before the exception is thrown, an error is reported in the server.log:

[#|2011-10-31T15:33:01.854+0000|SEVERE|glassfish3.1.1|com.sun.jersey.spi.inject.Errors|_ThreadID=18;_ThreadName=Thread-3;|The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Missing dependency for field: private com.skalio.bonusapp.core.User com.skalio.bonusapp.gateway.FooResource.user|#]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2024-12-19 13:49:19

这是由于范围不匹配造成的。您正在尝试将请求范围内的事物注入到非请求范围内的 bean 中。那是行不通的。

This is due to a scope mismatch. You are trying to inject a request-scoped thing to a bean that is not request scoped. That won't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文