前言

使用thymeleaf自定义标签,环境:springboot 2.3.7 + thymeleaf 3.0.11(2021-01-14最新版)
由于使用shiro,我们需要与thymeleaf整合一些标签方便更好操作,以isAuthenticated为例。
ps:虽然有一个开源的框架,但是很久没维护了,还不如照着官网来自定义。

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>

参考官方文档:https://www.thymeleaf.org/doc/tutorials/3.0/extendingthymeleaf.html
官方demo:https://github.com/thymeleaf/thymeleafexamples-extrathyme

一、创建方言

前缀仍然使用th

package com.example.shirodemo.config.thymeleaf;

import java.util.HashSet;
import java.util.Set;
import org.thymeleaf.dialect.AbstractProcessorDialect;
import org.thymeleaf.processor.IProcessor;
import org.thymeleaf.standard.StandardDialect; /**
* @author 绫小路
* @date 2021/1/14 22:43
* @description
*/
public class ShiroDialect extends AbstractProcessorDialect { private static final String DIALECT_NAME = "shiro方言"; public ShiroDialect() {
// We will set this dialect the same "dialect processor" precedence as
// the Standard Dialect, so that processor executions can interleave.
super(DIALECT_NAME, "th", StandardDialect.PROCESSOR_PRECEDENCE);
} @Override
public Set<IProcessor> getProcessors(String s) {
final Set<IProcessor> processors = new HashSet<>();
processors.add(new AuthTagProcessor(s));
return processors;
}
}

二、标签处理

package com.example.shirodemo.config.thymeleaf;

import static org.thymeleaf.standard.processor.StandardWithTagProcessor.PRECEDENCE;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractAttributeTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode; /**
* @author 绫小路
* @date 2021/1/14 23:02
* @description
*/
public class AuthTagProcessor extends AbstractAttributeTagProcessor { private static final String SHIRO = "shiro"; public AuthTagProcessor(String dialectPrefix) {
super(
TemplateMode.HTML, // This processor will apply only to HTML mode
dialectPrefix, // Prefix to be applied to name for matching 前缀
null, // No tag name: match any tag name
false, // No prefix to be applied to tag name
SHIRO, // Name of the attribute that will be matched
true, // Apply dialect prefix to attribute name
PRECEDENCE, // Precedence (inside dialect's own precedence)优先级
true); // Remove the matched attribute afterwards
// super( TemplateMode.HTML, dialectPrefix, elementName, prefixElementName, isAuthenticated, prefixAttributeName, precedence, removeAttribute);
} @Override
protected void doProcess(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName, String attributeValue,
IElementTagStructureHandler structureHandler) {
Subject subject = SecurityUtils.getSubject();
if ("isAuthenticated".equals(attributeValue)) {//判断是否登录
if (!subject.isAuthenticated()) {
structureHandler.removeElement();//未登录时将元素移除 例如<a href="/admin" th:shiro="isAuthenticated">admin</a>
}
}
}
}

三、配置

直接交给spring容器管理即可

  @Bean
public ShiroDialect shiroDialect() {
return new ShiroDialect();//添加自定义的标签
}

四、使用

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
首页 <a href="/login">登录</a>
<br>
<a href="/logout" th:shiro="isAuthenticated">注销</a>
<br>
<a href="/admin" th:shiro="isAuthenticated">admin</a>
</body>
</html>

未登陆时

登陆后:

