2016-05-25 11 views

回答

0

隨着有點難看管道的就可以解決如下(wildfly 8.xx):

if (SecurityContextAssociation.getSecurityContext()==null) 
    SecurityContextAssociation.setSecurityContext(new JBossSecurityContext("background-job")); 
SecurityContext current = SecurityContextAssociation.getSecurityContext(); 
final Object cred = current.getUtil().getCredential(); 
final Subject s = current.getUtil().getSubject(); 
final Principal up = current.getUtil().getUserPrincipal(); 
boolean needToUpdatePrincipal=true; 
if (up instanceof TenantPrincipal) { 
    if (t.getTenantName().equals(((TenantPrincipal) up).getAdditonalField())) { 
     needToUpdatePrincipal=false; 
    } 
} 
if (needToUpdatePrincipal) { 
    TenantPrincipal tp=new TenantPrincipal(up.getName()); 
    tp.setAdditionalField(t.getTenantName()); 
    current.getUtil().createSubjectInfo(
      , cred, (Subject) s); 
} 

基本上你需要創建自己的主體類設置上下文數據在其實例的附加字段中。

1

從本質上講,你只有兩種選擇:

  1. 傳遞值作爲參數
  2. 保存在一些全球性的地方該值。像靜態變量一樣。

第一個選項更清潔和更容易。不要使用第二個:)

+0

第一個選項的問題是我沒有一個而是幾十個異步方法,我不想做如此大量的重構。而且我需要傳遞的參數通常不是直接用於這些方法,而是更深層次的調用層次結構。因此,從我真正需要的地方以另一種方式訪問​​它會更好。單獨的靜態變量不是線程安全的。 – vinga

+0

線程安全取決於您如何使用該變量。你可以有'AtomicReference',你只能在'synchronized'塊中訪問它,你可以在靜態變量中爲你的數據設置線程安全的容器。選項很多。 – Nikem

+0

我所需要的只是確保在訪問它時,這與調用方法中的值完全相同(例如它是請求範圍)。我可以使用例如ThreadLocal,但問題仍然是如何傳播數據? – vinga

相關問題