Thymeleaf 与 Shrio 的整合

1、整合依赖
<!--配置 Thymeleaf 与 Shrio 的整合依赖-->
<dependency>
 <groupId>com.github.theborakompanioni</groupId>
 <artifactId>thymeleaf-extras-shiro</artifactId>
 <version>2.0.0</version>
</dependency>
2、配置类添加新配置

用于解析 thymeleaf 中的 shiro:相关属性

@Bean
public ShiroDialect shiroDialect(){
 return new ShiroDialect();
}

3、Thymeleaf 中常用的 shiro:属性

guest 标签
<shiro:guest>
</shiro:guest>

用户没有身份验证时显示相应信息,即游客访问信息。

user 标签
<shiro:user>
</shiro:user>

用户已经身份验证/记住我登录后显示相应的信息。

authenticated 标签
<shiro:authenticated>
</shiro:authenticated>

用户已经身份验证通过,即 Subject.login 登录成功,不是记住我登录的。

notAuthenticated 标签
<shiro:notAuthenticated>
</shiro:notAuthenticated>

用户已经身份验证通过,即没有调用 Subject.login 进行登录,包括记住我自动登录的也属于未进行身份验证。

principal 标签
<shiro: principal/>
<shiro:principal property="username"/>

相当于((User)Subject.getPrincipals()).getUsername()。

lacksPermission 标签
<shiro:lacksPermission name="org:create">
</shiro:lacksPermission>

如果当前 Subject 没有权限将显示 body 体内容。

hasRole 标签
<shiro:hasRole name="admin">
</shiro:hasRole>

如果当前 Subject 有角色将显示 body 体内容。

hasAnyRoles 标签
<shiro:hasAnyRoles name="admin,user">
</shiro:hasAnyRoles>

如果当前 Subject 有任意一个角色(或的关系)将显示 body 体内容。

lacksRole 标签
<shiro:lacksRole name="abc">
</shiro:lacksRole>

如果当前 Subject 没有角色将显示 body 体内容。

hasPermission 标签
<shiro:hasPermission name="user:create">
</shiro:hasPermission>

如果当前 Subject 有权限将显示 body 体内容

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shiro整合Thymeleaf是为了在Thymeleaf模板引擎中使用Shiro的功能。首先需要在页面中添加Shiro的命名空间声明,即xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro"。然后在项目的pom.xml文件中引入thymeleaf-extras-shiro的依赖,例如:<dependency><groupId>com.github.theborakompanioni</groupId><artifactId>thymeleaf-extras-shiro</artifactId><version>2.0.0</version></dependency>。在Shiro的配置类中配置ShiroDialect(Shiro方言)对象,例如:@Bean public ShiroDialect shiroDialect() { return new ShiroDialect(); }。最后,在Thymeleaf的页面中,可以使用Shiro的相关标签和表达式来实现动态检测和授权控制。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【Shiro笔记三】Shiro整合Thymeleaf](https://blog.csdn.net/m0_67393827/article/details/124099555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Shiro整合thymeleaf](https://blog.csdn.net/qq_44116526/article/details/122115756)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值