2017-04-10 91 views
0

將JavaEE應用程序部署爲WAR文件(使用WAS Liberty Profile應用程序服務器)時,應用程序角色和用戶組之間的映射在server.xml中定義。我們選擇通過EJB bean攔截器來實現安全性,該攔截器將方法註釋中聲明的權限與分配給用戶的一組權限進行比較。 這個想法是基於Java雜誌(Secure Java EE Authentication," Java Magazine, January/February 2013)的原創文章。如何以編程方式獲取由@DeclareRoles定義的角色

爲了進一步理解,我們希望將與用戶關聯的角色映射到更精細的一組權限。不幸的是,目前沒有辦法很容易地獲得與用戶關聯的角色列表。在stack overflow article中提出了兩種建議的方法,分別是@Josh@Steve

讓我感到震驚的是,如果我可以獲得由@DeclareRoles()註釋定義的角色列表,那麼我可以對每個角色使用request.isUserInRole(role)方法,而無需維護一個單獨的角色列表我。

有沒有人使用過這種方法,還是有更好的方法來實現一個更細粒度的安全模型,因爲寫了文章?

+0

您的意思是[javax.annotation.security.DeclareRoles](http://docs.oracle.com/javaee/7/api/javax/annotation/security/DeclareRoles.html)? –

+0

謝謝史蒂夫,你很正確我更新了我的問題。 – mikee

回答

1

好吧,你當然可以這樣做:

@Stateless 
@LocalBean 
@DeclareRoles({ ROLE1, ROLE2, ROLE3 }) 
public class IsCallerInRoleDemoSessionBean { 

    @Resource 
    private SessionContext sessionContext; 

    @PermitAll 
    public Set<String> discoverRoles() { 
     Set<String> roleNames = new HashSet<>(); 
     DeclareRoles declaredRoles = IsCallerInRoleDemoSessionBean.class.getAnnotation(DeclareRoles.class); 
     for (String roleName : declaredRoles.value()) 
      if (sessionContext.isCallerInRole(roleName)) 
       roleNames.add(roleName); 
     return roleNames; 
    } 

} 

這是從舊Arquillian Security Demo我幾年前做的人。

理想情況下,這也會檢查超類。

相關問題