Search code examples
jspjstlazure-web-roles

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


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?


Solution

  • You can use the Method expression 'pageContext.request.isUserInRole' in JSP to check whether the 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>
    

    Notes:

    • Make sure you have a JSTL Core Tags taglib directive on the top of your JSP file:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

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