Skip to content

add Sa token ehcache plugin #811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions sa-token-demo/sa-token-demo-ehcache/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.pj</groupId>
<artifactId>sa-token-demo-ehcache</artifactId>

<!-- SpringBoot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.14</version>
<!-- <version>1.5.9.RELEASE</version> -->
<relativePath/>
</parent>

<!-- 定义 Sa-Token 版本号 -->
<properties>
<sa-token.version>1.44.0</sa-token.version>
</properties>

<dependencies>

<!-- SpringBoot依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</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>

<!-- Sa-Token 整合 Ehcache -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-ehcache</artifactId>
<version>${sa-token.version}</version>
</dependency>

<!-- @ConfigurationProperties -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.pj;

import cn.dev33.satoken.SaManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Sa-Token 整合 Ehcache 示例
* @author sanyang176
*
*/
@SpringBootApplication
public class SaTokenDemoApplication {

public static void main(String[] args) {
SpringApplication.run(SaTokenDemoApplication.class, args);
System.out.println("\n启动成功:Sa-Token配置如下:" + SaManager.getConfig());
System.out.println(SaManager.getSaTokenDao());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.pj.current;

import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
* 全局异常处理
*/
@RestControllerAdvice
public class GlobalException {

// 全局异常拦截(拦截项目中的所有异常)
@ExceptionHandler
public SaResult handlerException(Exception e) {
e.printStackTrace();
return SaResult.error(e.getMessage());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.pj.test;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;

/**
* 登录测试
* @author sanyang176
*
*/
@RestController
@RequestMapping("/acc/")
public class LoginController {

// 测试登录 ---- http://localhost:8081/acc/doLogin?name=zhang&pwd=123456
@RequestMapping("doLogin")
public SaResult doLogin(String name, String pwd) {
// 此处仅作模拟示例,真实项目需要从数据库中查询数据进行比对
if("zhang".equals(name) && "123456".equals(pwd)) {
StpUtil.login(10001);
return SaResult.ok("登录成功");
}
return SaResult.error("登录失败");
}

// 查询登录状态 ---- http://localhost:8081/acc/isLogin
@RequestMapping("isLogin")
public SaResult isLogin() {
return SaResult.ok("是否登录:" + StpUtil.isLogin());
}

// 查询 Token 信息 ---- http://localhost:8081/acc/tokenInfo
@RequestMapping("tokenInfo")
public SaResult tokenInfo() {
return SaResult.data(StpUtil.getTokenInfo());
}

// 测试注销 ---- http://localhost:8081/acc/logout
@RequestMapping("logout")
public SaResult logout() {
StpUtil.logout();
return SaResult.ok();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 端口
server:
port: 8081

# sa-token 配置
sa-token:
# token 名称 (同时也是 cookie 名称)
token-name: satoken
# token 有效期(单位:秒) 默认30天,-1 代表永久有效
timeout: 2592000
# token 最低活跃频率(单位:秒),如果 token 超过此时间没有访问系统就会被冻结,默认-1 代表不限制,永不冻结
active-timeout: -1
# 是否允许同一账号多地同时登录 (为 true 时允许一起登录, 为 false 时新登录挤掉旧登录)
is-concurrent: true
# 在多人登录同一账号时,是否共用一个 token (为 true 时所有登录共用一个 token, 为 false 时每次登录新建一个 token)
is-share: false
# token 风格(默认可取值:uuid、simple-uuid、random-32、random-64、random-128、tik)
token-style: uuid
# 是否输出操作日志
is-log: true


8 changes: 8 additions & 0 deletions sa-token-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<caffeine.version>3.2.0</caffeine.version>
<forest.version>1.6.4</forest.version>
<okhttps.version>4.1.0</okhttps.version>
<ehcache.version>3.10.0</ehcache.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -312,6 +313,13 @@
<version>${okhttps.version}</version>
</dependency>

<!-- Ehcache -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>${ehcache.version}</version>
</dependency>

<!-- sa-token 自身依赖 -->
<dependency>
<groupId>cn.dev33</groupId>
Expand Down
1 change: 1 addition & 0 deletions sa-token-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<module>sa-token-serializer-features</module>
<module>sa-token-forest</module>
<module>sa-token-okhttps</module>
<module>sa-token-ehcache</module>

<!-- SpringBoot 环境插件 -->
<module>sa-token-redis-template</module>
Expand Down
29 changes: 29 additions & 0 deletions sa-token-plugin/sa-token-ehcache/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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/xsd/maven-4.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-ehcache</name>
<artifactId>sa-token-ehcache</artifactId>
<description>sa-token integrate Ehcache</description>

<dependencies>
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-core</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cn.dev33.satoken.config;

import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheManagerBuilder;

/**
* Ehcache 配置类
*
* @author sanyang176
*/
public class EhcacheConfig {

/**
* 创建CacheManager
*
* @return CacheManager
*/
public static CacheManager CreateCacheManager() {
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build();
cacheManager.init();
return cacheManager;
}

/**
* 关闭CacheManager
*
* @param cacheManager /
*/
public static void CloseCacheManager(CacheManager cacheManager) {
cacheManager.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cn.dev33.satoken.dao;

import cn.dev33.satoken.config.EhcacheConfig;
import cn.dev33.satoken.dao.timedcache.SaMapPackage;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import java.time.Duration;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
* Map 包装类 (Ehcache 版)
*
* @author sanyang176
*/
public class SaMapPackageForEhcache<V> implements SaMapPackage<V> {

private final Cache<String, V> cache;
private static final CacheManager cacheManager = EhcacheConfig.CreateCacheManager();

/**
* 初始化缓存
* @param clazzType / 创建cache需要的具体泛型类型
* @param cacheName / 创建cache的名字
*/
public SaMapPackageForEhcache(Class<V> clazzType,String cacheName) {
this.cache = cacheManager.createCache(
cacheName,
CacheConfigurationBuilder.newCacheConfigurationBuilder(
String.class,
clazzType,
ResourcePoolsBuilder.heap(Integer.MAX_VALUE)
).withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(Long.MAX_VALUE)))
);
}

/**
* 获取缓存
*/
@Override
public Object getSource() {
return cache;
}

/**
* 读
*
* @param key /
* @return /
*/
@Override
public V get(String key) {
return cache.get(key);
}

/**
* 写
*
* @param key /
* @param value /
*/
@Override
public void put(String key, V value) {
cache.put(key, value);
}

/**
* 删
* @param key /
*/
@Override
public void remove(String key) { cache.remove(key); }

/**
* 所有 key
*/
@Override
public Set<String> keySet() {
return StreamSupport.stream(cache.spliterator(), false)
.map(Cache.Entry::getKey)
.collect(Collectors.toSet());
}

/**
* CacheManager 关闭
*/
public void closeCacheManager() {
EhcacheConfig.CloseCacheManager(cacheManager);
}
}
Loading