Which is the right function to call in a jsp page to verify the logged user is in role

烈酒焚心 提交于 2019-12-06 00:54:22

问题


I'm looking for the function to verify if the user logged belongs to a role. is maybe the following?

pageContext.request.userPrincipal.roles 

How should I use it properly along with JSTL to test if the user belong to ADMIN group?


回答1:


You can use Method expression 'request.isUserInRole' in JSP to check whether current authenticated user has a role.

Test this:

<c:if test="${not empty pageContext.request.userPrincipal}">

    <c:if test="${pageContext.request.isUserInRole('ADMIN')}">

        User ${pageContext.request.userPrincipal.name} in ADMIN Group

    </c:if>

</c:if>

Note that: Calling method with/without parameters in EL expression is only supported from JavaEE6 (JSP 2.2 & EL 2.2).



来源:https://stackoverflow.com/questions/24170824/which-is-the-right-function-to-call-in-a-jsp-page-to-verify-the-logged-user-is-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!