【问题标题】:Equivalent to the deprecated <sec:authorize> ifNotGranted attribute等效于已弃用的 <sec:authorize> ifNotGranted 属性
【发布时间】:2012-06-12 18:28:17
【问题描述】:

如果用户没有特定角色,我想阻止图像具有链接。例如

<sec:authorize ifNotGranted="ROLE_ACCOUNTS" ><img src="someimage.jpg"/></sec:authorize>
<sec:authorize ifAllGranted="ROLE_ACCOUNTS" ><a href="somelink.htm"><img src="someimage.jpg"/></a></sec:authorize>

然而,ifNotGranted 和 ifAllGranted 现在已被弃用,取而代之的是访问表达式。我可以看到 ifAllGranted 可以复制:

<sec:authorize access="hasRole('ROLE_ACCOUNTS')"><a href="somelink.htm"><img src="someimage.jpg"/></a></sec:authorize>

但是如何使用访问方法复制 ifNotGranted 呢?任何帮助将不胜感激。

【问题讨论】:

  • access="isFullyAuthenticated() 而不是 hasRole('ROLE_ACCOUNTS')"
  • @Ritesh 感谢isFullyAuthenticated() 对于另一个页面可能非常有用。但是,此处的页面访问权限仅对登录用户可用。 not hasRole('ROLE_ACCOUNTS') 成功了。

标签: jsp spring-security jsp-tags


【解决方案1】:

SpEL 表达式可以用! 运算符取反:

<sec:authorize access="!hasRole('ROLE_ACCOUNTS')">...</sec:authorize> 

另请参阅:

【讨论】:

    【解决方案2】:

    如果你有很多文件要更新,我建议使用正则表达式进行搜索和替换

    找到

    <sec:authorize\s+ifAnyGranted="([^"]+)"
    

    替换为

    <sec:authorize access="hasAnyRole('$1')"
    

    搜索

    <sec:authorize\s+ifNotGranted="([^"]+)"
    

    替换为

    <sec:authorize access="!hasAnyRole('$1')"
    

    希望这可以节省您的时间

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      相关资源
      最近更新 更多