繁体   English   中英

如何使用SecureAnnotationsInterceptor(Apache CXF)保护多个端点?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM