根据 web.xml 中定义的用户角色显示菜单项

Display menu items depending on user roles defined in web.xml

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

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

在我的 ServletListener 中

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

我从 web.xml 中读取 security-constraint,解析它们并将其存储在我的 @ApplicationScoped bean 中。

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

检查每个人
FacesContext.getCurrentInstance().GetExternalContext().IsUserInRole(role);

方法。

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

编辑:这个解决方案的缺点是我只分析了 web.xml 文件,没有注释