feat(plugin): 新增 freemarker 集成插件。 fix: #651

This commit is contained in:
click33
2025-01-08 07:11:08 +08:00
parent 6c4cdf514e
commit 3192717c0f
18 changed files with 633 additions and 4 deletions

View File

@@ -28,6 +28,7 @@
<module>sa-token-alone-redis</module>
<module>sa-token-hutool-timed-cache</module>
<module>sa-token-dialect-thymeleaf</module>
<module>sa-token-freemarker</module>
<module>sa-token-sso</module>
<module>sa-token-oauth2</module>
<module>sa-token-quick-login</module>

View File

@@ -0,0 +1,33 @@
<?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>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<packaging>jar</packaging>
<name>sa-token-freemarker</name>
<artifactId>sa-token-freemarker</artifactId>
<description>sa-token-freemarker</description>
<dependencies>
<!-- sa-token-core -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-core</artifactId>
</dependency>
<!-- freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.freemarker.dialect;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import java.io.IOException;
import java.util.Map;
import java.util.function.Function;
/**
* Sa-Token Freemarker 标签模板指令模型
*
* @author click33
* @since 1.40.0
*/
public class SaTokenTemplateDirectiveModel implements TemplateDirectiveModel {
/**
* 使用标签指令模板时,指定值的属性名
*/
String attrName;
/**
* 断言函数,返回 true 时标签内容显示,返回 false 时标签内容不显示
*/
Function <String, Boolean> fun;
public SaTokenTemplateDirectiveModel(String attrName, Function <String, Boolean> fun) {
this.attrName = attrName;
this.fun = fun;
}
@Override
public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody)
throws TemplateException, IOException {
// 获取 value
Object obj = map.get(attrName);
String value = obj == null ? null : obj.toString();
// 使用断言函数判断是否显示标签内容
if(this.fun.apply(value)) {
templateDirectiveBody.render(environment.getOut());
}
}
}

View File

@@ -0,0 +1,98 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.freemarker.dialect;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaFoxUtil;
import freemarker.template.SimpleHash;
import java.util.List;
/**
* Sa-Token Freemarker 标签模板模型
*
* @author click33
* @since 1.40.0
*/
public class SaTokenTemplateModel extends SimpleHash {
/**
* 默认值属性名
*/
public static final String DEFAULT_ATTR_NAME = "value";
/**
* 底层使用的 StpLogic
*/
public StpLogic stpLogic;
/**
* 使用默认参数注册标签模板模型
*/
public SaTokenTemplateModel() {
this(DEFAULT_ATTR_NAME, StpUtil.stpLogic);
}
/**
* 构造标签模板模型,使用自定义参数
*
* @param stpLogic 使用的 StpLogic 对象
*/
public SaTokenTemplateModel(StpLogic stpLogic) {
this(DEFAULT_ATTR_NAME, stpLogic);
}
/**
* 构造标签模板模型,使用自定义参数
*
* @param attrName 属性名
* @param stpLogic 使用的 StpLogic 对象
*/
public SaTokenTemplateModel(String attrName, StpLogic stpLogic) {
this.stpLogic = stpLogic;
// 登录判断
put("login", new SaTokenTemplateDirectiveModel(attrName, value -> stpLogic.isLogin()));
put("notLogin", new SaTokenTemplateDirectiveModel(attrName, value -> ! stpLogic.isLogin()));
// 角色判断
put("hasRole", new SaTokenTemplateDirectiveModel(attrName, value -> stpLogic.hasRole(value)));
put("hasRoleAnd", new SaTokenTemplateDirectiveModel(attrName, value -> stpLogic.hasRoleAnd(toArray(value))));
put("hasRoleOr", new SaTokenTemplateDirectiveModel(attrName, value -> stpLogic.hasRoleOr(toArray(value))));
put("notRole", new SaTokenTemplateDirectiveModel(attrName, value -> ! stpLogic.hasRole(value)));
put("lackRole", new SaTokenTemplateDirectiveModel(attrName, value -> ! stpLogic.hasRole(value)));
// 权限判断
put("hasPermission", new SaTokenTemplateDirectiveModel(attrName, value -> stpLogic.hasPermission(value)));
put("hasPermissionAnd", new SaTokenTemplateDirectiveModel(attrName, value -> stpLogic.hasPermissionAnd(toArray(value))));
put("hasPermissionOr", new SaTokenTemplateDirectiveModel(attrName, value -> stpLogic.hasPermissionOr(toArray(value))));
put("notPermission", new SaTokenTemplateDirectiveModel(attrName, value -> ! stpLogic.hasPermission(value)));
put("lackPermission", new SaTokenTemplateDirectiveModel(attrName, value -> ! stpLogic.hasPermission(value)));
}
/**
* String 转 Array
* @param str 字符串
* @return 数组
*/
public String[] toArray(String str) {
List<String> list = SaFoxUtil.convertStringToList(str);
return list.toArray(new String[0]);
}
}