【发布时间】:2012-03-13 21:44:03
【问题描述】:
我想在 JSP 中打印一个用户角色?我知道有一个名为 <sec:authentication property="principal.username"/> 的 spring -security 标签
使用这个标签我可以访问用户名..但是如何在jsp中访问和打印当前角色?
【问题讨论】:
标签: java spring jsp spring-security
我想在 JSP 中打印一个用户角色?我知道有一个名为 <sec:authentication property="principal.username"/> 的 spring -security 标签
使用这个标签我可以访问用户名..但是如何在jsp中访问和打印当前角色?
【问题讨论】:
标签: java spring jsp spring-security
使用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。
【讨论】:
由于principal 指的是您的UserDetails 对象,如果您检查该对象,则角色存储在public Collection<GrantedAuthority> getAuthorities() { .. } 下。
也就是说,如果您只想在屏幕上打印角色,请执行以下操作:-
<sec:authentication property="principal.authorities"/>
【讨论】:
<sec:authentication property="principal.authorities" var="authorities" />
<c:forEach items="${authorities}" var="authority" varStatus="vs">
<p>${authority.authority}</p>
</c:forEach>
【讨论】: