新增 Thymeleaf 标签方言插件

This commit is contained in:
click33
2021-10-01 22:54:56 +08:00
parent f260f6028a
commit d107b6b341
23 changed files with 723 additions and 41 deletions

View File

@@ -0,0 +1,12 @@
target/
node_modules/
bin/
.settings/
unpackage/
.classpath
.project
.factorypath
.idea/

View File

@@ -0,0 +1,41 @@
<?xml version='1.0' encoding='utf-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-plugin</artifactId>
<version>1.26.0</version>
</parent>
<packaging>jar</packaging>
<name>sa-token-dialect-thymeleaf</name>
<artifactId>sa-token-dialect-thymeleaf</artifactId>
<description>sa-token-dialect-thymeleaf</description>
<dependencies>
<!-- sa-token-spring-boot-starter -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-core</artifactId>
<version>${sa-token-version}</version>
</dependency>
<!-- thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
<optional>true</optional>
</dependency>
<!-- spring-boot-configuration -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.0.0.RELEASE</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,82 @@
package cn.dev33.satoken.thymeleaf.dialect;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.thymeleaf.dialect.AbstractProcessorDialect;
import org.thymeleaf.processor.IProcessor;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaFoxUtil;
/**
* Sa-Token 集成 Thymeleaf 标签方言
*
* @author kong
*
*/
public class SaTokenDialect extends AbstractProcessorDialect {
/**
* 底层使用的 StpLogic
*/
public StpLogic stpLogic;
/**
* 使用默认参数注册方言
*/
public SaTokenDialect() {
this("sa", 1000, StpUtil.stpLogic);
}
/**
* 构造方言对象,使用
* @param name 方言名称
* @param recedence 优先级
* @param stpLogic 使用的 StpLogic 对象
*/
public SaTokenDialect(String name, int recedence, StpLogic stpLogic) {
// 名称、前缀、优先级
super(name, name, recedence);
this.stpLogic = stpLogic;
}
/**
* 返回所有方言处理器
*/
@Override
public Set<IProcessor> getProcessors(String prefix) {
return new HashSet<IProcessor>(Arrays.asList(
// 登录判断
new SaTokenTagProcessor(prefix, "login", value -> stpLogic.isLogin()),
new SaTokenTagProcessor(prefix, "notLogin", value -> stpLogic.isLogin() == false),
// 角色判断
new SaTokenTagProcessor(prefix, "hasRole", value -> stpLogic.hasRole(value)),
new SaTokenTagProcessor(prefix, "hasRoleOr", value -> stpLogic.hasRoleOr(toArray(value))),
new SaTokenTagProcessor(prefix, "hasRoleAnd", value -> stpLogic.hasRoleAnd(toArray(value))),
new SaTokenTagProcessor(prefix, "lackRole", value -> stpLogic.hasRole(value) == false),
// 权限判断
new SaTokenTagProcessor(prefix, "hasPermission", value -> stpLogic.hasPermission(value)),
new SaTokenTagProcessor(prefix, "hasPermissionOr", value -> stpLogic.hasPermissionOr(toArray(value))),
new SaTokenTagProcessor(prefix, "hasPermissionAnd", value -> stpLogic.hasPermissionAnd(toArray(value))),
new SaTokenTagProcessor(prefix, "lackPermission", value -> stpLogic.hasPermission(value) == false)
));
}
/**
* String 转 Array
* @param str 字符串
* @return 数组
*/
public String[] toArray(String str) {
List<String> list = SaFoxUtil.convertStringToList(str);
return list.toArray(new String[list.size()]);
}
}

View File

@@ -0,0 +1,45 @@
package cn.dev33.satoken.thymeleaf.dialect;
import java.util.function.Function;
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;
/**
* 封装 Sa-Token 标签方言处理器
* @author kong
*
*/
public class SaTokenTagProcessor extends AbstractAttributeTagProcessor {
Function <String, Boolean> fun;
public SaTokenTagProcessor(final String dialectPrefix, String arrtName, Function <String, Boolean> fun) {
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
arrtName, // Name of the attribute that will be matched
true, // Apply dialect prefix to attribute name
10000, // Precedence (inside dialect's own precedence)
true); // Remove the matched attribute afterwards
this.fun = fun;
}
@Override
protected void doProcess(
final ITemplateContext context, final IProcessableElementTag tag,
final AttributeName attributeName, final String attributeValue,
final IElementTagStructureHandler structureHandler) {
// 执行表达式返回值为false则删除这个标签
if(this.fun.apply(attributeValue) == false) {
structureHandler.removeElement();
};
}
}