应用增加白名单

This commit is contained in:
liulu 2024-12-24 15:02:01 +08:00
parent 7af2547027
commit 17bc552dc5
4 changed files with 68 additions and 0 deletions

View File

@ -1,8 +1,13 @@
package com.sunyard.chsm.mapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sunyard.chsm.model.entity.IpWhitelist;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.util.CollectionUtils;
import java.util.Collections;
import java.util.List;
/**
* @author liulu
@ -11,6 +16,21 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface IpWhitelisttMapper extends BaseMapper<IpWhitelist> {
default List<IpWhitelist> selectByAppIds(List<Long> appIds) {
if (CollectionUtils.isEmpty(appIds)) {
return Collections.emptyList();
}
return selectList(
new LambdaQueryWrapper<IpWhitelist>()
.in(IpWhitelist::getAppId, appIds)
);
}
default void deleteByAppId(Long appId) {
delete(
new LambdaQueryWrapper<IpWhitelist>()
.eq(IpWhitelist::getAppId, appId)
);
}
}

View File

@ -25,6 +25,12 @@ public class AppSave {
*/
@NotEmpty(message = "密码服务不能为空")
private List<Long> serviceIds;
/**
* 应用白名单
*/
private List<String> whiteIps;
@Size(max = 500, message = "备注长度在1-500之间")
private String remark;

View File

@ -20,6 +20,7 @@ public class AppView {
private String statusText;
private String appKey;
private String appSecret;
private List<String> whiteIps;
private String remark;
private LocalDateTime createTime;

View File

@ -11,10 +11,13 @@ import com.sunyard.chsm.enums.EnableStatus;
import com.sunyard.chsm.mapper.AppServiceMapper;
import com.sunyard.chsm.mapper.ApplicationMapper;
import com.sunyard.chsm.mapper.CryptoServiceMapper;
import com.sunyard.chsm.mapper.IpWhitelisttMapper;
import com.sunyard.chsm.model.entity.AppService;
import com.sunyard.chsm.model.entity.Application;
import com.sunyard.chsm.model.entity.CryptoService;
import com.sunyard.chsm.model.entity.IpWhitelist;
import com.sunyard.chsm.service.ApplicationService;
import com.sunyard.ssp.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
@ -46,6 +49,10 @@ public class ApplicationServiceImpl implements ApplicationService {
private AppServiceMapper appServiceMapper;
@Resource
private CryptoServiceMapper cryptoServiceMapper;
@Resource
private IpWhitelisttMapper ipWhitelisttMapper;
@Resource
private SecurityUtil securityUtil;
@Override
public Page<AppView> selectPageList(AppQuery query) {
@ -68,12 +75,17 @@ public class ApplicationServiceImpl implements ApplicationService {
List<CryptoService> services = cryptoServiceMapper.selectBatchIds(appServices.stream().map(AppService::getServiceId).collect(Collectors.toList()));
Map<Long, String> snMap = services.stream().collect(Collectors.toMap(CryptoService::getId, CryptoService::getName));
List<IpWhitelist> appWhiteIps = ipWhitelisttMapper.selectByAppIds(appIds);
Map<Long, List<String>> appIpMap = appWhiteIps.stream()
.collect(Collectors.groupingBy(IpWhitelist::getAppId, Collectors.mapping(IpWhitelist::getIp, Collectors.toList())));
List<AppView> viewList = records.stream()
.map(it -> {
AppView view = new AppView();
BeanUtils.copyProperties(it, view);
List<Long> sIds = appServiceMap.getOrDefault(it.getId(), Collections.emptyList());
view.setServiceIds(sIds.stream().map(String::valueOf).collect(Collectors.toList()));
view.setWhiteIps(appIpMap.get(it.getId()));
String sn = sIds.stream()
.map(snMap::get)
.filter(Objects::nonNull)
@ -111,9 +123,24 @@ public class ApplicationServiceImpl implements ApplicationService {
as.setCreateTime(now);
appServiceMapper.insert(as);
}
if (!CollectionUtils.isEmpty(save.getWhiteIps())) {
for (String ip : save.getWhiteIps()) {
IpWhitelist whitelist = new IpWhitelist();
whitelist.setId(IdWorker.getId());
whitelist.setAppId(app.getId());
whitelist.setIp(ip);
whitelist.setScope("app");
whitelist.setStatus(EnableStatus.ENABLED.getCode());
whitelist.setCreator(securityUtil.getCurrUser().getRealname());
whitelist.setCreateTime(LocalDateTime.now());
ipWhitelisttMapper.insert(whitelist);
}
}
return app.getId();
}
@Override
public void update(AppSave update) {
Assert.notNull(update.getId(), "id不能为空");
@ -141,6 +168,20 @@ public class ApplicationServiceImpl implements ApplicationService {
as.setCreateTime(now);
appServiceMapper.insert(as);
}
ipWhitelisttMapper.deleteByAppId(update.getId());
if (!CollectionUtils.isEmpty(update.getWhiteIps())) {
for (String ip : update.getWhiteIps()) {
IpWhitelist whitelist = new IpWhitelist();
whitelist.setId(IdWorker.getId());
whitelist.setAppId(exist.getId());
whitelist.setIp(ip);
whitelist.setScope("app");
whitelist.setStatus(EnableStatus.ENABLED.getCode());
whitelist.setCreator(securityUtil.getCurrUser().getRealname());
whitelist.setCreateTime(LocalDateTime.now());
ipWhitelisttMapper.insert(whitelist);
}
}
}