31 changed files with 953 additions and 59 deletions
@ -0,0 +1,8 @@ |
|||||
|
package org.dromara.system.api; |
||||
|
|
||||
|
import org.dromara.system.api.domain.vo.RemoteSubscribeApiVo; |
||||
|
|
||||
|
public interface RemoteSubscribeService { |
||||
|
RemoteSubscribeApiVo getPrivateKey(String subscribeApiCode); |
||||
|
|
||||
|
} |
@ -0,0 +1,49 @@ |
|||||
|
package org.dromara.system.api.domain.vo; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.io.Serial; |
||||
|
import java.io.Serializable; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Data |
||||
|
public class RemoteSubscribeApiVo implements Serializable { |
||||
|
|
||||
|
@Serial |
||||
|
private static final long serialVersionUID = 1L; |
||||
|
|
||||
|
|
||||
|
private Long id; |
||||
|
|
||||
|
/** |
||||
|
* 订阅名称 |
||||
|
*/ |
||||
|
private String subscribeName; |
||||
|
|
||||
|
/** |
||||
|
* 订阅编码 |
||||
|
*/ |
||||
|
private String subscribeCode; |
||||
|
|
||||
|
/** |
||||
|
* 订阅人 |
||||
|
*/ |
||||
|
private List<String> subscribeUser; |
||||
|
|
||||
|
/** |
||||
|
* 公钥 |
||||
|
*/ |
||||
|
private String publicKey; |
||||
|
|
||||
|
/** |
||||
|
*私钥 |
||||
|
*/ |
||||
|
private String privateKey; |
||||
|
|
||||
|
/** |
||||
|
*是否开启 |
||||
|
*/ |
||||
|
private Boolean isEnable; |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
package org.dromara.auth.form; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
import org.dromara.common.core.domain.model.LoginBody; |
||||
|
|
||||
|
/** |
||||
|
* 免密登录对象 |
||||
|
* |
||||
|
* @author Lion Li |
||||
|
*/ |
||||
|
@Data |
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
public class CasLoginBody extends LoginBody { |
||||
|
|
||||
|
/** |
||||
|
* 用户名 |
||||
|
*/ |
||||
|
private String username; |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,56 @@ |
|||||
|
package org.dromara.auth.service.impl; |
||||
|
|
||||
|
|
||||
|
import cn.dev33.satoken.stp.SaLoginModel; |
||||
|
import cn.dev33.satoken.stp.StpUtil; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.dubbo.config.annotation.DubboReference; |
||||
|
import org.dromara.auth.domain.vo.LoginVo; |
||||
|
import org.dromara.auth.form.CasLoginBody; |
||||
|
import org.dromara.auth.service.IAuthStrategy; |
||||
|
import org.dromara.common.json.utils.JsonUtils; |
||||
|
import org.dromara.common.satoken.utils.LoginHelper; |
||||
|
import org.dromara.common.tenant.helper.TenantHelper; |
||||
|
import org.dromara.system.api.RemoteUserService; |
||||
|
import org.dromara.system.api.domain.vo.RemoteClientVo; |
||||
|
import org.dromara.system.api.model.LoginUser; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Service("cas" + IAuthStrategy.BASE_NAME) |
||||
|
@RequiredArgsConstructor |
||||
|
public class CasAuthStrategy implements IAuthStrategy{ |
||||
|
|
||||
|
@DubboReference |
||||
|
private RemoteUserService remoteUserService; |
||||
|
|
||||
|
@Override |
||||
|
public LoginVo login(String body, RemoteClientVo client) { |
||||
|
CasLoginBody loginBody = JsonUtils.parseObject(body, CasLoginBody.class); |
||||
|
String tenantId = loginBody.getTenantId(); |
||||
|
String username = loginBody.getUsername(); |
||||
|
|
||||
|
LoginUser loginUser = TenantHelper.dynamic(tenantId, () -> { |
||||
|
return remoteUserService.getUserInfo(username, tenantId); |
||||
|
}); |
||||
|
|
||||
|
loginUser.setClientKey(client.getClientKey()); |
||||
|
loginUser.setDeviceType(client.getDeviceType()); |
||||
|
SaLoginModel model = new SaLoginModel(); |
||||
|
model.setDevice(client.getDeviceType()); |
||||
|
// 例如: 后台用户30分钟过期 app用户1天过期
|
||||
|
model.setTimeout(client.getTimeout()); |
||||
|
model.setActiveTimeout(client.getActiveTimeout()); |
||||
|
model.setExtra(LoginHelper.CLIENT_KEY, client.getClientId()); |
||||
|
|
||||
|
// 生成token
|
||||
|
LoginHelper.login(loginUser, model); |
||||
|
|
||||
|
LoginVo loginVo = new LoginVo(); |
||||
|
loginVo.setAccessToken(StpUtil.getTokenValue()); |
||||
|
loginVo.setExpireIn(StpUtil.getTokenTimeout()); |
||||
|
loginVo.setClientId(client.getClientId()); |
||||
|
return loginVo; |
||||
|
} |
||||
|
} |
@ -0,0 +1,108 @@ |
|||||
|
package org.dromara.auth.util; |
||||
|
|
||||
|
import org.apache.http.HttpEntity; |
||||
|
import org.apache.http.HttpResponse; |
||||
|
import org.apache.http.client.ClientProtocolException; |
||||
|
import org.apache.http.client.config.RequestConfig; |
||||
|
import org.apache.http.client.methods.*; |
||||
|
import org.apache.http.entity.ContentType; |
||||
|
import org.apache.http.entity.StringEntity; |
||||
|
import org.apache.http.impl.client.CloseableHttpClient; |
||||
|
import org.apache.http.impl.client.HttpClients; |
||||
|
import org.apache.http.util.EntityUtils; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
public class HttpClientUtil { |
||||
|
|
||||
|
// 默认超时时间
|
||||
|
private static final int TIMEOUT = 5000; |
||||
|
|
||||
|
// 创建默认的 HttpClient
|
||||
|
private static CloseableHttpClient createHttpClient() { |
||||
|
RequestConfig config = RequestConfig.custom() |
||||
|
.setConnectTimeout(TIMEOUT) |
||||
|
.setConnectionRequestTimeout(TIMEOUT) |
||||
|
.setSocketTimeout(TIMEOUT) |
||||
|
.build(); |
||||
|
return HttpClients.custom() |
||||
|
.setDefaultRequestConfig(config) |
||||
|
.build(); |
||||
|
} |
||||
|
|
||||
|
// 发送 GET 请求
|
||||
|
public static String sendGet(String url, Map<String, String> headers) throws IOException { |
||||
|
HttpGet httpGet = new HttpGet(url); |
||||
|
if (headers != null) { |
||||
|
headers.forEach(httpGet::addHeader); |
||||
|
} |
||||
|
|
||||
|
try (CloseableHttpClient httpClient = createHttpClient(); |
||||
|
CloseableHttpResponse response = httpClient.execute(httpGet)) { |
||||
|
|
||||
|
return handleResponse(response); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 发送 POST 请求
|
||||
|
public static String sendPost(String url, String body, Map<String, String> headers) throws IOException { |
||||
|
HttpPost httpPost = new HttpPost(url); |
||||
|
if (headers != null) { |
||||
|
headers.forEach(httpPost::addHeader); |
||||
|
} |
||||
|
if (body != null) { |
||||
|
HttpEntity entity = new StringEntity(body, ContentType.APPLICATION_JSON); |
||||
|
httpPost.setEntity(entity); |
||||
|
} |
||||
|
|
||||
|
try (CloseableHttpClient httpClient = createHttpClient(); |
||||
|
CloseableHttpResponse response = httpClient.execute(httpPost)) { |
||||
|
|
||||
|
return handleResponse(response); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 发送 PUT 请求
|
||||
|
public static String sendPut(String url, String body, Map<String, String> headers) throws IOException { |
||||
|
HttpPut httpPut = new HttpPut(url); |
||||
|
if (headers != null) { |
||||
|
headers.forEach(httpPut::addHeader); |
||||
|
} |
||||
|
if (body != null) { |
||||
|
HttpEntity entity = new StringEntity(body, ContentType.APPLICATION_JSON); |
||||
|
httpPut.setEntity(entity); |
||||
|
} |
||||
|
|
||||
|
try (CloseableHttpClient httpClient = createHttpClient(); |
||||
|
CloseableHttpResponse response = httpClient.execute(httpPut)) { |
||||
|
|
||||
|
return handleResponse(response); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 发送 DELETE 请求
|
||||
|
public static String sendDelete(String url, Map<String, String> headers) throws IOException { |
||||
|
HttpDelete httpDelete = new HttpDelete(url); |
||||
|
if (headers != null) { |
||||
|
headers.forEach(httpDelete::addHeader); |
||||
|
} |
||||
|
|
||||
|
try (CloseableHttpClient httpClient = createHttpClient(); |
||||
|
CloseableHttpResponse response = httpClient.execute(httpDelete)) { |
||||
|
|
||||
|
return handleResponse(response); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 处理响应
|
||||
|
private static String handleResponse(HttpResponse response) throws IOException { |
||||
|
int statusCode = response.getStatusLine().getStatusCode(); |
||||
|
if (statusCode >= 200 && statusCode < 300) { |
||||
|
HttpEntity entity = response.getEntity(); |
||||
|
return entity != null ? EntityUtils.toString(entity) : null; |
||||
|
} else { |
||||
|
throw new ClientProtocolException("Unexpected response status: " + statusCode); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,64 @@ |
|||||
|
package org.dromara.system.controller.system; |
||||
|
|
||||
|
|
||||
|
import cn.dev33.satoken.annotation.SaCheckPermission; |
||||
|
import jakarta.validation.constraints.NotEmpty; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.dromara.common.core.domain.R; |
||||
|
import org.dromara.common.core.validate.AddGroup; |
||||
|
import org.dromara.common.core.validate.EditGroup; |
||||
|
import org.dromara.common.encrypt.utils.EncryptUtils; |
||||
|
import org.dromara.common.idempotent.annotation.RepeatSubmit; |
||||
|
import org.dromara.common.log.annotation.Log; |
||||
|
import org.dromara.common.log.enums.BusinessType; |
||||
|
import org.dromara.common.web.core.BaseController; |
||||
|
import org.dromara.system.domain.SubscribeApi; |
||||
|
import org.dromara.system.domain.bo.SubscribeApiBo; |
||||
|
import org.dromara.system.service.ISubscribeApiService; |
||||
|
import org.springframework.validation.annotation.Validated; |
||||
|
import org.springframework.web.bind.annotation.*; |
||||
|
|
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Validated |
||||
|
@RequiredArgsConstructor |
||||
|
@RestController |
||||
|
@RequestMapping("/subscribe") |
||||
|
public class SubscribeApiController extends BaseController { |
||||
|
|
||||
|
private final ISubscribeApiService iSubscribeApiService; |
||||
|
|
||||
|
|
||||
|
@Log(title = "订阅添加", businessType = BusinessType.INSERT) |
||||
|
@RepeatSubmit() |
||||
|
@PostMapping() |
||||
|
public R<SubscribeApi> add(@Validated(AddGroup.class) @RequestBody SubscribeApiBo bo) { |
||||
|
return R.ok(iSubscribeApiService.addSubscribeApi(bo)); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@Log(title = "订阅编辑", businessType = BusinessType.UPDATE) |
||||
|
@RepeatSubmit() |
||||
|
@PutMapping() |
||||
|
public R<SubscribeApi> edit(@Validated(EditGroup.class) @RequestBody SubscribeApiBo bo) { |
||||
|
return R.ok(iSubscribeApiService.editSubscribeApi(bo)); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@Log(title = "订阅删除", businessType = BusinessType.DELETE) |
||||
|
@DeleteMapping("/delete/{id}") |
||||
|
public R<Boolean> remove(@PathVariable Long id) { |
||||
|
return R.ok(iSubscribeApiService.deleteSubscribeApi(id)); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 生成秘钥(公钥和私钥) |
||||
|
*/ |
||||
|
@GetMapping("/generate") |
||||
|
public R<Map<String,String>> generateRsaKey() { |
||||
|
|
||||
|
return R.ok(EncryptUtils.generateRsaKey()); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,59 @@ |
|||||
|
package org.dromara.system.domain; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.annotation.IdType; |
||||
|
import com.baomidou.mybatisplus.annotation.TableField; |
||||
|
import com.baomidou.mybatisplus.annotation.TableId; |
||||
|
import com.baomidou.mybatisplus.annotation.TableName; |
||||
|
import lombok.Data; |
||||
|
import org.dromara.system.handle.ListTypeHandler; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* 第三方接口订阅类 |
||||
|
*/ |
||||
|
@Data |
||||
|
@TableName(value = "subscribe_api",autoResultMap = true) |
||||
|
public class SubscribeApi { |
||||
|
|
||||
|
@TableId(type = IdType.ASSIGN_ID) |
||||
|
private Long id; |
||||
|
|
||||
|
/** |
||||
|
* 订阅名称 |
||||
|
*/ |
||||
|
@TableField(value = "subscribe_name") |
||||
|
private String subscribeName; |
||||
|
|
||||
|
/** |
||||
|
* 订阅编码 |
||||
|
*/ |
||||
|
@TableField(value = "subscribe_code") |
||||
|
private String subscribeCode; |
||||
|
|
||||
|
/** |
||||
|
* 订阅人 |
||||
|
*/ |
||||
|
@TableField(value = "subscribe_user",typeHandler = ListTypeHandler.class) |
||||
|
private List<String> subscribeUser; |
||||
|
|
||||
|
/** |
||||
|
* 公钥 |
||||
|
*/ |
||||
|
@TableField(value = "public_key") |
||||
|
private String publicKey; |
||||
|
|
||||
|
/** |
||||
|
*私钥 |
||||
|
*/ |
||||
|
@TableField(value = "private_key") |
||||
|
private String privateKey; |
||||
|
|
||||
|
/** |
||||
|
*是否开启 |
||||
|
*/ |
||||
|
@TableField(value = "is_enable") |
||||
|
private Boolean isEnable; |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,55 @@ |
|||||
|
package org.dromara.system.domain.bo; |
||||
|
|
||||
|
|
||||
|
import io.github.linpeilie.annotations.AutoMapper; |
||||
|
import jakarta.validation.constraints.NotNull; |
||||
|
import lombok.Data; |
||||
|
import org.dromara.common.core.validate.AddGroup; |
||||
|
import org.dromara.common.core.validate.EditGroup; |
||||
|
import org.dromara.system.domain.SubscribeApi; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
@Data |
||||
|
@AutoMapper(target = SubscribeApi.class, reverseConvertGenerate = false) |
||||
|
public class SubscribeApiBo { |
||||
|
|
||||
|
|
||||
|
private Long id; |
||||
|
|
||||
|
/** |
||||
|
* 订阅名称 |
||||
|
*/ |
||||
|
@NotNull(message = "订阅名称不能为空", groups = { EditGroup.class, AddGroup.class }) |
||||
|
private String subscribeName; |
||||
|
|
||||
|
/** |
||||
|
* 订阅编码 |
||||
|
*/ |
||||
|
@NotNull(message = "订阅编码不能为空", groups = { EditGroup.class, AddGroup.class }) |
||||
|
private String subscribeCode; |
||||
|
|
||||
|
/** |
||||
|
* 订阅人 |
||||
|
*/ |
||||
|
private List<String> subscribeUser; |
||||
|
|
||||
|
/** |
||||
|
* 公钥 |
||||
|
*/ |
||||
|
@NotNull(message = "公钥不能为空", groups = { EditGroup.class, AddGroup.class }) |
||||
|
private String publicKey; |
||||
|
|
||||
|
/** |
||||
|
*私钥 |
||||
|
*/ |
||||
|
@NotNull(message = "私钥不能为空", groups = { EditGroup.class, AddGroup.class }) |
||||
|
private String privateKey; |
||||
|
|
||||
|
/** |
||||
|
*是否开启 |
||||
|
*/ |
||||
|
private Boolean isEnable; |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
package org.dromara.system.dubbo; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.apache.dubbo.config.annotation.DubboService; |
||||
|
import org.dromara.common.core.utils.MapstructUtils; |
||||
|
import org.dromara.system.api.RemoteSubscribeService; |
||||
|
import org.dromara.system.api.domain.vo.RemoteSubscribeApiVo; |
||||
|
import org.dromara.system.domain.SubscribeApi; |
||||
|
import org.dromara.system.service.ISubscribeApiService; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
|
||||
|
@RequiredArgsConstructor |
||||
|
@Service |
||||
|
@DubboService |
||||
|
public class RemoteSubscribeServiceImpl implements RemoteSubscribeService { |
||||
|
|
||||
|
private final ISubscribeApiService subscribeApiService; |
||||
|
|
||||
|
|
||||
|
@Override |
||||
|
public RemoteSubscribeApiVo getPrivateKey(String subscribeApiCode) { |
||||
|
SubscribeApi subscribeApi = subscribeApiService.getPrivateKey(subscribeApiCode); |
||||
|
return MapstructUtils.convert(subscribeApi, RemoteSubscribeApiVo.class); |
||||
|
} |
||||
|
} |
@ -0,0 +1,51 @@ |
|||||
|
package org.dromara.system.handle; |
||||
|
|
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.apache.ibatis.type.JdbcType; |
||||
|
import org.apache.ibatis.type.MappedJdbcTypes; |
||||
|
import org.apache.ibatis.type.MappedTypes; |
||||
|
import org.apache.ibatis.type.TypeHandler; |
||||
|
import org.dromara.system.utils.JsonUtil; |
||||
|
|
||||
|
import java.sql.CallableStatement; |
||||
|
import java.sql.PreparedStatement; |
||||
|
import java.sql.ResultSet; |
||||
|
import java.sql.SQLException; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@MappedJdbcTypes(JdbcType.VARCHAR) // 数据库类型
|
||||
|
@MappedTypes({List.class}) // java数据类型
|
||||
|
public class ListTypeHandler implements TypeHandler<List<String>> { |
||||
|
|
||||
|
@Override |
||||
|
public void setParameter(PreparedStatement preparedStatement, int i, List<String> alertTypeList, JdbcType jdbcType) throws SQLException { |
||||
|
preparedStatement.setString(i, JsonUtil.toJson(alertTypeList)); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<String> getResult(ResultSet resultSet, String s) throws SQLException { |
||||
|
if (StringUtils.isBlank(resultSet.getString(s))) { |
||||
|
return new ArrayList<>(); |
||||
|
} |
||||
|
return JsonUtil.jsonToList(resultSet.getString(s), String.class); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<String> getResult(ResultSet resultSet, int i) throws SQLException { |
||||
|
if (StringUtils.isBlank(resultSet.getString(i))) { |
||||
|
return new ArrayList<>(); |
||||
|
} |
||||
|
return JsonUtil.jsonToList(resultSet.getString(i), String.class); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<String> getResult(CallableStatement callableStatement, int i) throws SQLException { |
||||
|
String value = callableStatement.getString(i); |
||||
|
|
||||
|
if (StringUtils.isBlank(value)) { |
||||
|
return new ArrayList<>(); |
||||
|
} |
||||
|
return JsonUtil.jsonToList(value, String.class); |
||||
|
} |
||||
|
} |
@ -0,0 +1,7 @@ |
|||||
|
package org.dromara.system.mapper; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
|
import org.dromara.system.domain.SubscribeApi; |
||||
|
|
||||
|
public interface SubscribeApiMapper extends BaseMapper<SubscribeApi> { |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
package org.dromara.system.service; |
||||
|
|
||||
|
import org.dromara.system.domain.SubscribeApi; |
||||
|
import org.dromara.system.domain.bo.SubscribeApiBo; |
||||
|
|
||||
|
public interface ISubscribeApiService { |
||||
|
SubscribeApi addSubscribeApi(SubscribeApiBo bo); |
||||
|
|
||||
|
SubscribeApi editSubscribeApi(SubscribeApiBo bo); |
||||
|
|
||||
|
Boolean deleteSubscribeApi(Long id); |
||||
|
|
||||
|
SubscribeApi getPrivateKey(String subscribeApiCode); |
||||
|
} |
@ -0,0 +1,51 @@ |
|||||
|
package org.dromara.system.service.impl; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.dromara.common.core.utils.MapstructUtils; |
||||
|
import org.dromara.system.domain.SubscribeApi; |
||||
|
import org.dromara.system.domain.bo.SubscribeApiBo; |
||||
|
import org.dromara.system.mapper.SubscribeApiMapper; |
||||
|
import org.dromara.system.service.ISubscribeApiService; |
||||
|
import org.springframework.beans.BeanUtils; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
@Service |
||||
|
@RequiredArgsConstructor |
||||
|
public class ISubscribeApiServiceImpl extends ServiceImpl<SubscribeApiMapper,SubscribeApi> implements ISubscribeApiService { |
||||
|
|
||||
|
|
||||
|
@Override |
||||
|
public SubscribeApi addSubscribeApi(SubscribeApiBo bo) { |
||||
|
SubscribeApi convert = MapstructUtils.convert(bo, SubscribeApi.class); |
||||
|
|
||||
|
this.baseMapper.insert(convert); |
||||
|
|
||||
|
return convert; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public SubscribeApi editSubscribeApi(SubscribeApiBo bo) { |
||||
|
SubscribeApi subscribeApi = this.baseMapper.selectById(bo.getId()); |
||||
|
|
||||
|
BeanUtils.copyProperties(bo, subscribeApi); |
||||
|
|
||||
|
this.baseMapper.updateById(subscribeApi); |
||||
|
|
||||
|
return subscribeApi; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Boolean deleteSubscribeApi(Long id) { |
||||
|
return this.baseMapper.deleteById(id) > 0; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public SubscribeApi getPrivateKey(String subscribeApiCode) { |
||||
|
LambdaQueryWrapper<SubscribeApi> wrapper = new LambdaQueryWrapper<>(); |
||||
|
wrapper.eq(SubscribeApi::getSubscribeCode, subscribeApiCode); |
||||
|
|
||||
|
return this.baseMapper.selectOne(wrapper); |
||||
|
} |
||||
|
} |
@ -0,0 +1,126 @@ |
|||||
|
package org.dromara.system.utils; |
||||
|
|
||||
|
import cn.hutool.core.util.ObjectUtil; |
||||
|
import com.fasterxml.jackson.core.JsonProcessingException; |
||||
|
import com.fasterxml.jackson.core.type.TypeReference; |
||||
|
import com.fasterxml.jackson.databind.JavaType; |
||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
|
||||
|
import java.lang.reflect.Field; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.LinkedHashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
/** |
||||
|
* 定义响应结构 |
||||
|
*/ |
||||
|
public class JsonUtil { |
||||
|
|
||||
|
private static ObjectMapper MAPPER; |
||||
|
static{ |
||||
|
MAPPER=new ObjectMapper(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 将对象转换成json字符串。 |
||||
|
* @param data |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String toJson(Object data){ |
||||
|
String string = null; |
||||
|
try { |
||||
|
string = MAPPER.writeValueAsString(data); |
||||
|
if(StringUtils.isEmpty(string)){ |
||||
|
return null; |
||||
|
} |
||||
|
return string; |
||||
|
} catch (JsonProcessingException e) { |
||||
|
e.printStackTrace(); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 将json结果集转化为对象 |
||||
|
* |
||||
|
* @param jsonData json数据 |
||||
|
* @return |
||||
|
*/ |
||||
|
public static <T> T jsonToPojo(String jsonData, Class<T> beanType) { |
||||
|
try { |
||||
|
T t = MAPPER.readValue(jsonData, beanType); |
||||
|
return t; |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 将json数据转换成pojo对象list |
||||
|
* <p>Title: jsonToList</p> |
||||
|
* <p>Description: </p> |
||||
|
* @param jsonData |
||||
|
* @param beanType |
||||
|
* @return |
||||
|
*/ |
||||
|
public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) { |
||||
|
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType); |
||||
|
try { |
||||
|
List<T> list = MAPPER.readValue(jsonData, javaType); |
||||
|
return list; |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 将Object对象里面的属性和值转化成Map对象 |
||||
|
* |
||||
|
* @param obj |
||||
|
* @return |
||||
|
* @throws IllegalAccessException |
||||
|
*/ |
||||
|
public static Map<String, Object> objectToMap(Object obj){ |
||||
|
try { |
||||
|
Map<String, Object> map = new HashMap<String,Object>(); |
||||
|
Class<?> clazz = obj.getClass(); |
||||
|
for (Field field : clazz.getDeclaredFields()) { |
||||
|
field.setAccessible(true); |
||||
|
String fieldName = field.getName(); |
||||
|
if(ObjectUtil.isNotEmpty(field.get(obj))){ |
||||
|
Object value = field.get(obj); |
||||
|
map.put(fieldName, value); |
||||
|
}else{ |
||||
|
map.put(fieldName, ""); |
||||
|
} |
||||
|
} |
||||
|
return map; |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static Map<String, Object> jsonToMap(String json){ |
||||
|
try { |
||||
|
return MAPPER.readValue(json, new TypeReference<Map<String, Object>>(){}); |
||||
|
} catch (JsonProcessingException e) { |
||||
|
e.printStackTrace(); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static LinkedHashMap<String, String> jsonToStrMap(String json){ |
||||
|
try { |
||||
|
return MAPPER.readValue(json, new TypeReference<LinkedHashMap<String, String>>(){}); |
||||
|
} catch (JsonProcessingException e) { |
||||
|
e.printStackTrace(); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,7 @@ |
|||||
|
<?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="org.dromara.system.mapper.SubscribeApiMapper"> |
||||
|
|
||||
|
</mapper> |
Loading…
Reference in new issue