thymeleaf自定义标签的更多相关文章

  1. spring thymeleaf 自定义标签

    概述 thymeleaf2.1.5自定义标签及自定义属性案例,类似于JSP中的自定义JSTL标签 详细 代码下载:http://www.demodashi.com/demo/10495.html 一. ...

  2. thymeleaf自定义标签方言处理

    项目背景:springboot+thymeleaf thymeleaf两种方式处理自定义标签:AbstractAttributeTagProcessor 和 AbstractElementTagPro ...

  3. thymeleaf教程-springboot项目中实现thymeleaf自定义标签

    转载: http://www.9191boke.com/466119140.html    91博客网 开始: 在使用thymeleaf的过程中有时候需要公共部分渲染页面,这个时候使用自定义标签实现自 ...

  4. Java Spring Boot VS .NetCore (十一)自定义标签 Java Tag Freemarker VS .NetCore Tag TagHelper

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  5. [JSP]自定义标签库taglib

    自定义标签的步骤 自定义标签的步骤大概有三步: 1.继承javax.servlet.jsp.tagext.*下提供的几个标签类,如Tag.TagSupport.BodyTagSupport.Simpl ...

  6. [Java] JSP笔记 - 自定义标签

    自定义标签的创建步骤: 自定义标签的四大功能: 自定义标签的类结构: 在 1.0 中呢, 可以将 <body-content> 的值设置为 JSP, 2.0中则不允许在自定义标签体中出现j ...

  7. thinkphp自定义标签库

    thinkphp ~ php中 的类, 的成员变量, 本身是没有类型说明的, 那么我怎么知道它的类型呢? 或初始值呢? 通常在类定义中, 如果能给一个初始值的(对于已知简单类型的),最好给一个初始值, ...

  8. 12 自定义标签/JSTL标签库/web国际化/java web之设计模式和案例

    EL应用      自定义一个标签,实现两个字符串的相加 1回顾      1.1servlet生命周期           init(ServletConfig)           service ...

  9. EL函数以及自定义标签的应用

    一.EL函数(调用普通类的静态方法) 编写步骤(自定义EL函数的编写步骤即自定义标签的编写步骤): ①编写一个普通的java类,提供一个静态方法,功能自定,例如下: package cn.wzbril ...

  10. JSTL 自定义标签

    编写描述标签的tld文件,把这个文件放到web-inf/目录下,才能在jsp页面上调用自定义的标签 package test.yz; import java.io.IOException; impor ...

随机推荐

  1. ​Python爬虫IP代理池的建立和使用

    写在前面建立Python爬虫IP代理池可以提高爬虫的稳定性和效率,可以有效避免IP被封锁或限制访问等问题. 下面是建立Python爬虫IP代理池的详细步骤和代码实现: 1. 获取代理IP我们可以从一些 ...

  2. OKR 是什么?

    OKR OKR 是什么? OKR(Objectives and Key Results)目标与关键结果管理法,起源于英特尔,后在谷歌发扬光大. OKR 是一套协助组织进行目标管理的工具和方法,旨在促进 ...

  3. docker入门加实战—从部署MySQL入门docker

    docker入门加实战-从部署MySQL入门docker docker部署MySQL 输入如下命令: docker run -d \ --name mysql \ -p 3306:3306 \ -e ...

  4. Java虚拟机(JVM):第五幕:自动内存管理 - HotSpot算法细节以及低延迟垃圾收集器

    一.HotSpot算法细节 1.根节点枚举:所有的收集器在根节点枚举的时候,必须暂停用户线程,同时要保证一致性的快照中得以进行.一致性:整个枚举期间执行子系统看起来就像是冻结在某一个时间点上,不会出现 ...

  5. 记一次Redis Cluster Pipeline导致的死锁问题

    作者:vivo 互联网服务器团队- Li Gang 本文介绍了一次排查Dubbo线程池耗尽问题的过程.通过查看Dubbo线程状态.分析Jedis连接池获取连接的源码.排查死锁条件等方面,最终确认是因为 ...

  6. ansible-playbook应用

    ansible-playbook剧本: 如上使用Ad-hoc方式点对点命令执行,可以管理远程主机,如果服务器数量很多,配置信息比较多,还可以利用ansible playbook编写剧本.从而以非常简单 ...

  7. undefined reference to vtable for问题解决(QT)

    主要在运行时出现 原因是在自定义类使用信号与槽,在创建文件时,未继承QObject类并且没有添加Q_OBJECT: 解决: 在需要的类中,添加Q_OBJECT,继承QObject类. 然后使用QTCr ...

  8. Isito 入门(九):安全认证

    本教程已加入 Istio 系列:https://istio.whuanle.cn 目录 7,认证 Peer Authentication PeerAuthentication 的定义 实验 Reque ...

  9. IDEA在Debug模式下修改Java类,不小心关闭 Reload Changed Classes for AppArrowWebApplication 框提示之后的处理

    问题描述:Springboot maven 聚合项目里面,经常要启动多个服务. 当我们修改其中一个服务的时候,debug 启动时 idea 就会提示是否需要重新编译修改的内容,弹窗让你选择reload ...

  10. 二。docker安装mysql 并配置

    1.docker安装mysql 1.1使用docker拉取mysql的镜像 docker pull mysql:5.7 1.2通过镜像启动 docker run -p 3306:3306 --name ...