简体   繁体   English

JSTL标签中的Spring安全开关案例

[英]Spring Security switch case in JSTL Tags

Is there a way to convert a jsp code of the form 有没有办法转换窗体的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>

to another form, similar to the following (doesn't work)? 到另一种形式,类似于以下(不起作用)?

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

More precisely, is there a way to substitute this Spring Security taglib functionality with JSTL tags? 更确切地说,有没有办法用JSTL标签替换这个Spring Security taglib功能?

You can use the var attribute of the <security:authorize/> tag which will create: 您可以使用<security:authorize/>标记的var属性来创建:

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>

Workarround: When you have only this simple hasRole(XXX) expressions, then you could have a variable that contains the role of the current user. Workarround:如果只有这个简单的hasRole(XXX)表达式,那么你可以拥有一个包含当前用户角色的变量。 This variable could be populated by the controller, or when you needed it in almost all jsps, by an Spring HandlerInterceptor or Servlet Filter (that is registered after the Spring Security Filter). 这个变量可以由控制器填充,或者在几乎所有jsps中需要时,由Spring HandlerInterceptor或Servlet Filter(在Spring Security Filter之后注册)填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM