【问题标题】:How to access role in JSP using spring security?如何使用 Spring Security 访问 JSP 中的角色?
【发布时间】:2012-03-13 21:44:03
【问题描述】:

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

【问题讨论】:

    标签: java spring jsp spring-security


    【解决方案1】:

    使用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

    【讨论】:

    • 如何在 jsp 中使用 getAuthorities ?
    • 只使用正常的 bean 约定:
    【解决方案2】:

    由于principal 指的是您的UserDetails 对象,如果您检查该对象,则角色存储在public Collection&lt;GrantedAuthority&gt; getAuthorities() { .. } 下。

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

    <sec:authentication property="principal.authorities"/>
    

    【讨论】:

      【解决方案3】:
      <sec:authentication property="principal.authorities" var="authorities" />
      <c:forEach items="${authorities}" var="authority" varStatus="vs">
      <p>${authority.authority}</p>
      </c:forEach>
      

      【讨论】:

        猜你喜欢
        • 2017-10-19
        • 1970-01-01
        • 1970-01-01
        • 2013-12-15
        • 2019-08-08
        • 2018-11-28
        • 1970-01-01
        • 2010-10-02
        • 1970-01-01
        相关资源
        最近更新 更多