You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Thymeleaf中实现用户授权登录后隐藏指定模块

实现方案

你可以直接使用Spring Security内置的isAnonymous()表达式实现效果,该表达式仅对未登录的匿名用户返回true,用户完成登录后无论持有任意角色都会返回false,对应模块会自动隐藏,代码写法如下:

<div sec:authorize="isAnonymous()">
   <a th:href="@{/forLogin}">Login</a>
</div>

可选反向写法

如果你更习惯用否定判断的逻辑,也可以使用如下写法,默认配置下和上面的实现效果完全一致:

<div sec:authorize="!isAuthenticated()">
   <a th:href="@{/forLogin}">Login</a>
</div>

内容的提问来源于stack exchange,提问作者Deniss Zadorozhniy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 15:06:05