api key功能开发
This commit is contained in:
1 parent
e6ad3bd85c
commit
bc772b8b15
8 files changed
+109
-37
No files matched your search
@@ -113,20 +113,22 @@ public class AppApiKeysController extends BaseController {
|
||||
* 新增应用密钥
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('system:keys:add')" )
|
||||
@Log(title = "应用密钥" , businessType = BusinessType.INSERT)
|
||||
@Log(title = "应用密钥" , businessType = BusinessType.INSERT,
|
||||
isSaveRequestData = false, isSaveResponseData = false)
|
||||
@PostMapping
|
||||
public AjaxResult add(@RequestBody AppApiKeys appApiKeys) {
|
||||
return toAjax(appApiKeysService.insertAppApiKeys(appApiKeys, false));
|
||||
return AjaxResult.success(appApiKeysService.insertAppApiKeys(appApiKeys, false));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增应用密钥
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('system:keys:user-add')" )
|
||||
@Log(title = "应用密钥" , businessType = BusinessType.INSERT)
|
||||
@Log(title = "应用密钥" , businessType = BusinessType.INSERT,
|
||||
isSaveRequestData = false, isSaveResponseData = false)
|
||||
@PostMapping("/user" )
|
||||
public AjaxResult userAdd(@RequestBody AppApiKeys appApiKeys) {
|
||||
return toAjax(appApiKeysService.insertAppApiKeys(appApiKeys, true));
|
||||
return AjaxResult.success(appApiKeysService.insertAppApiKeys(appApiKeys, true));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.ruoyi.system.domain;
|
||||
|
||||
import java.util.Date;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import com.ruoyi.common.annotation.Excel;
|
||||
@@ -29,7 +30,7 @@ public class AppApiKeys extends BaseEntity
|
||||
private Long userId;
|
||||
|
||||
/** 密钥 */
|
||||
@Excel(name = "密钥")
|
||||
@JsonIgnore
|
||||
private String apiSecret;
|
||||
|
||||
/** 状态 */
|
||||
@@ -142,7 +143,7 @@ public class AppApiKeys extends BaseEntity
|
||||
.append("id", getId())
|
||||
.append("appId", getAppId())
|
||||
.append("userId", getUserId())
|
||||
.append("apiSecret", getApiSecret())
|
||||
.append("apiSecret", "<redacted>")
|
||||
.append("status", getStatus())
|
||||
.append("expiredAt", getExpiredAt())
|
||||
.append("createdAt", getCreatedAt())
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.Date;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import com.ruoyi.common.annotation.Excel;
|
||||
@@ -33,7 +34,7 @@ public class AppApiKeysPlus implements Serializable
|
||||
@Excel(name = "用户")
|
||||
private Long userId;
|
||||
/** 密钥 */
|
||||
@Excel(name = "密钥")
|
||||
@JsonIgnore
|
||||
private String apiSecret;
|
||||
/** 状态 */
|
||||
@Excel(name = "状态")
|
||||
@@ -140,7 +141,7 @@ public class AppApiKeysPlus implements Serializable
|
||||
.append("id", getId())
|
||||
.append("appId", getAppId())
|
||||
.append("userId", getUserId())
|
||||
.append("apiSecret", getApiSecret())
|
||||
.append("apiSecret", "<redacted>")
|
||||
.append("status", getStatus())
|
||||
.append("expiredAt", getExpiredAt())
|
||||
.append("createdAt", getCreatedAt())
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.ruoyi.system.domain.vo;
|
||||
|
||||
/** API Key 创建结果。apiSecret 只在创建成功响应中返回一次。 */
|
||||
public class AppApiKeyCreateResult {
|
||||
private final Long id;
|
||||
private final Long appId;
|
||||
private final String apiSecret;
|
||||
|
||||
public AppApiKeyCreateResult(Long id, Long appId, String apiSecret) {
|
||||
this.id = id;
|
||||
this.appId = appId;
|
||||
this.apiSecret = apiSecret;
|
||||
}
|
||||
public Long getId() { return id; }
|
||||
public Long getAppId() { return appId; }
|
||||
public String getApiSecret() { return apiSecret; }
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import com.ruoyi.system.domain.AppApiKeysPlus;
|
||||
*/
|
||||
public interface AppApiKeysMapper extends BaseMapper<AppApiKeysPlus>
|
||||
{
|
||||
int softDeleteByIds(@Param("ids") Long[] ids, @Param("deletedAt") java.util.Date deletedAt);
|
||||
/**
|
||||
* 查询【请填写功能名称】
|
||||
*
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.ruoyi.system.domain.AppApiKeysPlus;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.ruoyi.common.select.SelectItem;
|
||||
import com.ruoyi.system.domain.vo.AppApiKeyCreateResult;
|
||||
/**
|
||||
* 应用密钥Service接口
|
||||
*
|
||||
@@ -38,7 +39,7 @@ public interface IAppApiKeysService extends IService<AppApiKeysPlus>
|
||||
* @param appApiKeys 应用密钥
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertAppApiKeys(AppApiKeys appApiKeys,boolean isUser);
|
||||
public AppApiKeyCreateResult insertAppApiKeys(AppApiKeys appApiKeys,boolean isUser);
|
||||
|
||||
/**
|
||||
* 修改应用密钥
|
||||
|
||||
+65
-24
@@ -5,18 +5,25 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.ruoyi.common.select.SelectItem;
|
||||
import com.ruoyi.common.exception.CustomException;
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.system.domain.AppApiKeys;
|
||||
import com.ruoyi.system.domain.AppApiKeysPlus;
|
||||
import com.ruoyi.system.mapper.AppApiKeysMapper;
|
||||
import com.ruoyi.system.mapper.AppsMapper;
|
||||
import com.ruoyi.system.domain.Apps;
|
||||
import com.ruoyi.system.domain.vo.AppApiKeyCreateResult;
|
||||
import com.ruoyi.system.service.IAppApiKeysService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Base64;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* 应用密钥Service业务层处理
|
||||
@@ -29,12 +36,14 @@ public class AppApiKeysServiceImpl extends ServiceImpl<AppApiKeysMapper, AppApiK
|
||||
@Autowired
|
||||
private AppApiKeysMapper appApiKeysMapper;
|
||||
@Autowired
|
||||
private AppsMapper appsMapper;
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Override
|
||||
public List<AppApiKeysPlus> getByIds(List<Long> ids, boolean isUser, SFunction<AppApiKeysPlus, ?>... columns) {
|
||||
LambdaQueryWrapper<AppApiKeysPlus> lambdaQueryWrapper = new LambdaQueryWrapper<AppApiKeysPlus>()
|
||||
.select(columns).in(AppApiKeysPlus::getId, ids);
|
||||
.select(columns).in(AppApiKeysPlus::getId, ids).isNull(AppApiKeysPlus::getDeletedAt);
|
||||
if (isUser) {
|
||||
lambdaQueryWrapper.eq(AppApiKeysPlus::getUserId, SecurityUtils.getUserId());
|
||||
}
|
||||
@@ -76,8 +85,8 @@ public class AppApiKeysServiceImpl extends ServiceImpl<AppApiKeysMapper, AppApiK
|
||||
@Override
|
||||
public List<SelectItem> select(String keyword, boolean isUser) {
|
||||
LambdaQueryWrapper<AppApiKeysPlus> lambdaQueryWrapper = new LambdaQueryWrapper<AppApiKeysPlus>()
|
||||
.select(AppApiKeysPlus::getId, AppApiKeysPlus::getUserId, AppApiKeysPlus::getApiSecret)
|
||||
.like(AppApiKeysPlus::getApiSecret, keyword);
|
||||
.select(AppApiKeysPlus::getId, AppApiKeysPlus::getUserId)
|
||||
.isNull(AppApiKeysPlus::getDeletedAt);
|
||||
if (isUser) {
|
||||
lambdaQueryWrapper.eq(AppApiKeysPlus::getUserId, SecurityUtils.getUserId());
|
||||
}
|
||||
@@ -88,7 +97,7 @@ public class AppApiKeysServiceImpl extends ServiceImpl<AppApiKeysMapper, AppApiK
|
||||
List<SelectItem> result = new ArrayList<>();
|
||||
for (AppApiKeysPlus item : list) {
|
||||
SelectItem selectItem = new SelectItem();
|
||||
selectItem.setName(item.getApiSecret());
|
||||
selectItem.setName("Key #" + item.getId());
|
||||
selectItem.setId(item.getId());
|
||||
result.add(selectItem);
|
||||
}
|
||||
@@ -215,13 +224,23 @@ public class AppApiKeysServiceImpl extends ServiceImpl<AppApiKeysMapper, AppApiK
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int insertAppApiKeys(AppApiKeys appApiKeys, boolean isUser) {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public AppApiKeyCreateResult insertAppApiKeys(AppApiKeys appApiKeys, boolean isUser) {
|
||||
if (appApiKeys == null || appApiKeys.getAppId() == null) throw new CustomException("应用 ID 不能为空");
|
||||
Apps app = requireApp(appApiKeys.getAppId(), isUser);
|
||||
validateStatus(appApiKeys.getStatus());
|
||||
validateExpiry(appApiKeys.getExpiredAt());
|
||||
byte[] secretBytes = new byte[32];
|
||||
new SecureRandom().nextBytes(secretBytes);
|
||||
String secret = Base64.getUrlEncoder().withoutPadding().encodeToString(secretBytes);
|
||||
appApiKeys.setApiSecret(secret);
|
||||
appApiKeys.setUserId(app.getUserId());
|
||||
if (appApiKeys.getStatus() == null) appApiKeys.setStatus(1);
|
||||
appApiKeys.setCreatedAt(DateUtils.getNowDate());
|
||||
appApiKeys.setUpdatedAt(DateUtils.getNowDate());
|
||||
if (isUser) {
|
||||
appApiKeys.setUserId(SecurityUtils.getUserId());
|
||||
}
|
||||
return appApiKeysMapper.insertAppApiKeys(appApiKeys);
|
||||
appApiKeys.setDeletedAt(null);
|
||||
if (appApiKeysMapper.insertAppApiKeys(appApiKeys) != 1) throw new CustomException("API Key 创建失败");
|
||||
return new AppApiKeyCreateResult(appApiKeys.getId(), appApiKeys.getAppId(), secret);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -232,11 +251,16 @@ public class AppApiKeysServiceImpl extends ServiceImpl<AppApiKeysMapper, AppApiK
|
||||
*/
|
||||
@Override
|
||||
public int updateAppApiKeys(AppApiKeys appApiKeys, boolean isUser) {
|
||||
appApiKeys.setUpdatedAt(DateUtils.getNowDate());
|
||||
if (isUser) {
|
||||
checkUser(appApiKeys);
|
||||
}
|
||||
return appApiKeysMapper.updateAppApiKeys(appApiKeys);
|
||||
if (appApiKeys == null || appApiKeys.getId() == null) throw new CustomException("API Key ID 不能为空");
|
||||
AppApiKeys current = requireKey(appApiKeys.getId(), isUser);
|
||||
validateStatus(appApiKeys.getStatus());
|
||||
validateExpiry(appApiKeys.getExpiredAt());
|
||||
AppApiKeys update = new AppApiKeys();
|
||||
update.setId(current.getId());
|
||||
update.setStatus(appApiKeys.getStatus());
|
||||
update.setExpiredAt(appApiKeys.getExpiredAt());
|
||||
update.setUpdatedAt(DateUtils.getNowDate());
|
||||
return appApiKeysMapper.updateAppApiKeys(update);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -247,11 +271,9 @@ public class AppApiKeysServiceImpl extends ServiceImpl<AppApiKeysMapper, AppApiK
|
||||
*/
|
||||
@Override
|
||||
public int deleteAppApiKeysByIds(Long[] ids, boolean isUser) {
|
||||
if (isUser) {
|
||||
List<AppApiKeysPlus> list = getByIds(Arrays.asList(ids), false, AppApiKeysPlus::getUserId, AppApiKeysPlus::getId);
|
||||
checkUserPlus(list);
|
||||
}
|
||||
return appApiKeysMapper.deleteAppApiKeysByIds(ids);
|
||||
if (ids == null || ids.length == 0) throw new CustomException("请选择要删除的 API Key");
|
||||
for (Long id : ids) requireKey(id, isUser);
|
||||
return appApiKeysMapper.softDeleteByIds(ids, DateUtils.getNowDate());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -262,10 +284,29 @@ public class AppApiKeysServiceImpl extends ServiceImpl<AppApiKeysMapper, AppApiK
|
||||
*/
|
||||
@Override
|
||||
public int deleteAppApiKeysById(Long id, boolean isUser) {
|
||||
if (isUser) {
|
||||
AppApiKeysPlus vo = getById(id, AppApiKeysPlus::getUserId, AppApiKeysPlus::getId);
|
||||
checkUser(vo);
|
||||
}
|
||||
return appApiKeysMapper.deleteAppApiKeysById(id);
|
||||
return deleteAppApiKeysByIds(new Long[] { id }, isUser);
|
||||
}
|
||||
|
||||
private Apps requireApp(Long appId, boolean isUser) {
|
||||
Apps app = appsMapper.selectAppsById(appId);
|
||||
if (app == null) throw new CustomException("商户应用不存在或已删除");
|
||||
if (isUser && !Objects.equals(app.getUserId(), SecurityUtils.getUserId())) throw new CustomException("无权操作该应用");
|
||||
return app;
|
||||
}
|
||||
|
||||
private AppApiKeys requireKey(Long id, boolean isUser) {
|
||||
if (id == null) throw new CustomException("API Key ID 不能为空");
|
||||
AppApiKeys key = appApiKeysMapper.selectAppApiKeysById(id);
|
||||
if (key == null) throw new CustomException("API Key 不存在或已删除");
|
||||
if (isUser && !Objects.equals(key.getUserId(), SecurityUtils.getUserId())) throw new CustomException("无权操作该 API Key");
|
||||
return key;
|
||||
}
|
||||
|
||||
private void validateStatus(Integer status) {
|
||||
if (status != null && status != 0 && status != 1) throw new CustomException("API Key 状态只能为启用或停用");
|
||||
}
|
||||
|
||||
private void validateExpiry(Date expiredAt) {
|
||||
if (expiredAt != null && !expiredAt.after(new Date())) throw new CustomException("有效期必须晚于当前时间");
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
|
||||
<select id="selectAppApiKeysList" parameterType="AppApiKeys" resultMap="AppApiKeysResult">
|
||||
<include refid="selectAppApiKeysVo"/>
|
||||
<where>
|
||||
<where>
|
||||
and deleted_at is null
|
||||
<if test="appId != null "> and app_id = #{appId}</if>
|
||||
<if test="userId != null "> and user_id = #{userId}</if>
|
||||
<if test="apiSecret != null and apiSecret != ''"> and api_secret = #{apiSecret}</if>
|
||||
@@ -33,7 +34,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
|
||||
<select id="selectAppApiKeysById" parameterType="Long" resultMap="AppApiKeysResult">
|
||||
<include refid="selectAppApiKeysVo"/>
|
||||
where id = #{id}
|
||||
where id = #{id} and deleted_at is null
|
||||
</select>
|
||||
|
||||
<insert id="insertAppApiKeys" parameterType="AppApiKeys" useGeneratedKeys="true" keyProperty="id">
|
||||
@@ -72,7 +73,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
<if test="deletedAt != null">deleted_at = #{deletedAt},</if>
|
||||
<if test="updatedAt != null">updated_at = #{updatedAt},</if>
|
||||
</trim>
|
||||
where id = #{id}
|
||||
where id = #{id} and deleted_at is null
|
||||
</update>
|
||||
|
||||
<update id="softDeleteByIds">
|
||||
update app_api_keys
|
||||
set status = 0, deleted_at = #{deletedAt}, updated_at = #{deletedAt}
|
||||
where deleted_at is null and id in
|
||||
<foreach item="id" collection="ids" open="(" separator="," close=")">#{id}</foreach>
|
||||
</update>
|
||||
|
||||
<delete id="deleteAppApiKeysById" parameterType="Long">
|
||||
@@ -85,4 +93,4 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
#{id}
|
||||
</foreach>
|
||||
</delete>
|
||||
</mapper>
|
||||
</mapper>
|
||||
Reference in new issue
Block a user