|
|
@ -1,5 +1,8 @@
|
|
|
|
package com.lyr.framework.config;
|
|
|
|
package com.lyr.framework.config;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
|
|
|
import com.lyr.common.core.domain.model.LoginUser;
|
|
|
|
|
|
|
|
import com.lyr.common.utils.StringUtils;
|
|
|
|
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
|
|
|
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
|
|
|
import org.springframework.cache.annotation.EnableCaching;
|
|
|
|
import org.springframework.cache.annotation.EnableCaching;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
@ -7,6 +10,7 @@ import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
|
|
|
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
|
|
|
|
|
|
|
import org.springframework.data.redis.serializer.SerializationException;
|
|
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -49,17 +53,44 @@ public class RedisConfig extends CachingConfigurerSupport {
|
|
|
|
* 限流脚本
|
|
|
|
* 限流脚本
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private String limitScriptText() {
|
|
|
|
private String limitScriptText() {
|
|
|
|
return "local key = KEYS[1]\n" +
|
|
|
|
return "local key = KEYS[1]\n" + "local count = tonumber(ARGV[1])\n" + "local time = tonumber(ARGV[2])\n" + "local current = redis.call('get', key);\n" + "if current and tonumber(current) > count then\n" + " return tonumber(current);\n" + "end\n" + "current = redis.call('incr', key)\n" + "if tonumber(current) == 1 then\n" + " redis.call('expire', key, time)\n" + "end\n" + "return tonumber(current);";
|
|
|
|
"local count = tonumber(ARGV[1])\n" +
|
|
|
|
}
|
|
|
|
"local time = tonumber(ARGV[2])\n" +
|
|
|
|
|
|
|
|
"local current = redis.call('get', key);\n" +
|
|
|
|
|
|
|
|
"if current and tonumber(current) > count then\n" +
|
|
|
|
public class StringRedisSerializerEx extends StringRedisSerializer {
|
|
|
|
" return tonumber(current);\n" +
|
|
|
|
|
|
|
|
"end\n" +
|
|
|
|
|
|
|
|
"current = redis.call('incr', key)\n" +
|
|
|
|
private String DEFAULT_PREFIX = "sync_data:";
|
|
|
|
"if tonumber(current) == 1 then\n" +
|
|
|
|
|
|
|
|
" redis.call('expire', key, time)\n" +
|
|
|
|
@Override
|
|
|
|
"end\n" +
|
|
|
|
public byte[] serialize(String string) throws SerializationException {
|
|
|
|
"return tonumber(current);";
|
|
|
|
if (string == null) {
|
|
|
|
|
|
|
|
return new byte[0];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 添加前缀
|
|
|
|
|
|
|
|
String realKey = DEFAULT_PREFIX + string;
|
|
|
|
|
|
|
|
return super.serialize(realKey);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public String deserialize(byte[] bytes) throws SerializationException {
|
|
|
|
|
|
|
|
String s = bytes == null ? null : new String(bytes);
|
|
|
|
|
|
|
|
if (StringUtils.isBlank(s)) {
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 移除前缀
|
|
|
|
|
|
|
|
if (s.startsWith(DEFAULT_PREFIX)) {
|
|
|
|
|
|
|
|
return s.substring(DEFAULT_PREFIX.length());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
String string = "{\"@type\":\"com.lyr.common.core.domain.model.LoginUser\",\"browser\":\"Chrome 13\",\"deptId\":103L,\"expireTime\":1730195709559,\"ipaddr\":\"127.0.0.1\",\"loginLocation\":\"内网IP\",\"loginTime\":1730193909559,\"os\":\"Windows 10\",\"permissions\":Set[\"*:*:*\"],\"token\":\"6636edfa-3090-4466-a1c9-965c171f87a3\",\"user\":{\"admin\":true,\"createBy\":\"admin\",\"createTime\":\"2024-10-29 14:37:51\",\"delFlag\":\"0\",\"dept\":{\"ancestors\":\"0,100,101\",\"children\":[],\"deptId\":103L,\"deptName\":\"研发部门\",\"leader\":\"若依\",\"orderNum\":1,\"params\":{\"@type\":\"java.util.HashMap\"},\"parentId\":101L,\"status\":\"0\"},\"deptId\":103L,\"email\":\"ry@163.com\",\"loginDate\":\"2024-10-29 17:23:50\",\"loginIp\":\"127.0.0.1\",\"nickName\":\"若依\",\"params\":{\"@type\":\"java.util.HashMap\"},\"password\":\"$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2\",\"phonenumber\":\"15888888888\",\"remark\":\"管理员\",\"roles\":[{\"admin\":true,\"dataScope\":\"1\",\"deptCheckStrictly\":false,\"flag\":false,\"menuCheckStrictly\":false,\"params\":{\"@type\":\"java.util.HashMap\"},\"roleId\":1L,\"roleKey\":\"admin\",\"roleName\":\"超级管理员\",\"roleSort\":1,\"status\":\"0\"}],\"sex\":\"1\",\"status\":\"0\",\"userId\":1L,\"userName\":\"admin\"},\"userId\":1L,\"username\":\"admin\"}";
|
|
|
|
|
|
|
|
LoginUser jsonObject = JSONObject.parseObject(string, LoginUser.class);
|
|
|
|
|
|
|
|
System.out.println(jsonObject.toString());
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|