【问题标题】:JSF Redirect page based on User role基于用户角色的 JSF 重定向页面
【发布时间】:2013-05-09 11:09:46
【问题描述】:

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

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

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

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

【问题讨论】:

  • 在 admin.xhtml 页面上,您希望通过什么操作重定向到不同的页面?

标签: jsf jsf-2 spring-security


【解决方案1】:

我对 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";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-13
    • 2016-01-17
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2022-08-24
    相关资源
    最近更新 更多