Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 348
Mazzy Avatar asked Jun 11 '14 19:06

Mazzy


People also ask

Is it possible to call JavaScript function from JSP?

It is not direct call of JavaScript function from JSP, you just prepare the call and call is performed when page is parsed/loaded on the client side:

How to validate a user in JSP?

Steps to Validate a User: 1 We click the link on index.html page to deploy the application. 2 We are then presented with a form, where we enter username and password and click submit. 3 The JSP gets automatically called and it returns the data entered in the form and the result of Validation. More ...

What is a JSP page?

The JSP page is called via a JSP page, HTML page, or JAVA servlet. Once it is called then the cycle mentioned below is triggered to generate dynamic content. This dynamic content is then fed on the web page. Any server can be used in the back end like wildfly, tomcat, or others for this technology to work.

How do I find the JSP code of a website?

The JSP code is written under separate tags to determine the JSP code but this should all be enclosed under HTML tags like this: <head> All header related things to be included here. The title is also attached to the web page in this section itself. </head> How does JSP Page work in JSP?


1 Answers

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).

like image 167
Loc Avatar answered Oct 17 '22 10:10

Loc