【问题标题】:Display menu items depending on user roles defined in web.xml根据 web.xml 中定义的用户角色显示菜单项
【发布时间】:2017-06-17 12:17:10
【问题描述】:

我有自己的领域类扩展了 AppservPasswordLoginModule 和 AppservRealm,我从我自己的数据库表中获取用户和角色。在 web.xml 中,我定义了对页面的访问,它可以工作。 我有一些机制可以使用 @SessionScoped 注释从我的 bean 中的 menu.xml 文件中读取主菜单。 我想使用 web.xml 中的规则来仅显示用户有权访问的这些项目(在 web.xml 中定义),而不在我的 menu.xml 文件中重复配置。 我想解决方案可能是在我的 SessionScoped bean 中为此页面创建菜单项时检查对页面的访问,但我不知道如何轻松检查它。 这种情况的最佳解决方案是什么? 我正在使用 glassfish 4.1 和 jsf 2.2。

【问题讨论】:

    标签: jsf authentication web.xml conditional-rendering security-constraint


    【解决方案1】:

    我通过以下方法解决了这个问题:

    在我的 ServletListener 中

    @WebListener
    public class implements ServletListener ServletContextListener {...}
    

    我从 web.xml 中读取安全约束,解析它们并将其存储在我的 @ApplicationScoped bean 中。

    在@SessionScoped bean 中,在@PostConstruct 注释方法中,我获取存储在@ApplicationScoped bean 中的所有角色并通过

    FacesContext.getCurrentInstance().GetExternalContext().IsUserInRole(role);
    

    方法。

    所以我拥有所有当前用户角色。然后,在我的@SessionScoped bean 中,对于每个菜单项,我检查由该菜单项的 url 表示的资源是否可用于当前用户拥有的角色。

    编辑:这个解决方案的不好的一面是我只分析 web.xml 文件,没有注释

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 2014-09-25
      • 2015-05-08
      • 1970-01-01
      • 2021-07-15
      相关资源
      最近更新 更多