1

我有两种类型的管理员。超级管理员和普通管理员。

两者都从页面 admin.xhtml 开始。

我想将超级管理员用户转发到 super-admin.xhtml,将普通管理员转发到 normal-admin.xhtml。

我如何在 JSF 中执行此操作(我正在使用 Spring Security)?

4

1 回答 1

0

我对 JSF 不熟悉,但假设它像 Spring MVC JSP 应用程序一样在后台运行,您可以让控制器根据用户持有的角色提供不同的页面:

@RequestMapping("/admin.xhtml")
@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPERADMIN')")
public String getAdminPage(Modelmap model, Principal principal) {
    Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
    for (GrantedAuthority authority : authorities) {
        if (authority.toString() == "ROLE_SUPERADMIN") { return "superadminpage"; }
    }
    //no need to check for admin privileges, since the annotation took care of that
    //if you're not using annotations (or @PostAuthorize), you'd have to capture the
    //'admin' role as well, and adjust the return statements accordingly.
    return "adminpage";
}
于 2013-05-09T15:20:48.330 回答