Reputation: 3014
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?
Upvotes: 21
Views: 29996
Reputation: 51
<sec:authentication property="principal.authorities" var="authorities" />
<c:forEach items="${authorities}" var="authority" varStatus="vs">
<p>${authority.authority}</p>
</c:forEach>
Upvotes: 5
Reputation: 47280
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 .
Upvotes: 21
Reputation: 40168
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"/>
Upvotes: 23