diff --git a/springboot/fastbee-common/src/main/java/com/fastbee/common/core/domain/BaseEntity.java b/springboot/fastbee-common/src/main/java/com/fastbee/common/core/domain/BaseEntity.java index 3bc489d5..27b2f01b 100644 --- a/springboot/fastbee-common/src/main/java/com/fastbee/common/core/domain/BaseEntity.java +++ b/springboot/fastbee-common/src/main/java/com/fastbee/common/core/domain/BaseEntity.java @@ -1,126 +1,175 @@ package com.fastbee.common.core.domain; -import java.io.Serializable; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; +import cn.hutool.core.util.ObjectUtil; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import io.swagger.annotations.ApiModelProperty; +import java.io.Serializable; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + /** * Entity基类 - * + * * @author ruoyi */ -public class BaseEntity implements Serializable -{ +public class BaseEntity implements Serializable { private static final long serialVersionUID = 1L; - /** 搜索值 */ + /** + * 搜索值 + */ + @TableField(exist = false) @ApiModelProperty("搜索值") @JsonIgnore private String searchValue; - /** 创建者 */ + /** + * 创建者 + */ @ApiModelProperty("创建者") private String createBy; - /** 创建时间 */ + /** + * 创建时间 + */ @ApiModelProperty("创建时间") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date createTime; - /** 更新者 */ + /** + * 更新者 + */ @ApiModelProperty("更新者") private String updateBy; - /** 更新时间 */ + /** + * 更新时间 + */ @ApiModelProperty("更新时间") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date updateTime; - /** 备注 */ + /** + * 备注 + */ @ApiModelProperty("备注") private String remark; - /** 请求参数 */ + /** + * 当前记录起始索引 默认值 + */ + public static final int DEFAULT_PAGE_NUM = 1; + + /** + * 每页显示记录数 默认值 默认查全部 + */ + public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE; + + /** + * 构建分页对象 + */ + public Page buildPage() { + Integer pageNum = ObjectUtil.defaultIfNull(this.pageNum, DEFAULT_PAGE_NUM); + Integer pageSize = ObjectUtil.defaultIfNull(this.pageSize, DEFAULT_PAGE_SIZE); + if (pageNum <= 0) { + pageNum = DEFAULT_PAGE_NUM; + } + Page page = new Page<>(pageNum, pageSize); + return page; + } + + public Integer getPageNum() { + return ObjectUtil.defaultIfNull(this.pageNum, DEFAULT_PAGE_NUM); + } + + public void setPageNum(Integer pageNum) { + this.pageNum = pageNum; + } + + public Integer getPageSize() { + return ObjectUtil.defaultIfNull(this.pageSize, DEFAULT_PAGE_SIZE); + } + + public void setPageSize(Integer pageSize) { + this.pageSize = pageSize; + } + + @TableField(exist = false) + private Integer pageNum; + + @TableField(exist = false) + private Integer pageSize; + + /** + * 请求参数 + */ + @TableField(exist = false) @ApiModelProperty("请求参数") @JsonInclude(JsonInclude.Include.NON_EMPTY) private Map params; - public String getSearchValue() - { + public String getSearchValue() { return searchValue; } - public void setSearchValue(String searchValue) - { + public void setSearchValue(String searchValue) { this.searchValue = searchValue; } - public String getCreateBy() - { + public String getCreateBy() { return createBy; } - public void setCreateBy(String createBy) - { + public void setCreateBy(String createBy) { this.createBy = createBy; } - public Date getCreateTime() - { + public Date getCreateTime() { return createTime; } - public void setCreateTime(Date createTime) - { + public void setCreateTime(Date createTime) { this.createTime = createTime; } - public String getUpdateBy() - { + public String getUpdateBy() { return updateBy; } - public void setUpdateBy(String updateBy) - { + public void setUpdateBy(String updateBy) { this.updateBy = updateBy; } - public Date getUpdateTime() - { + public Date getUpdateTime() { return updateTime; } - public void setUpdateTime(Date updateTime) - { + public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } - public String getRemark() - { + public String getRemark() { return remark; } - public void setRemark(String remark) - { + public void setRemark(String remark) { this.remark = remark; } - public Map getParams() - { - if (params == null) - { + public Map getParams() { + if (params == null) { params = new HashMap<>(); } return params; } - public void setParams(Map params) - { + public void setParams(Map params) { this.params = params; } } diff --git a/springboot/fastbee-common/src/main/java/com/fastbee/common/core/domain/PageEntity.java b/springboot/fastbee-common/src/main/java/com/fastbee/common/core/domain/PageEntity.java new file mode 100644 index 00000000..30c00af6 --- /dev/null +++ b/springboot/fastbee-common/src/main/java/com/fastbee/common/core/domain/PageEntity.java @@ -0,0 +1,76 @@ +package com.fastbee.common.core.domain; + +import cn.hutool.core.util.ObjectUtil; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +/** + * @author admin + * @version 1.0 + * @description: 分页参数基础类 + * @date 2024-11-15 18:00 + */ +@Data +public class PageEntity implements Serializable { + + @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) + @TableField(exist = false) + private Integer pageNum; + + @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) + @TableField(exist = false) + private Integer pageSize; + /** + * 当前记录起始索引 默认值 + */ + public static final int DEFAULT_PAGE_NUM = 1; + + /** + * 每页显示记录数 默认值 默认查全部 + */ + public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE; + + /** + * 构建分页对象 + */ + public Page buildPage() { + Integer pageNum = ObjectUtil.defaultIfNull(this.pageNum, DEFAULT_PAGE_NUM); + Integer pageSize = ObjectUtil.defaultIfNull(this.pageSize, DEFAULT_PAGE_SIZE); + if (pageNum <= 0) { + pageNum = DEFAULT_PAGE_NUM; + } + Page page = new Page<>(pageNum, pageSize); + return page; + } + + /** + * 请求参数 + */ + @TableField(exist = false) + @ApiModelProperty("请求参数") + @JsonInclude(JsonInclude.Include.NON_EMPTY) + private Map params; + + public Map getParams() { + if (params == null) { + params = new HashMap<>(); + } + return params; + } + + public Integer getPageNum() { + return ObjectUtil.defaultIfNull(this.pageNum, DEFAULT_PAGE_NUM); + } + + public Integer getPageSize() { + return ObjectUtil.defaultIfNull(this.pageSize, DEFAULT_PAGE_SIZE); + } +} diff --git a/springboot/fastbee-common/src/main/java/com/fastbee/common/core/domain/model/LoginUser.java b/springboot/fastbee-common/src/main/java/com/fastbee/common/core/domain/model/LoginUser.java index 07324faa..a461282c 100644 --- a/springboot/fastbee-common/src/main/java/com/fastbee/common/core/domain/model/LoginUser.java +++ b/springboot/fastbee-common/src/main/java/com/fastbee/common/core/domain/model/LoginUser.java @@ -1,15 +1,16 @@ package com.fastbee.common.core.domain.model; +import com.alibaba.fastjson2.annotation.JSONField; +import com.fastbee.common.core.domain.entity.SysUser; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; + import java.util.Collection; import java.util.Set; -import org.springframework.security.core.GrantedAuthority; -import org.springframework.security.core.userdetails.UserDetails; -import com.alibaba.fastjson2.annotation.JSONField; -import com.fastbee.common.core.domain.entity.SysUser; /** * 登录用户身份权限 - * + * * @author ruoyi */ public class LoginUser implements UserDetails @@ -31,6 +32,9 @@ public class LoginUser implements UserDetails */ private String token; + + private String requestToken; + /** * 登录时间 */ @@ -71,6 +75,36 @@ public class LoginUser implements UserDetails */ private SysUser user; + private String language; + + private Long deptUserId; + + private Boolean neverExpire = Boolean.FALSE; + + public Boolean getNeverExpire() { + return neverExpire; + } + + public void setNeverExpire(Boolean neverExpire) { + this.neverExpire = neverExpire; + } + + public String getLanguage() { + return language; + } + + public Long getDeptUserId() { + return deptUserId; + } + + public void setDeptUserId(Long deptUserId) { + this.deptUserId = deptUserId; + } + + public void setLanguage(String language) { + this.language = language; + } + public Long getUserId() { return userId; @@ -101,6 +135,14 @@ public class LoginUser implements UserDetails this.token = token; } + public String getRequestToken() { + return requestToken; + } + + public void setRequestToken(String requestToken) { + this.requestToken = requestToken; + } + public LoginUser() { } @@ -119,6 +161,15 @@ public class LoginUser implements UserDetails this.permissions = permissions; } + public LoginUser(Long userId, Long deptId, String language, SysUser user, Set permissions) + { + this.userId = userId; + this.deptId = deptId; + this.user = user; + this.language = language; + this.permissions = permissions; + } + @JSONField(serialize = false) @Override public String getPassword() @@ -144,7 +195,7 @@ public class LoginUser implements UserDetails /** * 指定用户是否解锁,锁定的用户无法进行身份验证 - * + * * @return */ @JSONField(serialize = false) @@ -156,7 +207,7 @@ public class LoginUser implements UserDetails /** * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证 - * + * * @return */ @JSONField(serialize = false) @@ -168,7 +219,7 @@ public class LoginUser implements UserDetails /** * 是否可用 ,禁用的用户不能身份验证 - * + * * @return */ @JSONField(serialize = false) diff --git a/springboot/fastbee-common/src/main/java/com/fastbee/common/mabatis/enums/DataBaseType.java b/springboot/fastbee-common/src/main/java/com/fastbee/common/mybatis/enums/DataBaseType.java similarity index 96% rename from springboot/fastbee-common/src/main/java/com/fastbee/common/mabatis/enums/DataBaseType.java rename to springboot/fastbee-common/src/main/java/com/fastbee/common/mybatis/enums/DataBaseType.java index 2fef9e70..33805d3b 100644 --- a/springboot/fastbee-common/src/main/java/com/fastbee/common/mabatis/enums/DataBaseType.java +++ b/springboot/fastbee-common/src/main/java/com/fastbee/common/mybatis/enums/DataBaseType.java @@ -1,4 +1,4 @@ -package com.fastbee.common.mabatis.enums; +package com.fastbee.common.mybatis.enums; import com.fastbee.common.utils.StringUtils; import lombok.AllArgsConstructor; diff --git a/springboot/fastbee-framework/src/main/java/com/fastbee/framework/mybatis/mapper/BaseMapperX.java b/springboot/fastbee-common/src/main/java/com/fastbee/common/mybatis/mapper/BaseMapperX.java similarity index 97% rename from springboot/fastbee-framework/src/main/java/com/fastbee/framework/mybatis/mapper/BaseMapperX.java rename to springboot/fastbee-common/src/main/java/com/fastbee/common/mybatis/mapper/BaseMapperX.java index e6252387..ab3b1dc1 100644 --- a/springboot/fastbee-framework/src/main/java/com/fastbee/framework/mybatis/mapper/BaseMapperX.java +++ b/springboot/fastbee-common/src/main/java/com/fastbee/common/mybatis/mapper/BaseMapperX.java @@ -1,4 +1,4 @@ -package com.fastbee.framework.mybatis.mapper; +package com.fastbee.common.mybatis.mapper; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; @@ -9,7 +9,7 @@ import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.baomidou.mybatisplus.extension.toolkit.Db; import com.fastbee.common.core.domain.PageParam; import com.fastbee.common.core.domain.PageResult; -import com.fastbee.framework.mybatis.utils.MyBatisUtils; +import com.fastbee.common.mybatis.utils.MyBatisUtils; import org.apache.ibatis.annotations.Param; import java.util.Collection; diff --git a/springboot/fastbee-framework/src/main/java/com/fastbee/framework/mybatis/utils/MyBatisUtils.java b/springboot/fastbee-common/src/main/java/com/fastbee/common/mybatis/utils/MyBatisUtils.java similarity index 98% rename from springboot/fastbee-framework/src/main/java/com/fastbee/framework/mybatis/utils/MyBatisUtils.java rename to springboot/fastbee-common/src/main/java/com/fastbee/common/mybatis/utils/MyBatisUtils.java index 9493e76a..7cb2d27b 100644 --- a/springboot/fastbee-framework/src/main/java/com/fastbee/framework/mybatis/utils/MyBatisUtils.java +++ b/springboot/fastbee-common/src/main/java/com/fastbee/common/mybatis/utils/MyBatisUtils.java @@ -1,4 +1,4 @@ -package com.fastbee.framework.mybatis.utils; +package com.fastbee.common.mybatis.utils; import cn.hutool.core.collection.CollectionUtil; import com.baomidou.mybatisplus.core.metadata.OrderItem; diff --git a/springboot/fastbee-framework/src/main/java/com/fastbee/framework/config/FastJson2JsonRedisSerializer.java b/springboot/fastbee-framework/src/main/java/com/fastbee/framework/config/FastJson2JsonRedisSerializer.java index 401fccdc..9df9451d 100644 --- a/springboot/fastbee-framework/src/main/java/com/fastbee/framework/config/FastJson2JsonRedisSerializer.java +++ b/springboot/fastbee-framework/src/main/java/com/fastbee/framework/config/FastJson2JsonRedisSerializer.java @@ -1,14 +1,11 @@ package com.fastbee.framework.config; +import java.nio.charset.Charset; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.data.redis.serializer.SerializationException; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONReader; import com.alibaba.fastjson2.JSONWriter; -import com.alibaba.fastjson2.filter.Filter; -import com.fastbee.common.constant.Constants; -import org.springframework.data.redis.serializer.RedisSerializer; -import org.springframework.data.redis.serializer.SerializationException; - -import java.nio.charset.Charset; /** * Redis使用FastJson序列化 @@ -19,8 +16,6 @@ public class FastJson2JsonRedisSerializer implements RedisSerializer { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); - static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter(Constants.JSON_WHITELIST_STR); - private Class clazz; public FastJson2JsonRedisSerializer(Class clazz) @@ -48,6 +43,6 @@ public class FastJson2JsonRedisSerializer implements RedisSerializer } String str = new String(bytes, DEFAULT_CHARSET); - return JSON.parseObject(str, clazz, AUTO_TYPE_FILTER); + return JSON.parseObject(str, clazz, JSONReader.Feature.SupportAutoType); } } diff --git a/springboot/fastbee-framework/src/main/java/com/fastbee/framework/config/RedisConfig.java b/springboot/fastbee-framework/src/main/java/com/fastbee/framework/config/RedisConfig.java index 909d32d9..70e9783d 100644 --- a/springboot/fastbee-framework/src/main/java/com/fastbee/framework/config/RedisConfig.java +++ b/springboot/fastbee-framework/src/main/java/com/fastbee/framework/config/RedisConfig.java @@ -1,7 +1,6 @@ package com.fastbee.framework.config; import org.springframework.cache.annotation.CachingConfigurerSupport; -import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; @@ -11,11 +10,10 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; /** * redis配置 - * + * * @author ruoyi */ @Configuration -@EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Bean diff --git a/springboot/fastbee-framework/src/main/java/com/fastbee/framework/mybatis/helper/DataBaseHelper.java b/springboot/fastbee-framework/src/main/java/com/fastbee/framework/mybatis/helper/DataBaseHelper.java index 2299365c..144e545a 100644 --- a/springboot/fastbee-framework/src/main/java/com/fastbee/framework/mybatis/helper/DataBaseHelper.java +++ b/springboot/fastbee-framework/src/main/java/com/fastbee/framework/mybatis/helper/DataBaseHelper.java @@ -3,7 +3,7 @@ package com.fastbee.framework.mybatis.helper; import cn.hutool.core.convert.Convert; import com.baomidou.dynamic.datasource.DynamicRoutingDataSource; import com.fastbee.common.exception.ServiceException; -import com.fastbee.common.mabatis.enums.DataBaseType; +import com.fastbee.common.mybatis.enums.DataBaseType; import com.fastbee.common.utils.spring.SpringUtils; import lombok.AccessLevel; import lombok.NoArgsConstructor; diff --git a/springboot/fastbee-framework/src/main/java/com/fastbee/framework/web/service/TokenService.java b/springboot/fastbee-framework/src/main/java/com/fastbee/framework/web/service/TokenService.java index 73f8b8f0..9ae8d160 100644 --- a/springboot/fastbee-framework/src/main/java/com/fastbee/framework/web/service/TokenService.java +++ b/springboot/fastbee-framework/src/main/java/com/fastbee/framework/web/service/TokenService.java @@ -2,6 +2,7 @@ package com.fastbee.framework.web.service; import com.fastbee.common.constant.CacheConstants; import com.fastbee.common.constant.Constants; +import com.fastbee.common.core.domain.entity.SysUser; import com.fastbee.common.core.domain.model.LoginUser; import com.fastbee.common.core.redis.RedisCache; import com.fastbee.common.utils.ServletUtils; @@ -9,27 +10,33 @@ import com.fastbee.common.utils.StringUtils; import com.fastbee.common.utils.ip.AddressUtils; import com.fastbee.common.utils.ip.IpUtils; import com.fastbee.common.utils.uuid.IdUtils; +import com.fastbee.system.domain.SysClient; +import com.fastbee.system.service.ISysClientService; import eu.bitwalker.useragentutils.UserAgent; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import java.util.concurrent.TimeUnit; + /** * token验证处理 * * @author ruoyi */ +@Slf4j @Component -public class TokenService -{ +public class TokenService { // 令牌自定义标识 @Value("${token.header}") private String header; @@ -51,34 +58,37 @@ public class TokenService @Autowired private RedisCache redisCache; + @Autowired + private ISysClientService sysClientService; + + @Autowired + private UserDetailsServiceImpl userDetailsServiceImpl; + /** * 获取用户身份信息 * * @return 用户信息 */ - public LoginUser getLoginUser(HttpServletRequest request) - { + public LoginUser getLoginUser(HttpServletRequest request) { // 获取请求携带的令牌 String token = getToken(request); - if (StringUtils.isNotEmpty(token)) - { - try - { + if (StringUtils.isNotEmpty(token)) { + try { Claims claims = parseToken(token); // 解析对应的权限以及用户信息 String uuid = (String) claims.get(Constants.LOGIN_USER_KEY); String userKey = getTokenKey(uuid); LoginUser user = redisCache.getCacheObject(userKey); + user.setRequestToken(token); return user; - } - catch (Exception e) - { + } catch (Exception e) { + log.info("获取缓存报错:{}", e.getMessage()); } } return null; } - /** + /** * 获取用户身份信息 * * @return 用户信息 @@ -93,6 +103,7 @@ public class TokenService LoginUser user = redisCache.getCacheObject(userKey); return user; } catch (Exception e) { + log.info("获取缓存报错:{}", e.getMessage()); } } return null; @@ -101,6 +112,7 @@ public class TokenService /** * 根据用户id获取用户身份信息 * 由于多端登录,根据token获取的用户信息不一样,所以增加一个根据用户id获取用户信息的缓存key,以后多端需要获取用户最新信息就用这个方法吧 + * * @return 用户信息 */ public LoginUser getLoginUserByUserId(Long userId) { @@ -109,6 +121,7 @@ public class TokenService String userKey = getUserIdKey(userId); return redisCache.getCacheObject(userKey); } catch (Exception e) { + log.info("获取缓存报错:{}", e.getMessage()); } } return null; @@ -117,10 +130,8 @@ public class TokenService /** * 设置用户身份信息 */ - public void setLoginUser(LoginUser loginUser) - { - if (StringUtils.isNotNull(loginUser) && StringUtils.isNotEmpty(loginUser.getToken())) - { + public void setLoginUser(LoginUser loginUser) { + if (StringUtils.isNotNull(loginUser) && StringUtils.isNotEmpty(loginUser.getToken())) { refreshToken(loginUser); } } @@ -128,10 +139,8 @@ public class TokenService /** * 删除用户身份信息 */ - public void delLoginUser(String token) - { - if (StringUtils.isNotEmpty(token)) - { + public void delLoginUser(String token) { + if (StringUtils.isNotEmpty(token)) { String userKey = getTokenKey(token); redisCache.deleteObject(userKey); } @@ -143,8 +152,7 @@ public class TokenService * @param loginUser 用户信息 * @return 令牌 */ - public String createToken(LoginUser loginUser) - { + public String createToken(LoginUser loginUser) { String token = IdUtils.fastUUID(); loginUser.setToken(token); setUserAgent(loginUser); @@ -155,18 +163,61 @@ public class TokenService return createToken(claims); } + public int addToken(SysUser user, SysClient sysClient) { + UserDetails userDetails = userDetailsServiceImpl.createLoginUser(user); + LoginUser loginUser = (LoginUser) userDetails; + sysClient.setToken(Constants.TOKEN_PREFIX + createToken(loginUser, sysClient.getClientSecret(), Math.toIntExact(sysClient.getTimeout()))); + return sysClientService.insertSysClient(sysClient); + } + + public int updateToken(SysUser loginuser, SysClient sysClient) { + LoginUser user; + Claims claims = parseToken(sysClient); + if (claims == null) { + UserDetails userDetails = userDetailsServiceImpl.createLoginUser(loginuser); + user = (LoginUser) userDetails; + sysClient.setToken(Constants.TOKEN_PREFIX + createToken(user, sysClient.getClientSecret(), Math.toIntExact(sysClient.getTimeout()))); + } else { + // 解析对应的权限以及用户信息 + String uuid = (String) claims.get(Constants.LOGIN_USER_KEY); + String userKey = getTokenKey(uuid); + user = redisCache.getCacheObject(userKey); + if (user == null) { + UserDetails userDetails = userDetailsServiceImpl.createLoginUser(loginuser); + user = (LoginUser) userDetails; + } + log.debug("loginUser:{}", user); + if (sysClient.getEnable() != null && "1".equals(sysClient.getEnable())) { + delLoginUser(uuid); + sysClient.setToken(Constants.TOKEN_PREFIX + createToken(user, sysClient.getClientSecret(), Math.toIntExact(sysClient.getTimeout()))); + } else if ("0".equals(sysClient.getEnable())) { + delLoginUser(uuid); + } + } + return sysClientService.updateSysClient(sysClient); + } + + public String createToken(LoginUser loginUser, String secret, int expireTime) { + String token = IdUtils.fastUUID(); + loginUser.setToken(token); + setUserAgent(loginUser); + refreshToken(loginUser, expireTime); + + Map claims = new HashMap<>(); + claims.put(Constants.LOGIN_USER_KEY, token); + return createToken(claims, secret); + } + /** * 验证令牌有效期,相差不足20分钟,自动刷新缓存 * * @param loginUser * @return 令牌 */ - public void verifyToken(LoginUser loginUser) - { + public void verifyToken(LoginUser loginUser) { long expireTime = loginUser.getExpireTime(); long currentTime = System.currentTimeMillis(); - if (expireTime - currentTime <= MILLIS_MINUTE_TEN) - { + if (expireTime - currentTime <= MILLIS_MINUTE_TEN) { refreshToken(loginUser); } } @@ -176,8 +227,7 @@ public class TokenService * * @param loginUser 登录信息 */ - public void refreshToken(LoginUser loginUser) - { + public void refreshToken(LoginUser loginUser, int expireTime) { loginUser.setLoginTime(System.currentTimeMillis()); loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE); // 根据uuid将loginUser缓存 @@ -188,13 +238,29 @@ public class TokenService redisCache.setCacheObject(userIdKey, loginUser, expireTime, TimeUnit.MINUTES); } + public void refreshToken(LoginUser loginUser) { + loginUser.setLoginTime(System.currentTimeMillis()); + loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE); + // 根据uuid将loginUser缓存 + String userKey = getTokenKey(loginUser.getToken()); + String userIdKey = getUserIdKey(loginUser.getUserId()); + if (Boolean.TRUE.equals(loginUser.getNeverExpire())) { + redisCache.setCacheObject(userKey, loginUser); + // 使用token作为用户信息缓存key,多端不能同步最新信息,需要重新登录,因此添加一个使用用户id作为缓存key + redisCache.setCacheObject(userIdKey, loginUser); + } else { + redisCache.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES); + // 使用token作为用户信息缓存key,多端不能同步最新信息,需要重新登录,因此添加一个使用用户id作为缓存key + redisCache.setCacheObject(userIdKey, loginUser, expireTime, TimeUnit.MINUTES); + } + } + /** * 设置用户代理信息 * * @param loginUser 登录信息 */ - public void setUserAgent(LoginUser loginUser) - { + public void setUserAgent(LoginUser loginUser) { UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent")); String ip = IpUtils.getIpAddr(ServletUtils.getRequest()); loginUser.setIpaddr(ip); @@ -209,8 +275,14 @@ public class TokenService * @param claims 数据声明 * @return 令牌 */ - private String createToken(Map claims) - { + private String createToken(Map claims) { + String token = Jwts.builder() + .setClaims(claims) + .signWith(SignatureAlgorithm.HS512, secret).compact(); + return token; + } + + private String createToken(Map claims, String secret) { String token = Jwts.builder() .setClaims(claims) .signWith(SignatureAlgorithm.HS512, secret).compact(); @@ -223,12 +295,23 @@ public class TokenService * @param token 令牌 * @return 数据声明 */ - private Claims parseToken(String token) - { + private Claims parseToken(String token) { return Jwts.parser() .setSigningKey(secret) .parseClaimsJws(token) .getBody(); + + } + + private Claims parseToken(SysClient sysClient) { + if (sysClient.getClientSecret() != null && !Objects.equals(sysClient.getToken(), "")) { + return Jwts.parser() + .setSigningKey(sysClient.getClientSecret()) + .parseClaimsJws(sysClient.getToken().substring(Constants.TOKEN_PREFIX.length())) + .getBody(); + } else { + return null; + } } /** @@ -237,8 +320,7 @@ public class TokenService * @param token 令牌 * @return 用户名 */ - public String getUsernameFromToken(String token) - { + public String getUsernameFromToken(String token) { Claims claims = parseToken(token); return claims.getSubject(); } @@ -249,18 +331,15 @@ public class TokenService * @param request * @return token */ - private String getToken(HttpServletRequest request) - { + private String getToken(HttpServletRequest request) { String token = request.getHeader(header); - if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX)) - { + if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX)) { token = token.replace(Constants.TOKEN_PREFIX, ""); } return token; } - private String getTokenKey(String uuid) - { + private String getTokenKey(String uuid) { return CacheConstants.LOGIN_TOKEN_KEY + uuid; } diff --git a/springboot/fastbee-service/fastbee-system-service/pom.xml b/springboot/fastbee-service/fastbee-system-service/pom.xml index 3fde8688..853c2a2f 100644 --- a/springboot/fastbee-service/fastbee-system-service/pom.xml +++ b/springboot/fastbee-service/fastbee-system-service/pom.xml @@ -29,6 +29,11 @@ ${dynamic-datasource.version} + + org.mapstruct + mapstruct + + diff --git a/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/convert/SysClientConvert.java b/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/convert/SysClientConvert.java new file mode 100644 index 00000000..e2f8be00 --- /dev/null +++ b/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/convert/SysClientConvert.java @@ -0,0 +1,76 @@ +package com.fastbee.system.convert; + +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.fastbee.system.domain.SysClient; +import com.fastbee.system.domain.vo.SysClientVO; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +import java.util.List; + +/** + * 系统授权Convert转换类 + * + * @author zhuangpeng.li + * @date 2024-12-12 + */ +@Mapper +public interface SysClientConvert +{ + /** 代码生成区域 可直接覆盖**/ + SysClientConvert INSTANCE = Mappers.getMapper(SysClientConvert.class); + + /** + * 实体类转换为VO类 + * + * @param sysClient + * @return 系统授权集合 + */ + SysClientVO convertSysClientVO(SysClient sysClient); + + /** + * VO类转换为实体类集合 + * + * @param sysClientVO + * @return 系统授权集合 + */ + SysClient convertSysClient(SysClientVO sysClientVO); + + /** + * 实体类转换为VO类集合 + * + * @param sysClientList + * @return 系统授权集合 + */ + List convertSysClientVOList(List sysClientList); + + /** + * VO类转换为实体类 + * + * @param sysClientVOList + * @return 系统授权集合 + */ + List convertSysClientList(List sysClientVOList); + + /** + * 实体类转换为VO类分页 + * + * @param sysClientPage + * @return 系统授权分页 + */ + Page convertSysClientVOPage(Page sysClientPage); + + /** + * VO类转换为实体类 + * + * @param sysClientVOPage + * @return 系统授权分页 + */ + Page convertSysClientPage(Page sysClientVOPage); + /** 代码生成区域 可直接覆盖END**/ + + /** 自定义代码区域 **/ + + + /** 自定义代码区域 END**/ +} diff --git a/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/domain/SysClient.java b/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/domain/SysClient.java new file mode 100644 index 00000000..121160f7 --- /dev/null +++ b/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/domain/SysClient.java @@ -0,0 +1,64 @@ +package com.fastbee.system.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableLogic; +import com.baomidou.mybatisplus.annotation.TableName; +import com.fastbee.common.core.domain.BaseEntity; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * 系统授权对象 sys_client + * + * @author zhuangpeng.li + * @date 2024-12-12 + */ +@EqualsAndHashCode(callSuper = true) +@ApiModel(value = "SysClient", description = "系统授权 sys_client") +@Data +@TableName("sys_client" ) +public class SysClient extends BaseEntity { + private static final long serialVersionUID=1L; + + /** id唯一标识 */ + @TableId(value = "id", type = IdType.AUTO) + @ApiModelProperty("id唯一标识") + private Long id; + + /** 客户端key */ + @ApiModelProperty("客户端key") + private String clientKey; + + /** 客户端秘钥 */ + @ApiModelProperty("客户端秘钥") + private String clientSecret; + + /** 客户端token */ + @ApiModelProperty("客户端token") + private String token; + + /** 授权类型 */ + @ApiModelProperty("授权类型") + private String grantType; + + /** 设备类型 */ + @ApiModelProperty("设备类型") + private String deviceType; + + /** token固定超时 */ + @ApiModelProperty("token固定超时") + private Long timeout; + + /** 是否生效(0-不生效,1-生效) */ + @ApiModelProperty("是否生效") + private String enable; + + /** 删除标志(0代表存在 2代表删除) */ + @ApiModelProperty("删除标志") + @TableLogic + private String delFlag; + +} diff --git a/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/domain/vo/SysClientVO.java b/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/domain/vo/SysClientVO.java new file mode 100644 index 00000000..2684c28c --- /dev/null +++ b/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/domain/vo/SysClientVO.java @@ -0,0 +1,105 @@ +package com.fastbee.system.domain.vo; + +import com.fastbee.common.annotation.Excel; +import com.fastbee.common.core.domain.PageEntity; +import com.fasterxml.jackson.annotation.JsonFormat; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.util.Date; + + +/** + * 系统授权对象 sys_client + * + * @author zhuangpeng.li + * @date 2024-12-12 + */ + +@EqualsAndHashCode(callSuper = true) +@ApiModel(value = "SysClientVO", description = "系统授权 sys_client") +@Data +public class SysClientVO extends PageEntity { + /** 代码生成区域 可直接覆盖**/ + /** id唯一标识 */ + @Excel(name = "id唯一标识") + @ApiModelProperty("id唯一标识") + private Long id; + + /** 客户端key */ + @Excel(name = "客户端key") + @ApiModelProperty("客户端key") + private String clientKey; + + /** 客户端秘钥 */ + @Excel(name = "客户端秘钥") + @ApiModelProperty("客户端秘钥") + private String clientSecret; + + /** 客户端token */ + @Excel(name = "客户端token") + @ApiModelProperty("客户端token") + private String token; + + /** 授权类型 */ + @Excel(name = "授权类型") + @ApiModelProperty("授权类型") + private String grantType; + + /** 设备类型 */ + @Excel(name = "设备类型") + @ApiModelProperty("设备类型") + private String deviceType; + + /** token固定超时 */ + @Excel(name = "token固定超时") + @ApiModelProperty("token固定超时") + private Long timeout; + + /** 是否生效(0-不生效,1-生效) */ + @ApiModelProperty("是否生效") + @Excel(name = "是否生效") + private String enable; + + /** 删除标志(0代表存在 2代表删除) */ + @ApiModelProperty("删除标志") + @Excel(name = "删除标志") + private String delFlag; + + /** 创建者 */ + @Excel(name = "创建者") + @ApiModelProperty("创建者") + private String createBy; + + /** 创建时间 */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @ApiModelProperty("创建时间") + @Excel(name = "创建时间") + private Date createTime; + + /** 更新者 */ + @Excel(name = "更新者") + @ApiModelProperty("更新者") + private String updateBy; + + /** 更新时间 */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @ApiModelProperty("更新时间") + @Excel(name = "更新时间") + private Date updateTime; + + /** 备注 */ + @Excel(name = "备注") + @ApiModelProperty("备注") + private String remark; + + + /** 代码生成区域 可直接覆盖END**/ + + /** 自定义代码区域 **/ + + + /** 自定义代码区域 END**/ +} diff --git a/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/mapper/SysClientMapper.java b/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/mapper/SysClientMapper.java new file mode 100644 index 00000000..141257e2 --- /dev/null +++ b/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/mapper/SysClientMapper.java @@ -0,0 +1,49 @@ +package com.fastbee.system.mapper; + + +import com.fastbee.common.mybatis.mapper.BaseMapperX; +import com.fastbee.system.domain.SysClient; + +import java.util.List; + +/** + * 系统授权Mapper接口 + * + * @author zhuangpeng.li + * @date 2024-07-26 + */ +public interface SysClientMapper extends BaseMapperX +{ + /** + * 查询系统授权 + * + * @param id 系统授权主键 + * @return 系统授权 + */ + public SysClient selectSysClientById(Long id); + + /** + * 查询系统授权列表 + * + * @param sysClient 系统授权 + * @return 系统授权集合 + */ + public List selectSysClientList(SysClient sysClient); + + /** + * 新增系统授权 + * + * @param sysClient 系统授权 + * @return 结果 + */ + public int insertSysClient(SysClient sysClient); + + /** + * 修改系统授权 + * + * @param sysClient 系统授权 + * @return 结果 + */ + public int updateSysClient(SysClient sysClient); + +} diff --git a/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/service/ISysClientService.java b/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/service/ISysClientService.java new file mode 100644 index 00000000..88f76c36 --- /dev/null +++ b/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/service/ISysClientService.java @@ -0,0 +1,73 @@ +package com.fastbee.system.service; + +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baomidou.mybatisplus.extension.service.IService; +import com.fastbee.system.domain.SysClient; +import com.fastbee.system.domain.vo.SysClientVO; + +import java.util.List; + +/** + * 系统授权Service接口 + * + * @author zhuangpeng.li + * @date 2024-07-26 + */ +public interface ISysClientService extends IService +{ + /** + * 查询系统授权 + * + * @param id 系统授权主键 + * @return 系统授权 + */ + public SysClient selectSysClientById(Long id); + + /** + * 查询系统授权列表 + * + * @param sysClient 系统授权 + * @return 系统授权集合 + */ + public List selectSysClientList(SysClient sysClient); + + /** + * 查询系统授权列表 + * + * @param sysClient 系统授权 + * @return 系统授权分页集合 + */ + Page pageSysClientVO(SysClient sysClient); + + /** + * 新增系统授权 + * + * @param sysClient 系统授权 + * @return 结果 + */ + public int insertSysClient(SysClient sysClient); + + /** + * 修改系统授权 + * + * @param sysClient 系统授权 + * @return 结果 + */ + public int updateSysClient(SysClient sysClient); + + /** + * 批量删除系统授权 + * + * @param ids 需要删除的系统授权主键集合 + * @return 结果 + */ + public int deleteSysClientByIds(Long[] ids); + + /** + * 删除系统授权信息 + * + * @param id 系统授权主键 + * @return 结果 + */ + public int deleteSysClientById(Long id); +} diff --git a/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/service/impl/SysClientServiceImpl.java b/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/service/impl/SysClientServiceImpl.java new file mode 100644 index 00000000..fd1c28e1 --- /dev/null +++ b/springboot/fastbee-service/fastbee-system-service/src/main/java/com/fastbee/system/service/impl/SysClientServiceImpl.java @@ -0,0 +1,147 @@ +package com.fastbee.system.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.fastbee.common.utils.DateUtils; +import com.fastbee.common.utils.StringUtils; +import com.fastbee.system.convert.SysClientConvert; +import com.fastbee.system.domain.SysClient; +import com.fastbee.system.domain.vo.SysClientVO; +import com.fastbee.system.mapper.SysClientMapper; +import com.fastbee.system.service.ISysClientService; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** + * 系统授权Service业务层处理 + * + * @author zhuangpeng.li + * @date 2024-07-26 + */ +@Service +public class SysClientServiceImpl extends ServiceImpl implements ISysClientService +{ + @Resource + private SysClientMapper sysClientMapper; + + /** + * 查询系统授权 + * + * @param id 系统授权主键 + * @return 系统授权 + */ + @Override + public SysClient selectSysClientById(Long id) + { + return sysClientMapper.selectById(id); + } + + /** + * 查询系统授权列表 + * + * @param sysClient 系统授权 + * @return 系统授权 + */ + @Override + public List selectSysClientList(SysClient sysClient) + { + LambdaQueryWrapper lqw = buildQueryWrapper(sysClient); + List sysClientList = baseMapper.selectList(lqw); + return SysClientConvert.INSTANCE.convertSysClientVOList(sysClientList); + } + + /** + * 查询系统授权分页列表 + * + * @param sysClient 系统授权 + * @return 系统授权 + */ + @Override + public Page pageSysClientVO(SysClient sysClient) { + LambdaQueryWrapper lqw = buildQueryWrapper(sysClient); + Page sysClientPage = baseMapper.selectPage(new Page<>(sysClient.getPageNum(), sysClient.getPageSize()), lqw); + return SysClientConvert.INSTANCE.convertSysClientVOPage(sysClientPage); + } + + private LambdaQueryWrapper buildQueryWrapper(SysClient query) { + Map params = query.getParams(); + LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); + lqw.eq(query.getId() != null, SysClient::getId, query.getId()); + lqw.eq(StringUtils.isNotBlank(query.getClientKey()), SysClient::getClientKey, query.getClientKey()); + lqw.eq(StringUtils.isNotBlank(query.getClientSecret()), SysClient::getClientSecret, query.getClientSecret()); + lqw.eq(StringUtils.isNotBlank(query.getToken()), SysClient::getToken, query.getToken()); + lqw.eq(StringUtils.isNotBlank(query.getGrantType()), SysClient::getGrantType, query.getGrantType()); + lqw.eq(StringUtils.isNotBlank(query.getDeviceType()), SysClient::getDeviceType, query.getDeviceType()); + lqw.eq(query.getTimeout() != null, SysClient::getTimeout, query.getTimeout()); + lqw.eq(StringUtils.isNotBlank(query.getEnable()), SysClient::getEnable, query.getEnable()); + lqw.eq(StringUtils.isNotBlank(query.getDelFlag()), SysClient::getDelFlag, query.getDelFlag()); + lqw.eq(StringUtils.isNotBlank(query.getCreateBy()), SysClient::getCreateBy, query.getCreateBy()); + lqw.eq(query.getCreateTime() != null, SysClient::getCreateTime, query.getCreateTime()); + lqw.eq(StringUtils.isNotBlank(query.getUpdateBy()), SysClient::getUpdateBy, query.getUpdateBy()); + lqw.eq(query.getUpdateTime() != null, SysClient::getUpdateTime, query.getUpdateTime()); + lqw.eq(StringUtils.isNotBlank(query.getRemark()), SysClient::getRemark, query.getRemark()); + + if (!Objects.isNull(params.get("beginTime")) && + !Objects.isNull(params.get("endTime"))) { + lqw.between(SysClient::getCreateTime, params.get("beginTime"), params.get("endTime")); + } + return lqw; + } + + /** + * 新增系统授权 + * + * @param sysClient 系统授权 + * @return 结果 + */ + @Override + public int insertSysClient(SysClient sysClient) + { + sysClient.setCreateTime(DateUtils.getNowDate()); + return sysClientMapper.insert(sysClient); + } + + /** + * 修改系统授权 + * + * @param sysClient 系统授权 + * @return 结果 + */ + @Override + public int updateSysClient(SysClient sysClient) + { + sysClient.setUpdateTime(DateUtils.getNowDate()); + return sysClientMapper.updateById(sysClient); + } + + /** + * 批量删除系统授权 + * + * @param ids 需要删除的系统授权主键 + * @return 结果 + */ + @Override + public int deleteSysClientByIds(Long[] ids) + { + return sysClientMapper.deleteBatchIds(Arrays.asList(ids)); + } + + /** + * 删除系统授权信息 + * + * @param id 系统授权主键 + * @return 结果 + */ + @Override + public int deleteSysClientById(Long id) + { + return sysClientMapper.deleteById(id); + } +} diff --git a/springboot/fastbee-service/fastbee-system-service/src/main/resources/mapper/system/SysClientMapper.xml b/springboot/fastbee-service/fastbee-system-service/src/main/resources/mapper/system/SysClientMapper.xml new file mode 100644 index 00000000..4a483d5f --- /dev/null +++ b/springboot/fastbee-service/fastbee-system-service/src/main/resources/mapper/system/SysClientMapper.xml @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + + + + + + + + + select id, client_key, client_secret, token, grant_type, device_type, timeout, enable, del_flag, create_by, create_time, update_by, update_time, remark from sys_client + + + + + + + + insert into sys_client + + client_key, + client_secret, + token, + grant_type, + device_type, + timeout, + enable, + del_flag, + create_by, + create_time, + update_by, + update_time, + remark, + + + #{clientKey}, + #{clientSecret}, + #{token}, + #{grantType}, + #{deviceType}, + #{timeout}, + #{enable}, + #{delFlag}, + #{createBy}, + #{createTime}, + #{updateBy}, + #{updateTime}, + #{remark}, + + + + + update sys_client + + client_key = #{clientKey}, + client_secret = #{clientSecret}, + token = #{token}, + grant_type = #{grantType}, + device_type = #{deviceType}, + timeout = #{timeout}, + enable = #{enable}, + del_flag = #{delFlag}, + create_by = #{createBy}, + create_time = #{createTime}, + update_by = #{updateBy}, + update_time = #{updateTime}, + remark = #{remark}, + + where id = #{id} + + +