6

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?

Mazzy
  • 13,354
  • 43
  • 126
  • 207

1 Answers1

12

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).
LHA
  • 9,398
  • 8
  • 46
  • 85