Shiro和Thymeleaf整合:实现标签的动态显示

1、导入相关依赖

<!--Shiro-->
<dependency>
   <groupId>org.apache.shiro</groupId>
   <artifactId>shiro-spring</artifactId>
   <version>1.7.1</version>
</dependency>
<!--Shiro整合Thymeleaf-->
<dependency>
   <groupId>com.github.theborakompanioni</groupId>
   <artifactId>thymeleaf-extras-shiro</artifactId>
   <version>2.0.0</version>
</dependency>

2、html标签引入Thymeleaf和shiro

<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

3、配置一个ShiroDialect

package com.oligizzz.wildfire.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: GNMD
 * @Description: 配置:用于thymeleaf和shiro标签配合使用
 * @Date: 2021/7/16
 * @Modify By:
 */
@Configuration
public class ThymeleafShiroConfiguration {

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

4、Shiro前端标签控制:

Example

<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

  <head>
    <title>thymeleaf-extras-shiro</title>
  </head>

  <body>
    <p shiro:guest="">Please <a href="login.html">login</a></p>
    <p shiro:authenticated="">
      Hello, <span shiro:principal=""></span>, how are you today?
    </p>
  </body>

</html>

Tags

The following examples show how to integrate the tags in your Thymeleaf templates. These are all implementations of the examples given in the JSP / GSP Tag Library Section of the Apache Shiro documentation.

Tags can be written in attribute or element notation:

Attribute
<p shiro:anyTag>
  Goodbye cruel World!
</p>
Element
<shiro:anyTag>
  <p>Hello World!</p>
</shiro:anyTag>

The guest tag
<p shiro:guest="">
  Please <a href="login.html">Login</a>
</p>
The user tag
<p shiro:user="">
  Welcome back John! Not John? Click <a href="login.html">here<a> to login.
</p>	
The authenticated tag
<a shiro:authenticated="" href="updateAccount.html">Update your contact information</a>
The notAuthenticated tag
<p shiro:notAuthenticated="">  Please <a href="login.html">login</a> in order to update your credit card information.</p>
The principal tag
<p>Hello, <span shiro:principal=""></span>, how are you today?</p>

or

<p>Hello, <shiro:principal/>, how are you today?</p>

Typed principal and principal property are also supported.

The hasRole tag
<a shiro:hasRole="administrator" href="admin.html">Administer the system</a>
The lacksRole tag
<p shiro:lacksRole="administrator">  Sorry, you are not allowed to administer the system.</p>
The hasAllRoles tag
<p shiro:hasAllRoles="developer, project manager">  You are a developer and a project manager.</p>
The hasAnyRoles tag
<p shiro:hasAnyRoles="developer, project manager, administrator">  You are a developer, project manager, or administrator.</p>
The hasPermission tag
<a shiro:hasPermission="user:create" href="createUser.html">Create a new User</a>
The lacksPermission tag
<p shiro:lacksPermission="user:delete">  Sorry, you are not allowed to delete user accounts.</p>
The hasAllPermissions tag
<p shiro:hasAllPermissions="user:create, user:delete">  You can create and delete users.</p>
The hasAnyPermissions tag
<p shiro:hasAnyPermissions="user:create, user:delete">  You can create or delete users.</p>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值