新增集成 beetl demo 示例

This commit is contained in:
click33 2024-04-13 11:07:07 +08:00
parent 7f6b495578
commit f764f33b5d
9 changed files with 269 additions and 4 deletions

View File

@ -18,6 +18,7 @@ cd sa-token-demo-springboot-redis & call mvn clean & cd ..
cd sa-token-demo-springboot-redisson & call mvn clean & cd ..
cd sa-token-demo-test & call mvn clean & cd ..
cd sa-token-demo-thymeleaf & call mvn clean & cd ..
cd sa-token-demo-beetl & call mvn clean & cd ..
cd sa-token-demo-webflux & call mvn clean & cd ..
cd sa-token-demo-webflux-springboot3 & call mvn clean & cd ..
cd sa-token-demo-websocket & call mvn clean & cd ..
@ -44,10 +45,10 @@ cd sa-token-demo-oauth2-client & call mvn clean & cd ..
cd sa-token-demo-oauth2-server & call mvn clean & cd ..
cd ..
cd sa-token-demo-cross
cd sa-token-demo-cross-header-server & call mvn clean & cd ..
cd sa-token-demo-cross-cookie-server & call mvn clean & cd ..
cd ..
:: cd sa-token-demo-cross
:: cd sa-token-demo-cross-header-server & call mvn clean & cd ..
:: cd sa-token-demo-cross-cookie-server & call mvn clean & cd ..
:: cd ..
cd sa-token-demo-dubbo
cd sa-token-demo-dubbo-consumer & call mvn clean & cd ..

View File

@ -0,0 +1,61 @@
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-demo-beetl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- SpringBoot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.14</version>
<relativePath/>
</parent>
<!-- 定义 Sa-Token 版本号 -->
<properties>
<sa-token.version>1.37.0</sa-token.version>
</properties>
<dependencies>
<!-- springboot依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Beetl 视图引擎 -->
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl-framework-starter</artifactId>
<version>1.2.40.Beetl.RELEASE</version>
</dependency>
<!-- Sa-Token 权限认证, 在线文档https://sa-token.cc/ -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<version>${sa-token.version}</version>
</dependency>
<!-- 热刷新 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>provided</scope>
</dependency>
<!-- @ConfigurationProperties -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,17 @@
package com.pj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import cn.dev33.satoken.SaManager;
@SpringBootApplication
public class SaTokenBeetlDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SaTokenBeetlDemoApplication.class, args);
System.out.println("\n启动成功Sa-Token 配置如下:" + SaManager.getConfig());
System.out.println("\n测试访问http://localhost:8081/");
}
}

View File

@ -0,0 +1,25 @@
package com.pj.satoken;
import cn.dev33.satoken.stp.StpUtil;
import com.ibeetl.starter.BeetlTemplateCustomize;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* [Sa-Token 权限认证] 配置类
* @author click33
*
*/
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
// Beetl 视图引擎注册自定义函数
// 通过 stp.xxx() 调用 StpUtil.stpLogic 对象上所有 public 方法
@Bean
public BeetlTemplateCustomize beetlTemplateCustomize(){
return groupTemplate -> groupTemplate.registerFunctionPackage("stp", StpUtil.stpLogic);
}
}

View File

@ -0,0 +1,44 @@
package com.pj.satoken;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
import cn.dev33.satoken.stp.StpInterface;
/**
* 自定义权限验证接口扩展
*/
@Component // 打开此注解保证此类被springboot扫描即可完成sa-token的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {
/**
* 返回一个账号所拥有的权限码集合
*/
@Override
public List<String> getPermissionList(Object loginId, String loginType) {
// 本list仅做模拟实际项目中要根据具体业务逻辑来查询权限
List<String> list = new ArrayList<String>();
list.add("101");
list.add("user-add");
list.add("user-delete");
list.add("user-update");
list.add("user-get");
list.add("article-get");
return list;
}
/**
* 返回一个账号所拥有的角色标识集合
*/
@Override
public List<String> getRoleList(Object loginId, String loginType) {
// 本list仅做模拟实际项目中要根据具体业务逻辑来查询角色
List<String> list = new ArrayList<String>();
list.add("admin");
list.add("super-admin");
return list;
}
}

View File

@ -0,0 +1,23 @@
package com.pj.test;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 全局异常处理
*/
@RestControllerAdvice
public class GlobalException {
// 全局异常拦截拦截项目中的所有异常
@ExceptionHandler
public SaResult handlerException(Exception e, HttpServletRequest request, HttpServletResponse response) {
e.printStackTrace();
return SaResult.error(e.getMessage());
}
}

View File

@ -0,0 +1,39 @@
package com.pj.test;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
/**
* 测试 Controller
* @author click33
*
*/
@RestController
public class TestController {
// 首页
@RequestMapping("/")
public Object index() {
return new ModelAndView("index.btl");
}
// 登录
@RequestMapping("login")
public SaResult login(@RequestParam(defaultValue="10001") String id) {
StpUtil.login(id);
StpUtil.getSession().set("name", "zhangsan");
return SaResult.ok();
}
// 注销
@RequestMapping("logout")
public SaResult logout() {
StpUtil.logout();
return SaResult.ok();
}
}

View File

@ -0,0 +1,5 @@
# 端口
server:
port: 8081

View File

@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<title>Sa-Token 集成 Beetl 示例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
</head>
<body>
<div class="view-box" style="padding: 30px;">
<h2>Sa-Token 集成 Beetl —— 测试页面</h2>
<p>当前是否登录:${stp.isLogin()}</p>
<p>
<span>操作:</span>
<a href="login" target="_blank">登录</a>
<a href="logout" target="_blank">注销</a>
</p>
<p>
登录之后才能显示:
<% if(stp.isLogin()){ %>
value
<%}%>
</p>
<p>
不登录才能显示:
<% if(!stp.isLogin()){ %>
value
<%}%>
</p>
<p>具有角色 admin 才能显示:<% if(stp.hasRole("admin")){ %> value <% } %></p>
<p>同时具备多个角色才能显示:<% if(stp.hasRoleAnd("admin", "ceo", "cto")){ %> value <% } %></p>
<p>只要具有其中一个角色就能显示:<% if(stp.hasRoleOr("admin", "ceo", "cto")){ %> value <% } %></p>
<p>不具有角色 admin 才能显示:<% if(!stp.hasRole("admin")){ %> value <% } %></p>
<p>具有权限 user-add 才能显示:<% if(stp.hasPermission("user-add")){ %> value <% } %></p>
<p>同时具备多个权限才能显示:<% if(stp.hasPermissionAnd("user-add", "user-delete", "user-get")){ %> value <% } %></p>
<p>只要具有其中一个权限就能显示:<% if(stp.hasPermissionOr("user-add", "user-delete", "user-get")){ %> value <% } %></p>
<p>不具有权限 user-add 才能显示:<% if(!stp.hasPermission("user-add")){ %> value <% } %></p>
<% if(stp.isLogin()){ %>
<p>
从SaSession中取值${stp.getSession()["name"]} <br>
或:${stp.getSession().name}
</p>
<%}%>
</div>
</body>
</html>