add
This commit is contained in:
@ -3,9 +3,9 @@ qiaoba:
|
||||
datasource:
|
||||
master:
|
||||
driver: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://121.5.136.69:3306/qiaoba-boot?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&allowMultiQueries=true
|
||||
url: jdbc:mysql://localhost:3306/qiaoba-boot?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&allowMultiQueries=true
|
||||
username: root
|
||||
password: LpYN7LUoL?l0OSpR2
|
||||
password: root
|
||||
pool:
|
||||
init: 5 #连接池初始化大小
|
||||
min: 10 #最小空闲连接数
|
||||
|
@ -1,5 +1,8 @@
|
||||
package com.qiaoba.auth.constants;
|
||||
|
||||
import com.qiaoba.common.base.constants.BaseConstant;
|
||||
import com.qiaoba.common.base.constants.ConfigConstant;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@ -19,14 +22,15 @@ public class SecurityConstant {
|
||||
public static final String BLACKLIST_KEY = "login:blacklist";
|
||||
public static final String LOGIN_ERROR_COUNT = "login:errorCount:";
|
||||
public static final String BLACKLIST_ON = "true";
|
||||
public static final String BLACKLIST_ON_OFF_KEY = "sys_config:sys.account.blacklistOnOff";
|
||||
public static final String BLACKLIST_ON_OFF_KEY = ConfigConstant.SYS_CONFIG_KEY_PREFIX + "sys.account.blacklistOnOff";
|
||||
public static final String CAPTCHA_KEY = "login:captcha:";
|
||||
public static final String CAPTCHA_ON_OFF_KEY = "sys_config:sys.account.captchaOnOff";
|
||||
public static final String CAPTCHA_ON_OFF_KEY = ConfigConstant.SYS_CONFIG_KEY_PREFIX + "sys.account.captchaOnOff";
|
||||
public static final String CAPTCHA_ON = "true";
|
||||
public static final String REGISTER_ON_OFF_KEY = "sys_config:sys.account.registerUser";
|
||||
public static final String REGISTER_ON_OFF_KEY = ConfigConstant.SYS_CONFIG_KEY_PREFIX + "sys.account.registerUser";
|
||||
public static final String REGISTER_ON = "true";
|
||||
public static final String REDIS_SECRET_KEY = "sys:secret:secret";
|
||||
public static final String USER_DETAILS_REDIS_KEY = "user_details:";
|
||||
public static final String TOKEN_EXPIRE_TIME_KEY = ConfigConstant.SYS_CONFIG_KEY_PREFIX + "sys.token.expireTime";
|
||||
/**
|
||||
* 登录成功
|
||||
*/
|
||||
|
@ -2,8 +2,11 @@ package com.qiaoba.auth.utils;
|
||||
|
||||
import cn.hutool.core.date.DateField;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.hutool.jwt.JWTPayload;
|
||||
import cn.hutool.jwt.JWTUtil;
|
||||
import com.qiaoba.auth.constants.SecurityConstant;
|
||||
import com.qiaoba.common.redis.service.RedisService;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
import java.util.HashMap;
|
||||
@ -23,11 +26,16 @@ public class TokenUtil {
|
||||
* jwt 加解密密钥,第一次项目启动时创建随机数
|
||||
*/
|
||||
public static String secret;
|
||||
private static final RedisService redisService = SpringUtil.getBean(RedisService.class);
|
||||
|
||||
public static String generateToken(String username) {
|
||||
DateTime now = DateTime.now();
|
||||
// 3天过期
|
||||
DateTime newTime = now.offsetNew(DateField.HOUR, 72);
|
||||
|
||||
Integer expireTime = 1;
|
||||
if (redisService.hasKey(SecurityConstant.TOKEN_EXPIRE_TIME_KEY)) {
|
||||
expireTime = Integer.parseInt(redisService.get(SecurityConstant.TOKEN_EXPIRE_TIME_KEY).toString());
|
||||
}
|
||||
DateTime newTime = now.offsetNew(DateField.HOUR, expireTime);
|
||||
|
||||
Map<String, Object> payload = new HashMap<String, Object>(4);
|
||||
//签发时间
|
||||
|
@ -48,4 +48,14 @@ public class BaseConstant {
|
||||
* 资源映射路径 前缀
|
||||
*/
|
||||
public static final String RESOURCE_PREFIX = "/resource";
|
||||
|
||||
/**
|
||||
* 是
|
||||
*/
|
||||
public static final String YES = "Y";
|
||||
|
||||
/**
|
||||
* 处理失败 个数 0
|
||||
*/
|
||||
public static final Integer HANDLE_ERROR = 0;
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
package com.qiaoba.common.base.constants;
|
||||
|
||||
/**
|
||||
* 配置常量
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023-04-23 15:37:43
|
||||
*/
|
||||
public class ConfigConstant {
|
||||
|
||||
/**
|
||||
* 参数管理 cache key 前缀
|
||||
*/
|
||||
public static final String SYS_CONFIG_KEY_PREFIX = "sys_config:";
|
||||
|
||||
}
|
@ -3,6 +3,8 @@ package com.qiaoba.module.system.mapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.qiaoba.api.system.entity.SysConfig;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 系统配置 数据层
|
||||
*
|
||||
@ -11,4 +13,20 @@ import com.qiaoba.api.system.entity.SysConfig;
|
||||
* @since 2023-04-23 20:33:43
|
||||
*/
|
||||
public interface SysConfigMapper extends BaseMapper<SysConfig> {
|
||||
|
||||
/**
|
||||
* 校验参数键名是否唯一
|
||||
*
|
||||
* @param sysConfig 参数配置信息
|
||||
* @return 结果
|
||||
*/
|
||||
String checkKeyIsExist(SysConfig sysConfig);
|
||||
|
||||
/**
|
||||
* 通过主键批量查询列表
|
||||
*
|
||||
* @param ids ids
|
||||
* @return list
|
||||
*/
|
||||
List<SysConfig> selectByIds(List<String> ids);
|
||||
}
|
||||
|
@ -1,17 +1,26 @@
|
||||
package com.qiaoba.module.system.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.qiaoba.api.system.entity.SysConfig;
|
||||
import com.qiaoba.api.system.entity.param.SysConfigParam;
|
||||
import com.qiaoba.auth.utils.SecurityUtil;
|
||||
import com.qiaoba.common.base.constants.BaseConstant;
|
||||
import com.qiaoba.common.base.constants.ConfigConstant;
|
||||
import com.qiaoba.common.base.exceptions.ServiceException;
|
||||
import com.qiaoba.common.database.entity.PageQuery;
|
||||
import com.qiaoba.common.database.entity.TableDataInfo;
|
||||
import com.qiaoba.common.redis.service.RedisService;
|
||||
import com.qiaoba.module.system.mapper.SysConfigMapper;
|
||||
import com.qiaoba.module.system.service.SysConfigService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 系统配置 服务层实现
|
||||
@ -25,15 +34,31 @@ import java.util.List;
|
||||
public class SysConfigServiceImpl implements SysConfigService {
|
||||
|
||||
private final SysConfigMapper sysConfigMapper;
|
||||
private final RedisService redisService;
|
||||
|
||||
|
||||
@Override
|
||||
public int insert(SysConfig sysConfig) {
|
||||
return sysConfigMapper.insert(sysConfig);
|
||||
checkKeyIsExist(sysConfig);
|
||||
sysConfig.setCreateTime(new Date());
|
||||
sysConfig.setCreateUser(SecurityUtil.getLoginUsername());
|
||||
int result = sysConfigMapper.insert(sysConfig);
|
||||
if (result > BaseConstant.HANDLE_ERROR) {
|
||||
cacheConfig(sysConfig);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updateById(SysConfig sysConfig) {
|
||||
return sysConfigMapper.updateById(sysConfig);
|
||||
checkKeyIsExist(sysConfig);
|
||||
sysConfig.setUpdateTime(new Date());
|
||||
sysConfig.setUpdateUser(SecurityUtil.getLoginUsername());
|
||||
int result = sysConfigMapper.updateById(sysConfig);
|
||||
if (result > BaseConstant.HANDLE_ERROR) {
|
||||
cacheConfig(sysConfig);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -48,21 +73,48 @@ public class SysConfigServiceImpl implements SysConfigService {
|
||||
|
||||
@Override
|
||||
public int deleteById(String configId) {
|
||||
SysConfig sysConfig = selectById(configId);
|
||||
if (Objects.isNull(sysConfig)) {
|
||||
return BaseConstant.HANDLE_ERROR;
|
||||
}
|
||||
if (BaseConstant.YES.equals(sysConfig.getConfigType())) {
|
||||
throw new ServiceException("系统内置不允许删除!");
|
||||
}
|
||||
redisService.del(ConfigConstant.SYS_CONFIG_KEY_PREFIX + sysConfig.getConfigKey());
|
||||
return sysConfigMapper.deleteById(configId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int deleteByIds(List<String> ids) {
|
||||
List<SysConfig> configs = sysConfigMapper.selectByIds(ids);
|
||||
if (CollUtil.isEmpty(configs)) {
|
||||
return BaseConstant.HANDLE_ERROR;
|
||||
}
|
||||
List<String> delKeys = new ArrayList<>();
|
||||
for (SysConfig config : configs) {
|
||||
if (BaseConstant.YES.equals(config.getConfigType())) {
|
||||
throw new ServiceException("系统内置不允许删除!");
|
||||
}
|
||||
delKeys.add(ConfigConstant.SYS_CONFIG_KEY_PREFIX + config.getConfigKey());
|
||||
}
|
||||
redisService.del(delKeys);
|
||||
return sysConfigMapper.deleteBatchIds(ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetConfigCache() {
|
||||
|
||||
List<SysConfig> list = selectList(new SysConfigParam());
|
||||
for (SysConfig sysConfig : list) {
|
||||
cacheConfig(sysConfig);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String selectConfigByKey(String configKey) {
|
||||
configKey = ConfigConstant.SYS_CONFIG_KEY_PREFIX + configKey;
|
||||
if (redisService.hasKey(configKey)) {
|
||||
return redisService.getObject(configKey, String.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -79,4 +131,14 @@ public class SysConfigServiceImpl implements SysConfigService {
|
||||
public TableDataInfo<SysConfig> selectPageList(SysConfigParam param, PageQuery pageQuery) {
|
||||
return TableDataInfo.build(sysConfigMapper.selectPage(pageQuery.build(), param2Wrapper(param)));
|
||||
}
|
||||
|
||||
private void checkKeyIsExist(SysConfig sysConfig) {
|
||||
if (StrUtil.isNotBlank(sysConfigMapper.checkKeyIsExist(sysConfig))) {
|
||||
throw new ServiceException("参数Key已存在, 操作失败!");
|
||||
}
|
||||
}
|
||||
|
||||
private void cacheConfig(SysConfig sysConfig) {
|
||||
redisService.set(ConfigConstant.SYS_CONFIG_KEY_PREFIX + sysConfig.getConfigKey(), sysConfig.getConfigValue());
|
||||
}
|
||||
}
|
||||
|
@ -96,21 +96,19 @@ public class SysLoginServiceImpl implements SysLoginService {
|
||||
}
|
||||
|
||||
private boolean getCaptchaConfig() {
|
||||
return true;
|
||||
/* try {
|
||||
try {
|
||||
return SecurityConstant.CAPTCHA_ON.equals(redisService.get(SecurityConstant.CAPTCHA_ON_OFF_KEY));
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException("Redis中验证码配置不存在!");
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
private boolean getRegisterConfig() {
|
||||
return true;
|
||||
/* try {
|
||||
try {
|
||||
return SecurityConstant.REGISTER_ON.equals(redisService.get(SecurityConstant.REGISTER_ON_OFF_KEY));
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException("Redis中注册配置不存在!");
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
private void validateCaptcha(String code, String uuid) {
|
||||
|
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.qiaoba.module.system.mapper.SysConfigMapper">
|
||||
<select id="checkKeyIsExist" resultType="string">
|
||||
select config_key from sys_config
|
||||
where config_key = #{configKey}
|
||||
<if test="configId !=null">
|
||||
and config_id != #{configId}
|
||||
</if>
|
||||
limit 1
|
||||
</select>
|
||||
|
||||
<select id="selectByIds" resultType="com.qiaoba.api.system.entity.SysConfig">
|
||||
select config_id, config_name, config_key, config_value, config_type, create_user, create_time, update_user, update_time, remark
|
||||
from sys_config where config_id in
|
||||
<foreach item="configId" collection="list" open="(" separator="," close=")">
|
||||
#{configId}
|
||||
</foreach>
|
||||
</select>
|
||||
</mapper>
|
Reference in New Issue
Block a user