如何使用Spring安全性访问JSP中的角色?

Raj*_*esh 20 java spring jsp spring-security

我想在JSP中打印用户角色?我知道有一个名为" <sec:authentication property="principal.username"/> 使用此标记" 的spring -security标记我可以访问用户名.但是如何在jsp中访问和打印当前角色?

lim*_*imc 22

由于principal引用了您的UserDetails对象,因此如果您检查该对象,则存储角色public Collection<GrantedAuthority> getAuthorities() { .. }.

也就是说,如果您只想在屏幕上打印角色,请执行以下操作: -

<sec:authentication property="principal.authorities"/>
Run Code Online (Sandbox Code Playgroud)


Nim*_*sky 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>
Run Code Online (Sandbox Code Playgroud)

这里开始.


小智 5

<sec:authentication property="principal.authorities" var="authorities" />
<c:forEach items="${authorities}" var="authority" varStatus="vs">
<p>${authority.authority}</p>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)