实现用户/角色服务.

This commit is contained in:
lijiahang
2023-07-17 18:23:06 +08:00
parent 9c3fd1ef6e
commit e7c1995dab
52 changed files with 635 additions and 1358 deletions

View File

@@ -41,11 +41,17 @@ public enum ErrorCode implements CodeInfo {
OTHER_DEVICE_LOGIN(700, "该账号于 {} 已在其他设备登陆 {}({})"),
USER_DISABLED(701, "当前用户已禁用"),
USER_LOCKED(702, "当前用户已被锁定"),
ROLE_PRESENT(703, "角色 [{}] 不存在"),
// -------------------- 自定义 - 通用 --------------------
NETWORK_FLUCTUATION(900, "当前环境网路波动"),
HTTP_API(901, "api 调用异常"),
HTTP_API_REQUEST_ERROR(901, "api 调用异常"),
IO_EXCEPTION(902, "网络异常"),
@@ -75,6 +81,8 @@ public enum ErrorCode implements CodeInfo {
DIABLED_ERROR(915, "数据已被禁用"),
UNSUPPOETED(916, "不支持此操作"),
;
ErrorCode(int code, String message) {

View File

@@ -13,7 +13,7 @@ public interface ErrorMessage {
String ID_MISSING = "id 不能为空";
String INVALID_PARAM = "参数错误";
String INVALID_PARAM = "参数验证失败";
String DATA_PRESENT = "数据已存在";
@@ -21,12 +21,20 @@ public interface ErrorMessage {
String CODE_PRESENT = "编码已存在";
String NICKNAME_PRESENT = "花名已存在";
String USERNAME_PRESENT = "用户名已存在";
String ROLE_ABSENT = "角色已存在";
String DATA_ABSENT = "数据不存在";
String USERNAME_PASSWORD_ERROR = "用户名或密码错误";
String MAX_LOGIN_FAILED = "登陆失败次数已上限";
String USER_ABSENT = "用户不存在";
String USER_DISABLED = "用户已被禁用";
String USER_LOCKED = "用户已被锁定";

View File

@@ -1,6 +1,7 @@
package com.orion.ops.framework.redis.config;
import com.orion.ops.framework.common.constant.AutoConfigureOrderConst;
import com.orion.ops.framework.redis.core.utils.RedisUtils;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
@@ -29,18 +30,19 @@ public class OrionRedisAutoConfiguration {
/**
* @param redisConnectionFactory factory
* @param <T> T
* @return RedisTemplate
*/
@Bean
public <T> RedisTemplate<String, T> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, T> redisTemplate = new RedisTemplate<>();
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
redisTemplate.setHashValueSerializer(RedisSerializer.string());
redisTemplate.afterPropertiesSet();
// 将其设置到 RedisUtils
RedisUtils.setRedisTemplate(redisTemplate);
return redisTemplate;
}

View File

@@ -1,5 +1,7 @@
package com.orion.ops.framework.redis.core.utils;
import com.alibaba.fastjson.JSON;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.utils.io.Streams;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisCallback;
@@ -8,6 +10,7 @@ import org.springframework.data.redis.core.ScanOptions;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
/**
* redis 工具类
@@ -18,32 +21,32 @@ import java.util.Set;
*/
public class RedisUtils {
private static RedisTemplate<String, String> redisTemplate;
private RedisUtils() {
}
/**
* 扫描key
* 扫描 key
*
* @param redisTemplate redisTemplate
* @param match 匹配值
* @param count 数量
* @param match 匹配值
* @param count 数量
* @return keys
*/
public static Set<String> scanKeys(RedisTemplate<?, ?> redisTemplate, String match, int count) {
return scanKeys(redisTemplate, ScanOptions.scanOptions()
public static Set<String> scanKeys(String match, int count) {
return scanKeys(ScanOptions.scanOptions()
.match(match)
.count(count)
.build());
}
/**
* 扫描key
* 扫描 key
*
* @param redisTemplate redisTemplate
* @param scanOptions scan
* @param scanOptions scan
* @return keys
*/
public static Set<String> scanKeys(RedisTemplate<?, ?> redisTemplate, ScanOptions scanOptions) {
public static Set<String> scanKeys(ScanOptions scanOptions) {
return redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
Set<String> keys = new HashSet<>();
Cursor<byte[]> cursor = connection.scan(scanOptions);
@@ -55,4 +58,76 @@ public class RedisUtils {
});
}
/**
* 获取并且设置 json
*
* @param define define
* @param type type
* @param processor processor
* @param params params
* @param <T> type
*/
public static <T> void processSetJson(CacheKeyDefine define, Class<T> type, Consumer<T> processor, Object... params) {
processSetJson(define.format(params), define, type, processor);
}
/**
* 获取并且设置 json
*
* @param key key
* @param define define
* @param type type
* @param processor processor
* @param <T> type
*/
public static <T> void processSetJson(String key, CacheKeyDefine define, Class<T> type, Consumer<T> processor) {
String value = redisTemplate.opsForValue().get(key);
if (value == null) {
return;
}
// 转换
T cache = JSON.parseObject(value, type);
// 执行处理逻辑
processor.accept(cache);
// 重新设置
setJson(key, define, cache);
}
/**
* 设置 json
*
* @param key key
* @param define define
* @param value value
*/
public static void setJson(String key, CacheKeyDefine define, Object value) {
if (define.getTimeout() == 0) {
// 不过期
redisTemplate.opsForValue().set(key, JSON.toJSONString(value));
} else {
// 过期
redisTemplate.opsForValue().set(key, JSON.toJSONString(value),
define.getTimeout(),
define.getUnit());
}
}
/**
* 设置过期时间
*
* @param key key
* @param define define
*/
public static void setExpire(String key, CacheKeyDefine define) {
if (define.getTimeout() != 0) {
// 设置过期时间
redisTemplate.expire(key, define.getTimeout(), define.getUnit());
}
}
public static void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
RedisUtils.redisTemplate = redisTemplate;
}
}

View File

@@ -206,7 +206,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(value = {HttpRequestException.class})
public HttpWrapper<?> httpApiRequestExceptionHandler(Exception ex) {
log.error("httpApiRequestExceptionHandler", ex);
return ErrorCode.HTTP_API.getWrapper();
return ErrorCode.HTTP_API_REQUEST_ERROR.getWrapper();
}
@ExceptionHandler(value = VcsException.class)