feat(多租户):多租户数据隔离功能实现
This commit is contained in:
@@ -47,3 +47,13 @@ tencent:
|
||||
expire-seconds: 3600 #过期时间为1小时
|
||||
app-name: live
|
||||
key: 06b20e5b7d637c1128fa1b48ea91a13e
|
||||
|
||||
zlt:
|
||||
#多租户配置
|
||||
tenant:
|
||||
enable: false
|
||||
ignoreTables:
|
||||
ignoreSqls:
|
||||
noIgnoreSqls:
|
||||
- com.central.device.mapper.AgeDeviceMapper.findList
|
||||
- com.central.device.mapper.LineFileMapper.findList
|
||||
|
||||
@@ -4,10 +4,10 @@ server:
|
||||
spring:
|
||||
application:
|
||||
name: device-center
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
ip: 119.45.169.13
|
||||
# cloud:
|
||||
# nacos:
|
||||
# discovery:
|
||||
# ip: 119.45.169.13
|
||||
# cloud:
|
||||
# nacos:
|
||||
# discovery:
|
||||
|
||||
@@ -41,7 +41,7 @@ public class HomeCarousel extends SuperEntity {
|
||||
private Date updateTime;
|
||||
|
||||
//租户ID
|
||||
private Long tenantId;
|
||||
private String tenantId;
|
||||
private String company;
|
||||
private Integer isDel;
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public class OrderFile extends SuperEntity {
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private Date updateTime;
|
||||
//租户ID
|
||||
private Long tenantId;
|
||||
private String tenantId;
|
||||
private Integer isDel;
|
||||
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ public class QrCode extends SuperEntity {
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private Date updateTime;
|
||||
//租户ID
|
||||
private Long tenantId;
|
||||
private String tenantId;
|
||||
private String company;
|
||||
private Integer isDel;
|
||||
|
||||
|
||||
@@ -38,9 +38,9 @@ public class Tourism extends SuperEntity {
|
||||
private Date createTime;
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private Date updateTime;
|
||||
|
||||
|
||||
//租户ID
|
||||
private Long tenantId;
|
||||
private String tenantId;
|
||||
private String company;
|
||||
private Integer isDel;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class TourismFile extends SuperEntity {
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private Date updateTime;
|
||||
//租户ID
|
||||
private Long tenantId;
|
||||
private String tenantId;
|
||||
private String company;
|
||||
private Integer isDel;
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ knife4j:
|
||||
zlt:
|
||||
#多租户配置
|
||||
tenant:
|
||||
enable: false
|
||||
enable: true
|
||||
ignoreTables:
|
||||
- edit_task
|
||||
- home_carousel
|
||||
@@ -41,11 +41,15 @@ zlt:
|
||||
- order_statistics
|
||||
ignoreSqls:
|
||||
# 查询全部列表
|
||||
- com.central.order.mapper.OrderMapper.findList
|
||||
# - com.central.order.mapper.OrderMapper.findList
|
||||
# 获取某个订单
|
||||
- com.central.order.mapper.OrderMapper.getOrderByNumber
|
||||
# - com.central.order.mapper.OrderMapper.getOrderByNumber
|
||||
noIgnoreSqls:
|
||||
- com.central.order.mapper.OrderMapper.findList
|
||||
- com.central.order.mapper.TourismMapper.findList
|
||||
- com.central.order.mapper.OrderFileMapper.findList
|
||||
- com.central.order.mapper.QrCodeMapper.findList
|
||||
- com.central.order.mapper.QrCodeMapper.save
|
||||
# 微信支付商户号
|
||||
tencent:
|
||||
merchantId: ${zlt.wxpay.merchantId}
|
||||
|
||||
+1
-1
@@ -39,7 +39,7 @@ public class TenantProperties {
|
||||
);
|
||||
|
||||
/**
|
||||
* 忽略租户的SQL
|
||||
* 不可忽略租户的SQL
|
||||
*/
|
||||
private List<String> noIgnoreSqls = Arrays.asList(
|
||||
"com.central.user.mapper.SysRoleMapper.findAll",
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.central.db.interceptor;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
|
||||
import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.common.context.TenantContextHolder;
|
||||
import com.central.common.model.LoginAppUser;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.NullValue;
|
||||
import net.sf.jsqlparser.expression.StringValue;
|
||||
|
||||
/**
|
||||
* 超级管理员可以管理租户数据权限开放
|
||||
* @date 2025-05-15
|
||||
* @author lsl
|
||||
*/
|
||||
public class CustomTenantLineHandler implements TenantLineHandler {
|
||||
@Override
|
||||
public Expression getTenantId() {
|
||||
// 判断当前用户是否为超级管理员
|
||||
if (isSuperAdmin()) {
|
||||
// 超级管理员返回Null,不加租户条件
|
||||
return new NullValue();
|
||||
}
|
||||
// 普通用户返回租户ID
|
||||
return new StringValue(TenantContextHolder.getTenant());
|
||||
}
|
||||
|
||||
private boolean isSuperAdmin() {
|
||||
// 你可以通过ThreadLocal、SecurityContext、LoginUserContextHolder等获取当前用户角色
|
||||
// 例如:
|
||||
LoginAppUser user = LoginUserContextHolder.getUser();
|
||||
return user != null && user.getUsername().equals("admin");
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ const Footer: React.FC = () => {
|
||||
|
||||
return (
|
||||
<DefaultFooter
|
||||
copyright={`${2018} ${defaultMessage}`}
|
||||
copyright={`${2025} ${defaultMessage}`}
|
||||
links={[
|
||||
{
|
||||
key: 'github',
|
||||
|
||||
@@ -11,7 +11,7 @@ export default {
|
||||
'layout.user.link.help': '帮助',
|
||||
'layout.user.link.privacy': '隐私',
|
||||
'layout.user.link.terms': '条款',
|
||||
'app.copyright.produced': 'zlt版权所有:https://gitee.com/zlt2000/microservices-platform',
|
||||
'app.copyright.produced': '版权所有:https://gitee.com/L-Shilin/go-fly-platform-services.git',
|
||||
'app.preview.down.block': '下载此页面到本地项目',
|
||||
'app.welcome.link.fetch-blocks': '获取全部区块',
|
||||
'app.welcome.link.block-list': '基于 block 开发,快速构建标准页面',
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import Footer from '@/components/Footer';
|
||||
import { login } from '@/services/login/api';
|
||||
import { InfoCircleOutlined, LockOutlined, UserOutlined } from '@ant-design/icons';
|
||||
import { InfoCircleOutlined, LockOutlined, UserOutlined, AppstoreOutlined } from '@ant-design/icons';
|
||||
import { LoginForm, ProFormCaptcha, ProFormText, ProFormCheckbox } from '@ant-design/pro-components';
|
||||
import { message, Tooltip, Spin } from 'antd';
|
||||
import React, { useState } from 'react';
|
||||
import { message, Tooltip, Spin, Form } from 'antd';
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { FormattedMessage, history, useIntl, useModel } from 'umi';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import styles from './index.less';
|
||||
@@ -13,6 +13,7 @@ const Login: React.FC = () => {
|
||||
const [loading, setLoading] = useState<boolean>(false);
|
||||
const { initialState, setInitialState } = useModel('@@initialState');
|
||||
const intl = useIntl();
|
||||
const [form] = Form.useForm();
|
||||
|
||||
const fetchUserInfo = async () => {
|
||||
try {
|
||||
@@ -28,11 +29,32 @@ const Login: React.FC = () => {
|
||||
}
|
||||
};
|
||||
|
||||
// 缓存租户标识
|
||||
const cacheTenantId = (clientId: string) => {
|
||||
localStorage.setItem('tenant_client_id', clientId);
|
||||
};
|
||||
|
||||
// 获取缓存的租户标识
|
||||
const getCachedTenantId = () => {
|
||||
return localStorage.getItem('tenant_client_id');
|
||||
};
|
||||
|
||||
// 自动填充缓存的租户标识
|
||||
useEffect(() => {
|
||||
const clientId = getCachedTenantId();
|
||||
if (clientId) {
|
||||
form.setFieldsValue({
|
||||
clientId,
|
||||
});
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleSubmit = async (values: API.LoginParams) => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const msg = await login({ ...values, deviceId: uuid });
|
||||
|
||||
|
||||
debugger;
|
||||
if (msg.resp_code === 0 && msg.data?.access_token) {
|
||||
sessionStorage.setItem('access_token', msg.data.access_token);
|
||||
message.success('登录成功!');
|
||||
@@ -62,6 +84,7 @@ const Login: React.FC = () => {
|
||||
title="microservices-platform"
|
||||
subTitle={intl.formatMessage({ id: 'pages.layouts.userLayout.title' })}
|
||||
initialValues={{ autoLogin: true }}
|
||||
form={form}
|
||||
onFinish={async (values) => {
|
||||
if (!loading) {
|
||||
await handleSubmit(values as API.LoginParams);
|
||||
@@ -69,16 +92,30 @@ const Login: React.FC = () => {
|
||||
}}
|
||||
>
|
||||
<ProFormText
|
||||
name="username"
|
||||
name="clientId"
|
||||
fieldProps={{
|
||||
size: 'large',
|
||||
prefix: <UserOutlined className={styles.prefixIcon} />,
|
||||
prefix: <AppstoreOutlined />,
|
||||
}}
|
||||
placeholder="请输入账号"
|
||||
placeholder="请输入租户标识"
|
||||
rules={[
|
||||
{
|
||||
required: true,
|
||||
message: '请输入账号!',
|
||||
message: '请输入租户标识!',
|
||||
},
|
||||
]}
|
||||
/>
|
||||
<ProFormText
|
||||
name="username"
|
||||
fieldProps={{
|
||||
size: 'large',
|
||||
prefix: <UserOutlined />,
|
||||
}}
|
||||
placeholder="请输入用户名"
|
||||
rules={[
|
||||
{
|
||||
required: true,
|
||||
message: '请输入用户名!',
|
||||
},
|
||||
]}
|
||||
/>
|
||||
@@ -86,7 +123,7 @@ const Login: React.FC = () => {
|
||||
name="password"
|
||||
fieldProps={{
|
||||
size: 'large',
|
||||
prefix: <LockOutlined className={styles.prefixIcon} />,
|
||||
prefix: <LockOutlined />,
|
||||
}}
|
||||
placeholder="请输入密码"
|
||||
rules={[
|
||||
|
||||
@@ -33,15 +33,36 @@ export async function outLogin(options?: { [key: string]: any }) {
|
||||
});
|
||||
}
|
||||
|
||||
// 修改生成 basicToken 的工具函数,只使用 clientId
|
||||
const generateBasicToken = (clientId: string) => {
|
||||
// 确保使用 UTF-8 编码
|
||||
const token = `${clientId}:${clientId}`;
|
||||
// 使用 encodeURIComponent 和 decodeURIComponent 处理中文字符
|
||||
const encodedToken = btoa(unescape(encodeURIComponent(token)));
|
||||
return `Basic ${encodedToken}`;
|
||||
};
|
||||
|
||||
export async function login(body: API.LoginParams, options?: { [key: string]: any }) {
|
||||
|
||||
const clientId = body.clientId;
|
||||
const basicToken = generateBasicToken(clientId);
|
||||
body.clientId = undefined;
|
||||
debugger;
|
||||
|
||||
return request<API.OptionalResult<API.LoginResult>>('/api-uaa/oauth/token', {
|
||||
method: 'POST',
|
||||
requestType: 'form',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
Authorization: AUTH_CONFIG.basicToken,
|
||||
Authorization: basicToken,
|
||||
},
|
||||
data: {
|
||||
username: body.username,
|
||||
password: body.password,
|
||||
validCode: body.validCode,
|
||||
deviceId: body.deviceId,
|
||||
grant_type: 'password_code',
|
||||
},
|
||||
data: { ...body, grant_type: 'password_code' },
|
||||
...(options || {}),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ declare namespace API {
|
||||
password?: string;
|
||||
validCode?: string;
|
||||
deviceId?: string;
|
||||
clientId?: string;
|
||||
};
|
||||
|
||||
type ErrorResponse = {
|
||||
|
||||
Reference in New Issue
Block a user