我正在使用 Apache CXF,并且我想使用 SecureAnnotationsInterceptor
通过 @RolesAllowed
注释来保护我的端点。
据我了解,我必须通过将引用传递给 setSecuredObject
方法来告诉拦截器要保护哪个对象。
不幸的是,该代码并不是为处理 bean 列表而设计的。
然后我想知道如何使用此拦截器保护多个端点。
我是否必须创建自己的拦截器版本或创建它的多个实例(每个端点一个以确保安全)或其他内容?
最佳答案
抱歉,这是一个答案,因为我没有足够的代表来评论 Ahmed M Farghali 的答案。通过上述实现,我们遇到了一个问题,即我们使用 @RolesAllowed 注释接口(interface),但并非所有端点都受到保护。事实证明,如果 RolesMap 为空,则 findRoles() 将检查父类(super class)。第一次运行时,这种情况会正确发生,但由于 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);
}
关于java - 如何使用 SecureAnnotationsInterceptor (Apache CXF) 保护多个端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21135671/