是否可以在Wicket中使用基于元数据驱动的组件授权?

时间:2010-04-08 21:36:53

标签: authorization wicket

是否可以使用基于元数据驱动的组件授权?

Wicket in Action 提供了一个例子:

@AdminOnly
private class ModeLink extends Link {.....}
     

然后实施授权策略的isActionAuthorized()。

但我觉得为每个角色创建新课程都不是一个好的解决方案。

是否有以元数据驱动的方式执行此操作?我是否可以向组件添加一些元数据,然后根据授权策略的isActionAuthorized()方法进行检查?

2 个答案:

答案 0 :(得分:0)

我相信我在这里提供了一个基于元数据的解决方案:Wicket Authorization Using MetaDataKey以回答您的问题。

对于注释,您可以使用单个注释,例如@RequiresRole,使用角色ID进行参数化:例如@RequiresRole(“admin”)。

答案 1 :(得分:0)

您可以为所有角色使用字符串常量,并执行:

@Require(MySession.ADMIN_ROLE)
public class AdminPanel extends Panel {
  // snip
}

public boolean isActionAuthorized(Component c, Action a) {
  // I usually disallow instantiation of pages and rendering of components
  if (c instanceof Page || a == Component.RENDER) {
    Require r = c.getClass().getAnnotation(Require.class);
    if (r != null && !Strings.isEmpty(r.value()) {
      Roles roles = ((AuthenticatedWebSession)Session.get()).getRoles();
      return roles != null && roles.hasRole(a.value());
    }
  }
  return true;
}

您也可以使用枚举来禁用随机字符串,而不是字符串。在这种情况下,只需使用a.value().name()来检查角色。