I want to print a user role in JSP? I know there is a spring -security tag called as <sec:authentication property="principal.username"/>
Using this tag I can access the user name..but how to access and print the current role in jsp?
The first way to check for user roles in Java is to use the @PreAuthorize annotation provided by Spring Security. This annotation can be applied to a class or method, and it accepts a single string value that represents a SpEL expression.
properties, Spring MVC will look for view-books. jsp inside the /WEB-INF/jsp/ directory. The above example shows us how to use the JSTL <c:url> tag to link to external resources such as JavaScript and CSS. We normally place these under the ${project.
“/userPage” is used by USER Role to access and perform Normal user activities. “/adminPage” is used by ADMIN Role to access and perform Admin user activities. ADMIN role can access “/userPage” URL too.
Since principal
refers to your UserDetails
object, if you inspect that object the roles are stored under public Collection<GrantedAuthority> getAuthorities() { .. }
.
That said, if you merely want to print the roles on the screen, do this:-
<sec:authentication property="principal.authorities"/>
Use getAuthorities or write your own implementation of userdetails and create a convenience method.
or :
<sec:authorize access="hasRole('supervisor')">
This content will only be visible to users who have
the "supervisor" authority in their list of <tt>GrantedAuthority</tt>s.
</sec:authorize>
from here .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With