多租户模式
This commit is contained in:
parent
78c12e1fd8
commit
70834b64d1
@ -0,0 +1,21 @@
|
|||||||
|
package com.cdzy.staff.component;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
|
import com.mybatisflex.core.tenant.TenantFactory;
|
||||||
|
import org.springframework.web.context.request.RequestAttributes;
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
|
||||||
|
public class EbikeTenantFactory implements TenantFactory {
|
||||||
|
|
||||||
|
public Object[] getTenantIds() {
|
||||||
|
if (StpUtil.isLogin()) {
|
||||||
|
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
|
||||||
|
Object attribute = attributes.getAttribute("tenantId", RequestAttributes.SCOPE_REQUEST);
|
||||||
|
if (attribute != null) {
|
||||||
|
Long tenantId = Long.valueOf(attribute.toString());
|
||||||
|
return new Object[]{tenantId};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
package com.cdzy.staff.component;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
|
import com.cdzy.staff.model.dto.StaffInfo;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
|
|
||||||
|
public class TenantInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(@NotNull HttpServletRequest request
|
||||||
|
, @NotNull HttpServletResponse response, @NotNull Object handler) throws Exception {
|
||||||
|
|
||||||
|
//通过 request 去获取租户 ID
|
||||||
|
if (StpUtil.isLogin()) {
|
||||||
|
Long tenantId = getTenantIdByReuqest(request);
|
||||||
|
//设置租户ID到 request 的 attribute
|
||||||
|
request.setAttribute("tenantId", tenantId);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Long getTenantIdByReuqest(HttpServletRequest request) {
|
||||||
|
String token = request.getHeader("Authorization");
|
||||||
|
String id = (String) StpUtil.getLoginIdByToken(token);
|
||||||
|
StaffInfo staffDto = (StaffInfo) StpUtil.getSessionByLoginId(id).get(id);
|
||||||
|
return staffDto.getStaff().getOperatorId();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
package com.cdzy.staff.config;
|
||||||
|
|
||||||
|
import com.cdzy.staff.component.EbikeTenantFactory;
|
||||||
|
import com.cdzy.staff.component.TenantInterceptor;
|
||||||
|
import com.mybatisflex.core.tenant.TenantFactory;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public TenantFactory tenantFactory(){
|
||||||
|
return new EbikeTenantFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
registry.addInterceptor(new TenantInterceptor());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -29,7 +29,7 @@ public class EbikeOperatorStaff implements Serializable {
|
|||||||
@Serial
|
@Serial
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Id(keyType = KeyType.Auto)
|
@Id
|
||||||
private Long staffId;
|
private Long staffId;
|
||||||
|
|
||||||
private String username;
|
private String username;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user