2012-09-13 65 views
3

我有一個匿名WebService EJB - webservice調用正在正常工作。@RunAs for @WebService EJB

現在我想將WebService作爲一個特定的SecurityRole來運行。

在一個Webservice,我有以下注釋:

@Stateless 
@WebService 
@DeclareRoles({ "LoggedUser" }) 
@SecurityDomain("my-jboss-real") 
@RunAs("LoggedUser") 
public class MyWebService { ... 

現在我想從Webservice的方法有與@RolesAllowed({"LoggedUser"})訪問@EJB我得到:

ERROR [org.jboss.aspects.tx.TxPolicy] javax.ejb.EJBTransactionRolledbackException: javax.ejb.EJBAccessException.message: 'Caller unauthorized' 
WARN [org.jboss.ejb3.stateless.StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from stateless bean context, it should already have been injected 
ERROR [org.jboss.ws.core.jaxws.SOAPFaultHelperJAXWS] SOAP request exception 
javax.ejb.EJBTransactionRolledbackException: javax.ejb.EJBAccessException.message: 'Caller unauthorized' 
     at org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:115) 

我在JBoss 5.1上運行GA

這是否正確使用@RunAs還是有另一種方法來做到這一點。

編輯

補充:

@Resource 
private WebServiceContext wsCtx; 
@Resource 
private EJBContext ejbCtx; 

myWebServiceMethod(){ 
    ... 
    System.out.println("EJBCtx: " + ejbCtx.getCallerPrincipal()); 
    System.out.println("EJBCtx: " + ejbCtx.isCallerInRole("LoggedUser")); 
    System.out.println("WebContext: " + wsCtx.getUserPrincipal()); 
    System.out.println("WebContext: " + wsCtx.isUserInRole("LoggedUser")); 
    ... 

此輸出:

EJBCtx: anonymous 
EJBCtx: false 
WebContext: anonymous 
WebContext: false 

回答

4

JBoss AS中5和6特別是非常錯誤關於安全上下文和@RunAs在任何但最基本的用例。

大量這些錯誤已在 AS 7中修復。您可以嘗試在那裏設置測試用例,看看是否遇到同樣的問題。

請認識到@RunAs不適用於在應用了註釋的bean中運行的代碼。相反,它只適用於從bean中調用的bean。您可以將其視爲「傳出/傳出」角色。

更麻煩的是Java EE有一個嚴重的遺漏,那就是沒有辦法同時定義一個RunAs 的標識。當您爲「未經身份驗證的」身份定義RunAs角色時,某些服務器的反應不佳。 JBoss擁有RunAs標識的專有註釋。如果這會讓你更進一步,你可能會想嘗試。