修复域名绑定模块功能

This commit is contained in:
gxwebsoft
2024-03-27 17:59:25 +08:00
parent 229134096a
commit d436b3fad7
6 changed files with 122 additions and 52 deletions
@@ -1,10 +1,14 @@
package com.gxwebsoft.cms.controller; package com.gxwebsoft.cms.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.cms.entity.Domain; import com.gxwebsoft.cms.entity.Domain;
import com.gxwebsoft.cms.param.DomainParam; import com.gxwebsoft.cms.param.DomainParam;
import com.gxwebsoft.cms.service.DomainService; import com.gxwebsoft.cms.service.DomainService;
import com.gxwebsoft.common.core.utils.RedisUtil; import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.core.web.*; import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User; import com.gxwebsoft.common.system.entity.User;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
@@ -12,6 +16,9 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
import java.util.UUID;
import static com.gxwebsoft.common.core.utils.DomainUtils.DNSLookup;
/** /**
* 网站域名记录表控制器 * 网站域名记录表控制器
@@ -38,36 +45,45 @@ public class DomainController extends BaseController {
@ApiOperation("查询全部网站域名记录表") @ApiOperation("查询全部网站域名记录表")
@GetMapping() @GetMapping()
public ApiResult<List<Domain>> list(DomainParam param) { public ApiResult<List<Domain>> list(DomainParam param) {
PageParam<Domain, DomainParam> page = new PageParam<>(param);
page.setDefaultOrder("create_time desc");
return success(domainService.list(page.getOrderWrapper()));
// 使用关联查询 // 使用关联查询
//return success(domainService.listRel(param)); return success(domainService.listRel(param));
} }
@ApiOperation("根据id查询网站域名记录表") @ApiOperation("根据id查询网站域名记录表")
@GetMapping("/{id}") @GetMapping("/{id}")
public ApiResult<Domain> get(@PathVariable("id") Integer id) { public ApiResult<Domain> get(@PathVariable("id") Integer id) {
return success(domainService.getById(id));
// 使用关联查询 // 使用关联查询
//return success(domainService.getByIdRel(id)); return success(domainService.getByIdRel(id));
} }
@ApiOperation("添加网站域名记录表") @ApiOperation("添加网站域名记录表")
@PostMapping() @PostMapping()
public ApiResult<?> save(@RequestBody Domain domain) { public ApiResult<?> save(@RequestBody Domain domain) {
// 记录当前登录用户id // 记录当前登录用户id
User loginUser = getLoginUser(); User loginUser = getLoginUser();
if (loginUser != null) { if (loginUser != null) {
domain.setUserId(loginUser.getUserId()); domain.setUserId(loginUser.getUserId());
domain.setTenantId(loginUser.getTenantId()); domain.setTenantId(loginUser.getTenantId());
} domain.setHostValue(UUID.randomUUID() + ".wsdns.cn");
if (domainService.save(domain)) { }
// 重写缓存 // 检查缓存是否已存在
String key = "Domain:" + domain.getDomain(); String key = "Domain:" + domain.getDomain();
redisUtil.set(key,domain.getTenantId()); final String string = redisUtil.get(key);
return success("添加成功"); if(string != null){
} return fail("您绑定的域名已存在");
}
// 是否有重复
if(domainService.count(new LambdaQueryWrapper<Domain>().eq(Domain::getDomain, domain.getDomain())) > 0){
return fail("您绑定的域名已存在");
}
// 是否超过最大绑定数量
if(domainService.count() > 20){
return fail("超过最大绑定限制");
}
if (domainService.save(domain)) {
return success("添加成功");
}
return fail("添加失败"); return fail("添加失败");
} }
@@ -120,4 +136,20 @@ public class DomainController extends BaseController {
return fail("删除失败"); return fail("删除失败");
} }
@ApiOperation("验证DNS解析信息是否填写正确")
@GetMapping("/resolvable/{id}")
public ApiResult<?> resolvable(@PathVariable("id") Integer id){
Domain domain = domainService.getByIdRel(id);
if(DNSLookup(domain)){
// 改变状态
domain.setStatus(1);
domainService.updateById(domain);
// 重写缓存
String key = "Domain:" + domain.getDomain();
redisUtil.set(key,domain.getTenantId());
return success("验证通过");
};
return fail("未检测到解析值");
}
} }
@@ -1,9 +1,6 @@
package com.gxwebsoft.cms.entity; package com.gxwebsoft.cms.entity;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@@ -32,6 +29,12 @@ public class Domain implements Serializable {
@ApiModelProperty(value = "域名") @ApiModelProperty(value = "域名")
private String domain; private String domain;
@ApiModelProperty(value = "主机记录")
private String hostName;
@ApiModelProperty(value = "记录值")
private String hostValue;
@ApiModelProperty(value = "状态") @ApiModelProperty(value = "状态")
private Integer status; private Integer status;
@@ -23,7 +23,7 @@ public class DomainServiceImpl extends ServiceImpl<DomainMapper, Domain> impleme
@Override @Override
public PageResult<Domain> pageRel(DomainParam param) { public PageResult<Domain> pageRel(DomainParam param) {
PageParam<Domain, DomainParam> page = new PageParam<>(param); PageParam<Domain, DomainParam> page = new PageParam<>(param);
//page.setDefaultOrder("create_time desc"); page.setDefaultOrder("status desc,create_time desc");
List<Domain> list = baseMapper.selectPageRel(page, param); List<Domain> list = baseMapper.selectPageRel(page, param);
return new PageResult<>(list, page.getTotal()); return new PageResult<>(list, page.getTotal());
} }
@@ -33,7 +33,7 @@ public class DomainServiceImpl extends ServiceImpl<DomainMapper, Domain> impleme
List<Domain> list = baseMapper.selectListRel(param); List<Domain> list = baseMapper.selectListRel(param);
// 排序 // 排序
PageParam<Domain, DomainParam> page = new PageParam<>(); PageParam<Domain, DomainParam> page = new PageParam<>();
//page.setDefaultOrder("create_time desc"); page.setDefaultOrder("status desc,create_time desc");
return page.sortRecords(list); return page.sortRecords(list);
} }
@@ -39,22 +39,15 @@ public class MybatisPlusConfig {
TenantLineHandler tenantLineHandler = new TenantLineHandler() { TenantLineHandler tenantLineHandler = new TenantLineHandler() {
@Override @Override
public Expression getTenantId() { public Expression getTenantId() {
// 从域名拿ID // 从域名拿ID
final String domain = request.getHeader("Domain"); String Domain = request.getHeader("Domain");
if(StrUtil.isNotBlank(domain)){ if (StrUtil.isNotBlank(Domain)) {
final String serverName = request.getServerName(); String key = "Domain:" + Domain;
System.out.println("serverName = " + serverName); String tenantId = redisUtil.get(key);
System.out.println("domain2 = " + domain); if(tenantId != null){
final String domainName = redisUtil.get("Domain:".concat(domain)); System.out.println("从域名拿TID = " + tenantId);
System.out.println("domainName = " + domainName); return new LongValue(tenantId);
return new LongValue(domainName); }
}
// 从设备请求头拿ID
String DeviceID = request.getHeader("Device-ID");
if (StrUtil.isNotBlank(DeviceID)) {
return new LongValue(10048);
} }
// 从请求头拿ID // 从请求头拿ID
final String tenantId = request.getHeader("tenantId"); final String tenantId = request.getHeader("tenantId");
@@ -0,0 +1,34 @@
package com.gxwebsoft.common.core.utils;
import com.gxwebsoft.cms.entity.Domain;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class DomainUtils {
public static boolean isDomainResolvable(String domain) {
try {
InetAddress.getByName(domain);
return true;
} catch (UnknownHostException e) {
return false;
}
}
public static boolean DNSLookup(Domain domain){
try {
// 获取域名对应的InetAddress对象
InetAddress inetAddress = InetAddress.getByName(domain.getDomain());
final String hostAddress = inetAddress.getHostAddress();
InetAddress inetAddress2 = InetAddress.getByName(domain.getHostValue());
final String hostAddress2 = inetAddress2.getHostAddress();
if(hostAddress.equals(hostAddress2)){
return true;
}
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
@@ -4,13 +4,10 @@ import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gxwebsoft.common.core.Constants; import com.gxwebsoft.common.core.Constants;
import com.gxwebsoft.common.core.exception.BusinessException; import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.utils.CacheClient; import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.core.utils.SignCheckUtil; import com.gxwebsoft.common.core.utils.SignCheckUtil;
import com.gxwebsoft.common.system.entity.User; import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.entity.UserInfo;
import com.gxwebsoft.common.system.service.UserService;
import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
@@ -30,10 +27,10 @@ import java.util.Map;
* @since 2017-06-10 10:10:19 * @since 2017-06-10 10:10:19
*/ */
public class BaseController { public class BaseController {
@Resource @Resource
private HttpServletRequest request; private HttpServletRequest request;
@Resource @Resource
private StringRedisTemplate stringRedisTemplate; private RedisUtil redisUtil;
/** /**
* 获取当前登录的user * 获取当前登录的user
@@ -71,14 +68,25 @@ public class BaseController {
* @return tenantId * @return tenantId
*/ */
public Integer getTenantId() { public Integer getTenantId() {
// 从登录用户拿tenantId // 1 从登录用户拿tenantId
User loginUser = getLoginUser(); User loginUser = getLoginUser();
if (loginUser != null) { if (loginUser != null) {
return loginUser.getTenantId(); return loginUser.getTenantId();
} }
// 从请求头拿tenantId // 2 从域名拿ID
if(StrUtil.isNotBlank(request.getHeader("tenantId"))){ String Domain = request.getHeader("Domain");
return Integer.valueOf(request.getHeader("tenantId")); if (StrUtil.isNotBlank(Domain)) {
String key = "Domain:" + Domain;
String tenantId = redisUtil.get(key);
if(tenantId != null){
System.out.println("从域名拿ID = " + tenantId);
return Integer.valueOf(tenantId);
}
}
// 3 从请求头拿ID
String tenantId = request.getHeader("tenantId");
if(StrUtil.isNotBlank(tenantId)){
return Integer.valueOf(tenantId);
} }
return null; return null;
} }
@@ -266,7 +274,7 @@ public class BaseController {
public String getAppSecret() { public String getAppSecret() {
String key = "cache5:AppSecret:" + Integer.valueOf(getAppId()); String key = "cache5:AppSecret:" + Integer.valueOf(getAppId());
System.out.println("key = " + key); System.out.println("key = " + key);
return stringRedisTemplate.opsForValue().get(key); return redisUtil.get(key);
} }
/** /**