2013-03-27 58 views
1

背景:Spring 3 Bean中的EJB 3 Sessioncontext

我正在使用java安全性的編程式身份驗證(基本身份驗證)。 我有一個無狀態會話bean(EJB 3)。我可以注入SessionContext來獲取安全主體,以獲取用戶名。 在同一個項目中,我使用JDBC和Aspect的spring bean。我想在一個方面獲得用戶名(審計),這也是一個春天的bean。

問:

是否有可能訪問EJB sesssioncontext中的Spring bean。如果是這樣怎麼辦? 如果不是,如何從Spring bean中訪問用戶名,這也是一個方面。

感謝, 阿米特

+0

注:我不使用彈簧安全... – Dutta 2013-03-28 00:36:52

回答

1

kolossus,感謝您的回覆。 還有另外一種方法可以找到。以下是鏈接到previous post。所以基本上我做同樣的事情: 1)增加以下行至春上下文XML

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"> 
    <property name="alwaysUseJndiLookup" value="true" /> 
</bean> 

2)添加下面一行到我的Spring bean來訪問EJB會話的上下文

@Resource(mappedName = "java:global/TopoServices/MessageRestService!com.myrest.MessageRestService") 
    private MessageRestService myBean; 

重要的是Jboss 7.1不再支持自定義jndi,與Jboss 5不同,所以我使用了默認的jndi。

我認爲這是更清潔的方式來解決我的要求。

+0

+1,做得不錯的工作user1842486。不要忘記接受你的答案 – kolossus 2013-03-29 18:28:38

1

這可以通過注射EJB到的Spring bean來完成。您可以通過手動JNDI查找(就像您在任何其他EJB客戶端中那樣)或使用Spring的JndiObjectFactoryBean來完成。 Spring的JndiObjectFactoryBean來:

  1. 注入SessionContext到EJB

    @Resource 
    private SessionContext ctxt; 
    //getter and setter 
    
  2. 配置工廠bean在bean的配置文件。 postageService作爲EJBRef我們感興趣的

    <bean id="postageService class="org.springframework.jndi.JndiObjectFactoryBean"> 
         <property name="jndiEnvironment"> 
          <props> 
           <prop key="java.naming.factory.initial"> 
            org.apache.openejb.client.RemoteInitialContextFactory 
           </prop> 
           <prop key="java.naming.provider.url"> 
            ejbd://localhost:4201 
           </prop> 
          </props> 
         </property> 
         <property name="jndiName" value="PostageServiceBeanRemote"/> 
        </bean> 
    
  3. 線EJB引用(配置從Apress的春節食譜荷包蛋)到您的Spring bean

     public class PostalServiceClient{ 
    
         //wired EJB 
         private PostageService service; 
         //getter and setter 
    
         private SessionContext retrieveSessionContext(){ 
          service.getCtxt(); //get the SessionContext from the EJB injection 
         } 
    
         }