Skip to content

Commit

Permalink
add freemaker discover
Browse files Browse the repository at this point in the history
  • Loading branch information
v1ll4n committed Jul 5, 2024
1 parent 830e44b commit 757b222
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
15 changes: 15 additions & 0 deletions java-freemaker/java-freemaker-basic.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
desc(
title: 'checking [freemaker.Template.Process directly]',
type: audit
)

// write your SyntaxFlow Rule, like:
// DocumentBuilderFactory.newInstance()...parse(* #-> * as $source) as $sink; // find some call chain for parse
// check $sink then 'find sink point' else 'No Found' // if not found sink, the rule will stop here and report error
// alert $source // record $source

getTemplate().process() as $sink;

check $sink then "Found Freemaker Process Using" else "No Freemaker Process Simple";

// the template is generate by yak.ssa.syntaxflow command line
18 changes: 18 additions & 0 deletions java-freemaker/java-freemaker-create-process-env.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
desc(
title: 'checking [freemaker.Template.CreateProcessingEnvironment]',
type: audit
)

// write your SyntaxFlow Rule, like:
// DocumentBuilderFactory.newInstance()...parse(* #-> * as $source) as $sink; // find some call chain for parse
// check $sink then 'find sink point' else 'No Found' // if not found sink, the rule will stop here and report error
// alert $source // record $source

getTemplate().createProcessingEnvironment() as $env;
$env.process() as $sink;
$env.invoke() as $sink;

check $sink then "Found Freemaker CreateProcessingEnvironment invoke or process";
alert $env;

// the template is generate by yak.ssa.syntaxflow command line
35 changes: 35 additions & 0 deletions java-freemaker/sample/BasicUseCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import freemarker.template.*;

import java.io.*;
import java.util.*;

public class FreemarkerExample {
public static void main(String[] args) {
// 配置 Freemarker
Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
try {
cfg.setDirectoryForTemplateLoading(new File("src/main/resources/templates"));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
cfg.setWrapUncheckedExceptions(true);

// 加载模板
Template template = cfg.getTemplate("welcome.ftl");

// 数据模型
Map<String, Object> templateData = new HashMap<>();
templateData.put("user", "John Doe");

// 渲染模板
Writer out = new StringWriter();
template.process(templateData, out);

// 输出渲染后的文本
System.out.println(out.toString());

} catch (IOException | TemplateException e) {
e.printStackTrace();
}
}
}
37 changes: 37 additions & 0 deletions java-freemaker/sample/CreateProcEnv.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import freemarker.template.*;
import java.io.*;
import java.util.*;

public class FreemarkerDemo {
public static void main(String[] args) {
// 创建和配置 Freemarker
Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
try {
cfg.setDirectoryForTemplateLoading(new File("src/main/resources/templates"));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
cfg.setWrapUncheckedExceptions(true);

// 加载模板
Template template = cfg.getTemplate("helloTemplate.ftl");

// 准备数据模型
Map<String, Object> templateData = new HashMap<>();
templateData.put("name", "Freemarker User");

// 准备输出流
Writer out = new StringWriter();

// 创建 Environment 对象并处理模板
Environment env = template.createProcessingEnvironment(templateData, out);
env.process();

// 输出结果
System.out.println(out.toString());

} catch (IOException | TemplateException e) {
e.printStackTrace();
}
}
}
2 changes: 1 addition & 1 deletion java-security-config/java-spring-websecurity-from-login.sf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $configEntry ... authorizeRequests().../(?i).*matcher.*/()...authenticated() as

$configEntry ... formLogin()...loginPage(*?{opcode: const} as $loginPage ) as $sink;

check $sink then "Found fromLogin().loginPage()" else "Not Found login page";
check $sink then "Found formLogin().loginPage()" else "Not Found login page";
alert $loginPage;
alert $auth;

Expand Down

0 comments on commit 757b222

Please sign in to comment.