识别绑定的域名换取tenantID

This commit is contained in:
gxwebsoft
2024-05-13 19:00:03 +08:00
parent 638fa61c61
commit a100c69981
4 changed files with 50 additions and 33 deletions

View File

@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.system.entity.User;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
@@ -15,6 +16,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
@@ -26,6 +28,8 @@ import java.util.Arrays;
*/
@Configuration
public class MybatisPlusConfig {
@Resource
private RedisUtil redisUtil;
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(HttpServletRequest request) {
@@ -33,18 +37,22 @@ public class MybatisPlusConfig {
// 多租户插件配置
TenantLineHandler tenantLineHandler = new TenantLineHandler() {
@Override
public Expression getTenantId() {
// String device_id = request.getHeader("device-id");
// System.out.println("device_id = " + device_id);
// 从设备请求头拿ID
String DeviceID = request.getHeader("Device-ID");
if (StrUtil.isNotBlank(DeviceID)) {
return new LongValue(10048);
// 从域名拿ID
String Domain = request.getHeader("Domain");
if (StrUtil.isNotBlank(Domain)) {
String key = "Domain:" + Domain;
String tenantId = redisUtil.get(key);
if(tenantId != null){
// System.out.println("从域名拿TID = " + tenantId);
return new LongValue(tenantId);
}
}
// 从请求头拿ID
final String tenantId = request.getHeader("tenantId");
if (tenantId != null) {
if(tenantId != null){
return new LongValue(tenantId);
}
return getLoginUserTenantId();

View File

@@ -73,10 +73,10 @@ public class SocketIOConfig implements InitializingBean {
config.setKeyStorePassword("123456"); // 设置证书密码
// 启动socket服务
// SocketIOServer server = new SocketIOServer(config);
// server.addListeners(socketIOHandler);
// server.start();
// ClientCache.setSocketIOServer(server);
// logger.debug("Netty SocketIO启动{}:{}",host,port);
SocketIOServer server = new SocketIOServer(config);
server.addListeners(socketIOHandler);
server.start();
ClientCache.setSocketIOServer(server);
logger.debug("Netty SocketIO启动{}:{}",host,port);
}
}

View File

@@ -5,13 +5,12 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gxwebsoft.common.core.Constants;
import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.system.entity.Company;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.mapper.CompanyMapper;
import com.gxwebsoft.common.system.service.CompanyService;
import com.gxwebsoft.common.system.service.UserService;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;
@@ -35,6 +34,8 @@ public class BaseController {
private UserService userService;
@Resource
private CompanyService companyService;
@Resource
private RedisUtil redisUtil;
/**
* 获取当前登录的user
@@ -72,14 +73,25 @@ public class BaseController {
* @return tenantId
*/
public Integer getTenantId() {
// 从登录用户拿tenantId
// 1 从登录用户拿tenantId
User loginUser = getLoginUser();
if (loginUser != null) {
return loginUser.getTenantId();
}
// 从请求头拿tenantId
if (StrUtil.isNotBlank(request.getHeader("tenantId"))) {
return Integer.valueOf(request.getHeader("tenantId"));
// 2 从域名拿ID
String Domain = request.getHeader("Domain");
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;
}

View File

@@ -4,7 +4,6 @@ import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
@@ -15,19 +14,19 @@ import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.config.ConfigProperties;
import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.security.JwtSubject;
import com.gxwebsoft.common.core.security.JwtUtil;
import com.gxwebsoft.common.core.utils.*;
import com.gxwebsoft.common.core.utils.CacheClient;
import com.gxwebsoft.common.core.utils.CommonUtil;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.ExistenceParam;
import com.gxwebsoft.common.system.entity.*;
import com.gxwebsoft.common.system.mapper.CompanyMapper;
import com.gxwebsoft.common.system.param.LoginParam;
import com.gxwebsoft.common.system.param.SmsCaptchaParam;
import com.gxwebsoft.common.system.param.UpdatePasswordParam;
@@ -39,24 +38,20 @@ import com.wf.captcha.SpecCaptcha;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import static com.gxwebsoft.common.core.constants.PlatformConstants.MP_WEIXIN;
/**
* 登录认证控制器
*
@@ -92,11 +87,11 @@ public class MainController extends BaseController {
@PostMapping("/login")
public ApiResult<LoginResult> login(@RequestBody LoginParam param, HttpServletRequest request) {
String username = param.getUsername();
Integer tenantId = param.getTenantId();
Integer tenantId = getTenantId();
// 判断租户是否销毁
final Tenant tenant = tenantService.getById(tenantId);
if (tenant == null) {
throw new BusinessException("租户不存在");
throw new BusinessException("租户不存在".concat(tenantId.toString()));
}
// 登录账号|手机号码|邮箱登录
User user = userService.getByUsername(username, tenantId);
@@ -399,9 +394,11 @@ public class MainController extends BaseController {
@PostMapping("/loginBySms")
public ApiResult<LoginResult> loginBySms(@RequestBody LoginParam param, HttpServletRequest request) {
final String phone = param.getPhone();
final Integer tenantId = param.getTenantId();
final Integer tenantId = getTenantId();
final String code = param.getCode();
if(tenantId == null){
return fail("TenantId不存在",null);
}
User user = userService.getByUsername(phone, tenantId);
// 是否管理员
if(param.getIsAdmin() != null && !user.getIsAdmin()){