refactor: 优化相关代码

This commit is contained in:
horizons 2022-09-06 07:21:51 +08:00
parent f0ba8d48c9
commit 3d99e4a99f
13 changed files with 23 additions and 618 deletions

View File

@ -1,14 +1,14 @@
package com.youlai.admin.api;
import com.youlai.admin.dto.ClientAuthDTO;
import com.youlai.admin.dto.OAuth2ClientDTO;
import com.youlai.common.result.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "youlai-admin", contextId = "oauth-client")
public interface OAuthClientFeignClient {
public interface OAuth2ClientFeignClient {
@GetMapping("/api/v1/oauth-clients/getOAuth2ClientById")
Result<ClientAuthDTO> getOAuth2ClientById(@RequestParam String clientId);
Result<OAuth2ClientDTO> getOAuth2ClientById(@RequestParam String clientId);
}

View File

@ -9,7 +9,7 @@ import lombok.Data;
* @date 2021/1/15
*/
@Data
public class ClientAuthDTO {
public class OAuth2ClientDTO {
/**
* 客户端ID

View File

@ -39,8 +39,8 @@ public class SysDeptController {
@ApiOperation(value = "获取部门下拉选项")
@GetMapping("/options")
public Result<List<Option>> lisetDeptOptions() {
List<Option> list = deptService.lisetDeptOptions();
public Result<List<Option>> listDeptOptions() {
List<Option> list = deptService.listDeptOptions();
return Result.success(list);
}

View File

@ -3,7 +3,7 @@ package com.youlai.admin.controller;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.admin.dto.ClientAuthDTO;
import com.youlai.admin.dto.OAuth2ClientDTO;
import com.youlai.admin.pojo.entity.SysOauthClient;
import com.youlai.admin.pojo.query.ClientPageQuery;
import com.youlai.admin.pojo.vo.client.ClientPageVO;
@ -66,13 +66,13 @@ public class SysOauthClientController {
@ApiOperation(value = "获取 OAuth2 客户端认证信息", notes = "Feign 调用", hidden = true)
@GetMapping("/getOAuth2ClientById")
public Result<ClientAuthDTO> getOAuth2ClientById(
public Result<OAuth2ClientDTO> getOAuth2ClientById(
@ApiParam("客户端ID") @RequestParam String clientId) {
SysOauthClient client = clientService.getById(clientId);
Assert.isTrue(client != null, "OAuth2 客户端不存在");
ClientAuthDTO clientAuthDTO = new ClientAuthDTO();
BeanUtil.copyProperties(client, clientAuthDTO);
return Result.success(clientAuthDTO);
OAuth2ClientDTO oAuth2ClientDTO = new OAuth2ClientDTO();
BeanUtil.copyProperties(client, oAuth2ClientDTO);
return Result.success(oAuth2ClientDTO);
}
}

View File

@ -29,7 +29,7 @@ public interface SysDeptService extends IService<SysDept> {
*
* @return
*/
List<Option> lisetDeptOptions();
List<Option> listDeptOptions();
/**
* 新增部门

View File

@ -119,7 +119,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
* @return
*/
@Override
public List<Option> lisetDeptOptions() {
public List<Option> listDeptOptions() {
List<SysDept> deptList = this.list(new LambdaQueryWrapper<SysDept>()
.eq(SysDept::getStatus, GlobalConstants.STATUS_YES)
.select(SysDept::getId, SysDept::getParentId, SysDept::getName)

View File

@ -1,7 +1,7 @@
package com.youlai.auth.security.core.clientdetails;
import com.youlai.admin.api.OAuthClientFeignClient;
import com.youlai.admin.dto.ClientAuthDTO;
import com.youlai.admin.api.OAuth2ClientFeignClient;
import com.youlai.admin.dto.OAuth2ClientDTO;
import com.youlai.auth.common.enums.PasswordEncoderTypeEnum;
import com.youlai.common.result.Result;
import lombok.RequiredArgsConstructor;
@ -20,15 +20,15 @@ import org.springframework.stereotype.Service;
@RequiredArgsConstructor
public class ClientDetailsServiceImpl implements ClientDetailsService {
private final OAuthClientFeignClient oAuthClientFeignClient;
private final OAuth2ClientFeignClient oAuth2ClientFeignClient;
@Override
@Cacheable(cacheNames = "auth", key = "'oauth-client:'+#clientId")
public ClientDetails loadClientByClientId(String clientId) {
try {
Result<ClientAuthDTO> result = oAuthClientFeignClient.getOAuth2ClientById(clientId);
Result<OAuth2ClientDTO> result = oAuth2ClientFeignClient.getOAuth2ClientById(clientId);
if (Result.success().getCode().equals(result.getCode())) {
ClientAuthDTO client = result.getData();
OAuth2ClientDTO client = result.getData();
BaseClientDetails clientDetails = new BaseClientDetails(
client.getClientId(),
client.getResourceIds(),

View File

@ -43,7 +43,6 @@
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -2,8 +2,8 @@ package com.youlai.common.redis;
import com.youlai.common.constant.RedisConstants;
import com.youlai.common.enums.BusinessTypeEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@ -11,13 +11,12 @@ import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
@Component
@Slf4j
@RequiredArgsConstructor
public class BusinessNoGenerator {
@Autowired
private RedisTemplate redisTemplate;
private final RedisTemplate redisTemplate;
/**
* @param businessType 业务类型枚举

View File

@ -1,590 +0,0 @@
package com.youlai.common.redis.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @author huawei
* @desc spring redis 通用工具类
* @email huawei_code@163.com
* @date 2021/2/9
*/
@Component
public class RedisUtils {
/**
* 注入redisTemplate bean
*/
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 指定缓存失效时间
*
* @param key
* @param time 时间()
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据key获取过期时间
*
* @param key 不能为null
* @return 时间() 返回0代表为永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判断key是否存在
*
* @param key
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除缓存
*
* @param key 可以传一个值 或多个
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete((List<String>) CollectionUtils.arrayToList(key));
}
}
}
// ============================String(字符串)=============================
/**
* 普通缓存获取
*
* @param key
* @return
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通缓存放入
*
* @param key
* @param value
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通缓存放入并设置时间
*
* @param key
* @param value
* @param time 时间() time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 递增
*
* @param key
* @param delta 要增加几(大于0)
* @return
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 递减
*
* @param key
* @param delta 要减少几(小于0)
* @return
*/
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
// ================================Hash(哈希)=================================
/**
* HashGet
*
* @param key 不能为null
* @param item 不能为null
* @return
*/
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* 获取hashKey对应的所有键值
*
* @param key
* @return 对应的多个键值
*/
public Map<Object, Object> hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* HashSet
*
* @param key
* @param map 对应多个键值
* @return true 成功 false 失败
*/
public boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* HashSet 并设置时间
*
* @param key
* @param map 对应多个键值
* @param time 时间()
* @return true成功 false失败
*/
public boolean hmset(String key, Map<String, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key
* @param item
* @param value
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key
* @param item
* @param value
* @param time 时间() 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除hash表中的值
*
* @param key 不能为null
* @param item 可以使多个 不能为null
*/
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/**
* 判断hash表中是否有该项的值
*
* @param key 不能为null
* @param item 不能为null
* @return true 存在 false不存在
*/
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
*
* @param key
* @param item
* @param by 要增加几(大于0)
* @return
*/
public double hincr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/**
* hash递减
*
* @param key
* @param item
* @param by 要减少记(小于0)
* @return
*/
public double hdecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
// ============================Set(集合)=============================
/**
* 根据key获取Set中的所有值
*
* @param key
* @return
*/
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 根据value从一个set中查询,是否存在
*
* @param key
* @param value
* @return true 存在 false不存在
*/
public boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将数据放入set缓存
*
* @param key
* @param values 可以是多个
* @return 成功个数
*/
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 将set数据放入缓存
*
* @param key
* @param time 时间()
* @param values 可以是多个
* @return 成功个数
*/
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0) {
expire(key, time);
}
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 获取set缓存的长度
*
* @param key
* @return
*/
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 移除值为value的
*
* @param key
* @param values 可以是多个
* @return 移除的个数
*/
public long setRemove(String key, Object... values) {
try {
Long count = redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
// ===============================List(列表)=================================
/**
* 获取list缓存的内容
*
* @param key
* @param start 开始
* @param end 结束 0 -1代表所有值
* @return
*/
public List<Object> lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取list缓存的长度
*
* @param key
* @return
*/
public long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 通过索引 获取list中的值
*
* @param key
* @param index 索引 index>=0时 0 表头1 第二个元素依次类推index<0时-1表尾-2倒数第二个元素依次类推
* @return
*/
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 将list放入缓存
*
* @param key
* @param value
* @return
*/
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将list放入缓存
*
* @param key
* @param value
* @param time 时间()
* @return
*/
public boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将list放入缓存
*
* @param key
* @param value
* @return
*/
public boolean lSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将list放入缓存
*
* @param key
* @param value
* @param time 时间()
* @return
*/
public boolean lSet(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据索引修改list中的某条数据
*
* @param key
* @param index 索引
* @param value
* @return
*/
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 移除N个值为value
*
* @param key
* @param count 移除多少个
* @param value
* @return 移除的个数
*/
public long lRemove(String key, long count, Object value) {
try {
Long remove = redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 根据正则表达式获取key列表
*
* @param patternKey 正则表达式
* @return 匹配key列表
*/
public Set<String> keys(String patternKey) {
try {
Set<String> keys = redisTemplate.keys(patternKey);
return keys;
} catch (Exception e) {
e.printStackTrace();
return new HashSet<>();
}
}
}

View File

@ -1,7 +1,6 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.youlai.common.redis.RedisConfig,\
com.youlai.common.redis.BusinessNoGenerator,\
com.youlai.common.redis.utils.RedisUtils,\
com.youlai.common.redis.RedissonConfig,\
com.youlai.common.redis.RedisCacheConfig

View File

@ -1,6 +1,5 @@
package com.youlai.common.web.domain;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

View File

@ -9,12 +9,12 @@ import lombok.NoArgsConstructor;
import java.util.List;
/**
* Select选择器默认Option属性对象
* 下拉选项对象
*
* @author haoxr
* @date 2022/1/22
*/
@ApiModel("Select选择器默认Option属性")
@ApiModel("下拉选项对象")
@Data
@NoArgsConstructor
public class Option<T> {
@ -39,5 +39,4 @@ public class Option<T> {
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
private List<Option> children;
}