SecurityContext에서 User
오브젝트를 검색하여 사용 가능하게하는 사용자 정의 InjectionProvider가 있습니다. 사용자 지정 ResourceFilter
은 HTTP 기본 인증을 수행하고 SecurityContext를 만들고이를 Principal에 채 웁니다. 이는 this suggestion을 기준으로합니다. 나는 자원 방법에서 액세스Jersey : Injection은 리소스 메소드에서 작동하지만 EJB 속성에는 적용되지 않습니다.
, 그것은 예상대로 작동합니다
@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
내가 뭘 잘못하고 있니?
편집 :이이 범위 불일치로 인해
[#|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|#]
알겠습니다. 리소스 자체는 요청 범위가 아니지만 메서드 호출은 분명히 있습니다. 감사. 다른 방법이 있습니까? 아니면 각각의 (관련) 메소드 호출시 사용자를 주입하는 것이 실제로 좋은 패턴일까요? – Hank
일반 자원은 요청 범위이지만 stateless EJB 자원은 요청 범위가 아닙니다. 시도 할 수있는 한 가지 아이디어는 ThreadLocals를 사용하여 주어진 컨텍스트에 대해 해당 속성의 올바른 값을 제공하는 Singleton (Singleton Injection Provider 구현)을 User 객체로 만드는 것입니다. 또는 (ThreadLocals를 사용하여) 주어진 스레드에 대해 올바른 사용자를 반환하는 getUser() 메소드로 UserInfo 객체를 가질 수 있습니다. ContainerRequestFilter를 사용하여 주어진 스레드에 대해 해당 객체의 값을 설정하고 요청이 처리 된 후 ContainerResponseFilter가이를 정리할 수 있습니다. –