java - JSTL 标签中的 Spring Security 开关盒

标签 java spring jsp spring-security taglib

有没有办法把jsp代码转换成

的形式
<security:authorize access="hasRole('ROLE_USER')">You're an user</security:authorize>
<security:authorize access="hasRole('ROLE_ADMIN')">You're an admin</security:authorize>
<security:authorize access="hasRole('ROLE_SADMIN')">You're a superadmin</security:authorize>

另一种形式,类似于以下(不起作用)?

<c:choose>
  <c:when test="hasRole('ROLE_USER')">
    You're an user
  </c:when>
  <c:when test="hasRole('ROLE_ADMIN')">
    You're an admin
  </c:when>
  <c:when test="hasRole('ROLE_SADMIN')">
    You're a superadmin
  </c:when>
  <c:otherwise>
    You have no relevant role
  </c:otherwise>
</c:choose>

更准确地说,有没有一种方法可以用 JSTL 标签替换这个 Spring Security 标签库功能?

最佳答案

您可以使用 var <security:authorize/> 的属性将创建的标签:

A page scoped variable into which the boolean result of the tag evaluation will be written, allowing the same condition to be reused subsequently in the page without re-evaluation.

<security:authorize access="hasRole('ROLE_USER')" var="isUser" />
<security:authorize access="hasRole('ROLE_ADMIN')" var="isAdmin" />
<security:authorize access="hasRole('ROLE_SADMIN')" var="isSuperUser" />

<c:choose>
  <c:when test="${isSuperUser}">
    You're a superadmin
  </c:when>
  <c:when test="${isAdmin}">
    You're an admin
  </c:when>
  <c:when test="${isUser}">
    You're an user
  </c:when>
  <c:otherwise>
    You have no relevant role
  </c:otherwise>
</c:choose>

关于java - JSTL 标签中的 Spring Security 开关盒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26630095/

相关文章:

java - 更新 Android 中 CalendarView 中输入的日期

java 两个线程操作同一个数组

java - 创建类路径资源 [EnableWebMvcConfiguration.class] 中定义的名称为 'resourceHandlerMapping' 的 bean 时出错

javascript - 在jsp中访问javascript中的 session 变量

java - 为什么会编译?

java - toString() 方法中没有发生递归

java - @Value ("${local.server.port}") 在 Spring boot 1.5 中不起作用

java - 如何从 Spring 处理的 POST 请求中获取原始二进制数据?

java - 悬停时显示单词含义,可用作 MT 系统的后期编辑工具

java - WebJars 无法在 JSP 中访问