add
This commit is contained in:
@ -47,3 +47,7 @@ mybatis-plus:
|
|||||||
# NONE:不做处理 WARNING:打印相关警告 FAILING:抛出异常和详细信息
|
# NONE:不做处理 WARNING:打印相关警告 FAILING:抛出异常和详细信息
|
||||||
autoMappingUnknownColumnBehavior: NONE
|
autoMappingUnknownColumnBehavior: NONE
|
||||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||||
|
|
||||||
|
logging:
|
||||||
|
level:
|
||||||
|
com.qiaoba: debug #开发环境输出sql日志
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.qiaoba.auth.filters;
|
package com.qiaoba.auth.filters;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.qiaoba.api.auth.service.AuthConfigApiService;
|
import com.qiaoba.api.auth.service.AuthConfigApiService;
|
||||||
import com.qiaoba.auth.constants.SecurityConstant;
|
import com.qiaoba.auth.constants.SecurityConstant;
|
||||||
import com.qiaoba.auth.entity.dto.OnlineUserDto;
|
import com.qiaoba.auth.entity.dto.OnlineUserDto;
|
||||||
@ -10,6 +11,7 @@ import com.qiaoba.common.redis.service.RedisService;
|
|||||||
import com.qiaoba.common.web.utils.ResponseUtil;
|
import com.qiaoba.common.web.utils.ResponseUtil;
|
||||||
import com.qiaoba.common.web.utils.UriUtil;
|
import com.qiaoba.common.web.utils.UriUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
@ -32,6 +34,7 @@ import java.util.Objects;
|
|||||||
* @since 2023-05-28 15:31:55
|
* @since 2023-05-28 15:31:55
|
||||||
*/
|
*/
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
public class AuthenticationCoreFilter extends OncePerRequestFilter {
|
public class AuthenticationCoreFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
private final RedisService redisService;
|
private final RedisService redisService;
|
||||||
@ -45,7 +48,7 @@ public class AuthenticationCoreFilter extends OncePerRequestFilter {
|
|||||||
protected void doFilterInternal(HttpServletRequest request,
|
protected void doFilterInternal(HttpServletRequest request,
|
||||||
HttpServletResponse response,
|
HttpServletResponse response,
|
||||||
FilterChain chain) throws ServletException, IOException {
|
FilterChain chain) throws ServletException, IOException {
|
||||||
|
log.debug(StrUtil.format("Start run AuthenticationCoreFilter, Uri: {}", request.getRequestURI()));
|
||||||
// 白名单 放行
|
// 白名单 放行
|
||||||
for (String uri : authConfigProperties.getWhitelist()) {
|
for (String uri : authConfigProperties.getWhitelist()) {
|
||||||
if (UriUtil.match(uri, request.getRequestURI())) {
|
if (UriUtil.match(uri, request.getRequestURI())) {
|
||||||
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.qiaoba.common.base.code;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 租户错误code
|
||||||
|
*
|
||||||
|
* @author ailanyin
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2023/6/12 13:33
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
public enum TenantErrorCode {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 未找到
|
||||||
|
*/
|
||||||
|
NOT_FIND(5010, "未找到租户信息"),
|
||||||
|
/**
|
||||||
|
* 禁用
|
||||||
|
*/
|
||||||
|
DISABLE(5011, "租户被禁用"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 过期
|
||||||
|
*/
|
||||||
|
EXPIRE(5012, "租户已过期");
|
||||||
|
|
||||||
|
private final Integer code;
|
||||||
|
private final String msg;
|
||||||
|
|
||||||
|
TenantErrorCode(Integer code, String msg) {
|
||||||
|
this.code = code;
|
||||||
|
this.msg = msg;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.qiaoba.common.base.code;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户错误code
|
||||||
|
*
|
||||||
|
* @author ailanyin
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2023/6/12 13:34
|
||||||
|
*/
|
||||||
|
public enum UserErrorCode {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,12 +1,14 @@
|
|||||||
package com.qiaoba.common.redis.service.impl;
|
package com.qiaoba.common.redis.service.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.qiaoba.common.base.constants.BaseConstant;
|
import com.qiaoba.common.base.constants.BaseConstant;
|
||||||
import com.qiaoba.common.base.constants.TenantConstant;
|
import com.qiaoba.common.base.constants.TenantConstant;
|
||||||
import com.qiaoba.common.base.context.BaseContext;
|
import com.qiaoba.common.base.context.BaseContext;
|
||||||
import com.qiaoba.common.base.entity.BasePage;
|
import com.qiaoba.common.base.entity.BasePage;
|
||||||
import com.qiaoba.common.redis.service.RedisService;
|
import com.qiaoba.common.redis.service.RedisService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@ -23,6 +25,7 @@ import java.util.stream.Collectors;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
public class RedisServiceImpl implements RedisService {
|
public class RedisServiceImpl implements RedisService {
|
||||||
|
|
||||||
private final RedisTemplate<String, Object> redisTemplate;
|
private final RedisTemplate<String, Object> redisTemplate;
|
||||||
@ -255,6 +258,7 @@ public class RedisServiceImpl implements RedisService {
|
|||||||
public String addTenantPrefix(String key) {
|
public String addTenantPrefix(String key) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append(TenantConstant.TENANT_KEY_PREFIX).append(BaseContext.getTenantId()).append(BaseConstant.COLON_JOIN_STR).append(key);
|
sb.append(TenantConstant.TENANT_KEY_PREFIX).append(BaseContext.getTenantId()).append(BaseConstant.COLON_JOIN_STR).append(key);
|
||||||
|
log.debug(StrUtil.format("拼接后的RedisKey: {}", sb.toString()));
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -262,6 +266,7 @@ public class RedisServiceImpl implements RedisService {
|
|||||||
public String removeTenantPrefix(String key) {
|
public String removeTenantPrefix(String key) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append(TenantConstant.TENANT_KEY_PREFIX).append(BaseContext.getTenantId()).append(BaseConstant.COLON_JOIN_STR);
|
sb.append(TenantConstant.TENANT_KEY_PREFIX).append(BaseContext.getTenantId()).append(BaseConstant.COLON_JOIN_STR);
|
||||||
|
log.debug(StrUtil.format("去除拼接后的RedisKey: {}", key.replace(sb.toString(), "")));
|
||||||
return key.replace(sb.toString(), "");
|
return key.replace(sb.toString(), "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ public enum TenantStatusEnum {
|
|||||||
/**
|
/**
|
||||||
* 禁用
|
* 禁用
|
||||||
*/
|
*/
|
||||||
DISABLE("1", "禁用"),
|
DISABLE("0", "禁用"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 过期
|
* 过期
|
||||||
|
@ -3,6 +3,7 @@ package com.qiaoba.module.tenant.filters;
|
|||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.qiaoba.auth.properties.AuthConfigProperties;
|
import com.qiaoba.auth.properties.AuthConfigProperties;
|
||||||
|
import com.qiaoba.common.base.code.TenantErrorCode;
|
||||||
import com.qiaoba.common.base.constants.TenantConstant;
|
import com.qiaoba.common.base.constants.TenantConstant;
|
||||||
import com.qiaoba.common.base.context.BaseContext;
|
import com.qiaoba.common.base.context.BaseContext;
|
||||||
import com.qiaoba.common.database.config.DynamicDataSourceConfig;
|
import com.qiaoba.common.database.config.DynamicDataSourceConfig;
|
||||||
@ -45,26 +46,20 @@ public class DynamicDataSourceFilter extends OncePerRequestFilter {
|
|||||||
@Resource
|
@Resource
|
||||||
private AuthConfigProperties authConfigProperties;
|
private AuthConfigProperties authConfigProperties;
|
||||||
|
|
||||||
|
private final String LOGIN_TENANT_LIST_URI = "/tenant/normal-list";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||||
|
log.debug(StrUtil.format("Start run DynamicDataSourceFilter, Uri: {}", request.getRequestURI()));
|
||||||
String tenantId = request.getHeader(TenantConstant.HEADER_KEY_TENANT);
|
String tenantId = request.getHeader(TenantConstant.HEADER_KEY_TENANT);
|
||||||
// 主系统
|
// 主系统 or 登录入口获取租户列表
|
||||||
if (TenantConstant.DEFAULT_TENANT_ID.equals(tenantId)) {
|
if (TenantConstant.DEFAULT_TENANT_ID.equals(tenantId) || LOGIN_TENANT_LIST_URI.equals(request.getRequestURI())) {
|
||||||
dynamicDataSourceConfig.setDefaultSetting();
|
dynamicDataSourceConfig.setDefaultSetting();
|
||||||
filterChain.doFilter(request, response);
|
filterChain.doFilter(request, response);
|
||||||
after();
|
after();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 白名单
|
|
||||||
for (String uri : authConfigProperties.getWhitelist()) {
|
|
||||||
if (UriUtil.match(uri, request.getRequestURI())) {
|
|
||||||
dynamicDataSourceConfig.setDefaultSetting();
|
|
||||||
filterChain.doFilter(request, response);
|
|
||||||
after();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SysTenant sysTenant = sysTenantService.selectById(tenantId);
|
SysTenant sysTenant = sysTenantService.selectById(tenantId);
|
||||||
// 检查租户是否允许访问
|
// 检查租户是否允许访问
|
||||||
@ -108,26 +103,32 @@ public class DynamicDataSourceFilter extends OncePerRequestFilter {
|
|||||||
private boolean checkTenantIsNotAllow(HttpServletResponse response, SysTenant sysTenant) throws IOException {
|
private boolean checkTenantIsNotAllow(HttpServletResponse response, SysTenant sysTenant) throws IOException {
|
||||||
if (Objects.isNull(sysTenant)) {
|
if (Objects.isNull(sysTenant)) {
|
||||||
// 未找到租户信息
|
// 未找到租户信息
|
||||||
ResponseUtil.errorAuth(response, 401, "未找到租户信息");
|
log.debug("未找到租户信息");
|
||||||
|
ResponseUtil.errorAuth(response, TenantErrorCode.NOT_FIND.getCode(), TenantErrorCode.NOT_FIND.getMsg());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TenantStatusEnum.DISABLE.getStatus().equals(sysTenant.getStatus())) {
|
if (TenantStatusEnum.DISABLE.getStatus().equals(sysTenant.getStatus())) {
|
||||||
// 封禁状态
|
// 封禁状态
|
||||||
ResponseUtil.errorAuth(response, 401, "租户已被封禁");
|
log.debug(StrUtil.format("租户已封禁, 租户ID: {}", sysTenant.getTenantId()));
|
||||||
|
ResponseUtil.errorAuth(response, TenantErrorCode.DISABLE.getCode(), TenantErrorCode.DISABLE.getMsg());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TenantStatusEnum.EXPIRE.getStatus().equals(sysTenant.getStatus())) {
|
if (TenantStatusEnum.EXPIRE.getStatus().equals(sysTenant.getStatus())) {
|
||||||
// 已过期
|
// 已过期
|
||||||
ResponseUtil.errorAuth(response, 401, "租户已过期");
|
log.debug(StrUtil.format("租户已过期, 租户ID: {}", sysTenant.getTenantId()));
|
||||||
|
ResponseUtil.errorAuth(response, TenantErrorCode.EXPIRE.getCode(), TenantErrorCode.EXPIRE.getMsg());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (DateUtil.compare(sysTenant.getExpireTime(), new Date()) < 0) {
|
if (DateUtil.compare(sysTenant.getExpireTime(), new Date()) < 0) {
|
||||||
// 已过期
|
// 已过期
|
||||||
ResponseUtil.errorAuth(response, 401, "租户已过期");
|
log.debug(StrUtil.format("租户已过期, 租户ID: {}", sysTenant.getTenantId()));
|
||||||
|
ResponseUtil.errorAuth(response, TenantErrorCode.EXPIRE.getCode(), TenantErrorCode.EXPIRE.getMsg());
|
||||||
// 更新租户状态为已过期
|
// 更新租户状态为已过期
|
||||||
sysTenantService.update(new SysTenant(sysTenant.getTenantId(), TenantStatusEnum.EXPIRE.getStatus()));
|
dynamicDataSourceConfig.setDefaultSetting();
|
||||||
|
sysTenantService.updateStatus(sysTenant.getTenantId(), TenantStatusEnum.EXPIRE.getStatus());
|
||||||
|
after();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -65,4 +65,12 @@ public interface SysTenantService {
|
|||||||
* @throws Exception Exception
|
* @throws Exception Exception
|
||||||
*/
|
*/
|
||||||
void initData(String tenantId) throws Exception;
|
void initData(String tenantId) throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更改状态
|
||||||
|
*
|
||||||
|
* @param tenantId tenantId
|
||||||
|
* @param status status
|
||||||
|
*/
|
||||||
|
void updateStatus(String tenantId, String status);
|
||||||
}
|
}
|
||||||
|
@ -115,6 +115,11 @@ public class SysTenantServiceImpl implements SysTenantService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateStatus(String tenantId, String status) {
|
||||||
|
sysTenantMapper.updateById(new SysTenant(tenantId, status));
|
||||||
|
}
|
||||||
|
|
||||||
private QueryWrapper<SysTenant> param2Wrapper(SysTenantParam param) {
|
private QueryWrapper<SysTenant> param2Wrapper(SysTenantParam param) {
|
||||||
QueryWrapper<SysTenant> wrapper = new QueryWrapper<>();
|
QueryWrapper<SysTenant> wrapper = new QueryWrapper<>();
|
||||||
wrapper.lambda()
|
wrapper.lambda()
|
||||||
|
Reference in New Issue
Block a user