[英]How to secure multiple endpoint with SecureAnnotationsInterceptor (Apache CXF)?
我正在使用Apache CXF,我想使用SecureAnnotationsInterceptor
通過@RolesAllowed
注釋來保護我的端點。
據我所知,我必須告訴攔截器通過將引用傳遞給setSecuredObject
方法來保護哪個對象。
不幸的是,代碼不是設計來處理bean列表。
我當時想知道如何用這個攔截器保護多個端點。
我是否必須創建我自己的攔截器版本或創建它的多個實例(每個端點一個以保護)或其他什么?
對不起,這是一個答案,因為我沒有足夠的代表評論Ahmed M Farghali的回答。 通過上面的實現,我們遇到了一個問題,我們使用@RolesAllowed注釋接口,但並非所有端點都受到保護。 事實證明,如果rolesMap為空,findRoles()將檢查超類。 在第一次運行時,這種情況正確發生,但由於重新使用rolesMap,因此其他服務將不受保護。 我們通過將setSecuredObject方法更改為:
public void setSecuredObject(Object object, Map<String, String> rolesMap) {
Class<?> cls = ClassHelper.getRealClass(object);
Map<String, String> instanceRoleMap = new HashMap<>();
findRoles(cls, instanceRoleMap);
if (instanceRoleMap.isEmpty()) {
LOG.warning("The roles map is empty, the service object is not protected");
} else if (LOG.isLoggable(Level.FINE)) {
for (Map.Entry<String, String> entry : instanceRoleMap.entrySet()) {
LOG.fine("Method: " + entry.getKey() + ", roles: " + entry.getValue());
}
}
rolesMap.putAll(instanceRoleMap);
}
我不知道你是否找到了答案。 對我來說,我已經修改了這個攔截器的setSecuredObject
方法如下:
public void setSecuredObjectsList(Object[] objects) {
Map<String, String> rolesMap = new HashMap<String, String>();
for (Object o:objects ) {
setSecuredObject(o, rolesMap);
}
super.setMethodRolesMap(rolesMap);
}
public void setSecuredObject(Object object, Map<String, String> rolesMap) {
Class<?> cls = ClassHelper.getRealClass(object);
findRoles(cls, rolesMap);
if (rolesMap.isEmpty()) {
LOG.warning("The roles map is empty, the service object is not protected");
} else if (LOG.isLoggable(Level.FINE)) {
for (Map.Entry<String, String> entry : rolesMap.entrySet()) {
LOG.fine("Method: " + entry.getKey() + ", roles: " + entry.getValue());
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.