我想在JSP中打印用户角色?我知道有一个名为<sec:authentication property="principal.username"/>
的spring -security标签
使用此标记我可以访问用户名..但是如何在jsp中访问和打印当前角色?
答案 0 :(得分:22)
由于principal
引用了您的UserDetails
对象,因此如果您检查该对象,则角色会存储在public Collection<GrantedAuthority> getAuthorities() { .. }
下。
也就是说,如果您只想在屏幕上打印角色,请执行以下操作: -
<sec:authentication property="principal.authorities"/>
答案 1 :(得分:21)
使用getAuthorities或编写自己的userdetails实现并创建便捷方法。
或:
<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>
来自here。
答案 2 :(得分:4)
<sec:authentication property="principal.authorities" var="authorities" />
<c:forEach items="${authorities}" var="authority" varStatus="vs">
<p>${authority.authority}</p>
</c:forEach>