前言

使用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. Use Closures Not Enumerations

    http://c2.com/  Use Closures Not Enumerations I was really disappointed when this turned out not to ...

  2. Solr Shiro Log4j2 命令执行--文件读取--反序列化--身份权限绕过--命令执行

    Solr Shiro Log4j2 命令执行--文件读取--反序列化--身份权限绕过--命令执行 solr 远程命令执行 (CVE-2019-17558) 漏洞简介 Apache Velocity是一 ...

  3. 题解 hdu 1269 迷宫城堡

    找点图论练习题写,发现hdu又寄了,那就发到blog里吧. 思路:tarjan缩点判断DAG中点数是否为1.若是,则该图为强连通图. //produced by miya555 //stupid mi ...

  4. nodejs修改npm包安装位置

    适用于非个人电脑.便携使用 npm config set cache D:\nodejs\node_cache npm config set prefix D:\nodejs npm config s ...

  5. OpenCv4.6.0交叉编译ARM(aarch64)平台库

    1.下载交叉编译工具:gcc-linaro-6.3.1-2017.02-x86_64_aarch64-linux-gnu 2.opencv官网下载opencv4.6.0源码,opencv官网下载ope ...

  6. mac os 升级到13后,系统免密失败

    # sudo vim /etc/ssh/ssh_config # 添加以下内容 PubkeyAcceptedKeyTypes +ssh-rsa

  7. liunx远程管理常用命令笔记

    1,关机/重启 shutdown -r now : 立刻重启的命令 2,查看或配置网卡信息 2.1  网卡和 IP 地址 2.2  ifconfig 用了管道和grep 查找到 IP 地址 2.3 p ...

  8. 前端JavaScript编码规范 和react编码规范

    JavaScript编码规范 点击链接查看:https://github.com/ecomfe/spec/blob/master/javascript-style-guide.md 前端React编码 ...

  9. 比较并交换(compare and swap, CAS)

    比较并交换(compare and swap, CAS),是原子操作的一种,可用于在多线程编程中实现不被打断的数据交换操作,从而避免多线程同时改写某一数据时由于执行顺序不确定性以及中断的不可预知性产生 ...

  10. JUC并发编程学习(十三)ForkJoin

    ForkJoin 什么是ForkJoin ForkJoin在JDK1.7,并发执行任务!大数据量时提高效率. 大数据:Map Reduce(把大任务拆分成小任务) ForkJoin特点:工作窃取 为什 ...