diff --git a/README.md b/README.md index 02c8f3dc9d1f7fbdbb45b325d16ad848acb3952d..359170e67ec1e6cc69e6982e325727b4fcf5dcac 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,8 @@ * 基于`Spring Boot 2.0.X`、`Spring Cloud Finchley`和`Spring Cloud Alibaba` * 主要针对解决微服务和业务开发时常见的**非功能性需求** * 深度定制`Spring Security`真正实现了基于`RBAC`、`jwt`和`oauth2`的无状态统一权限认证的解决方案 -* 提供应用管理,方便第三方系统接入 -* 引入组件化的思想实现高内聚低耦合,项目代码简洁注释丰富上手容易 +* 提供应用管理,方便第三方系统接入,支持多租户 +* 引入组件化的思想实现高内聚低耦合并且高度可配置化 * 注重代码规范,严格控制包依赖,每个工程基本都是最小依赖 * 非常适合学习和企业中使用 @@ -78,6 +78,7 @@ - nacos监控 - prometheus监控 * **业务基础功能支撑** + * 多租户(应用隔离) * 高性能方法级幂等性支持 * RBAC权限管理,实现细粒度控制(方法、url级别) * 快速实现导入、导出功能 diff --git a/zlt-business/file-center/src/main/java/com/central/FileCenterApp.java b/zlt-business/file-center/src/main/java/com/central/FileCenterApp.java index 77b6c20aa68065b6543c752fa4b9f1607c924f21..487e94e50caa8e7b022085171ed5ff4b56374ade 100644 --- a/zlt-business/file-center/src/main/java/com/central/FileCenterApp.java +++ b/zlt-business/file-center/src/main/java/com/central/FileCenterApp.java @@ -6,6 +6,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; /** * 文件中心 @@ -13,6 +14,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient; */ @EnableDiscoveryClient @EnableConfigurationProperties(FileServerProperties.class) +@EnableFeignClients @EnableFeignInterceptor @SpringBootApplication public class FileCenterApp { diff --git a/zlt-business/file-center/src/main/java/com/central/file/config/WebMvcConfig.java b/zlt-business/file-center/src/main/java/com/central/file/config/WebMvcConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..c4dc350f78da9b3e5e0fdff9bc22a65a96f1e956 --- /dev/null +++ b/zlt-business/file-center/src/main/java/com/central/file/config/WebMvcConfig.java @@ -0,0 +1,12 @@ +package com.central.file.config; + +import com.central.common.config.DefaultWebMvcConfig; +import org.springframework.context.annotation.Configuration; + +/** + * @author zlt + * @date 2019/8/5 + */ +@Configuration +public class WebMvcConfig extends DefaultWebMvcConfig { +} diff --git a/zlt-business/file-center/src/main/resources/application.yml b/zlt-business/file-center/src/main/resources/application.yml index fd6097116b0648ea4ebd06bde4255ec484c80923..fe69f3e90d646ceba694088633a69202dcfc678a 100644 --- a/zlt-business/file-center/src/main/resources/application.yml +++ b/zlt-business/file-center/src/main/resources/application.yml @@ -31,6 +31,9 @@ zlt: description: 文件中心接口文档 version: 1.0 base-package: com.central.file.controller + #多租户配置 + tenant: + enable: true #fastDFS配置 fdfs: diff --git a/zlt-business/search-center/search-server/src/main/java/com/central/SearchCenterApp.java b/zlt-business/search-center/search-server/src/main/java/com/central/SearchCenterApp.java index cb59ea14473532da72b3533fff27a1a3a6457d3c..b4d53f4fdd882141ce14426b745d794e566e82e4 100644 --- a/zlt-business/search-center/search-server/src/main/java/com/central/SearchCenterApp.java +++ b/zlt-business/search-center/search-server/src/main/java/com/central/SearchCenterApp.java @@ -1,7 +1,6 @@ package com.central; import com.central.admin.properties.IndexProperties; -import com.central.common.annotation.EnableLoginArgResolver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -11,7 +10,6 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient; * @author zlt * @date 2019/5/1 */ -@EnableLoginArgResolver @EnableDiscoveryClient @SpringBootApplication @EnableConfigurationProperties(IndexProperties.class) diff --git a/zlt-business/search-center/search-server/src/main/java/com/central/common/config/WebMvcConfig.java b/zlt-business/search-center/search-server/src/main/java/com/central/common/config/WebMvcConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..b7b56bf325a8d254b0c57c30a1d5c5db311062b2 --- /dev/null +++ b/zlt-business/search-center/search-server/src/main/java/com/central/common/config/WebMvcConfig.java @@ -0,0 +1,11 @@ +package com.central.common.config; + +import org.springframework.context.annotation.Configuration; + +/** + * @author zlt + * @date 2019/8/5 + */ +@Configuration +public class WebMvcConfig extends DefaultWebMvcConfig { +} diff --git a/zlt-business/user-center/src/main/java/com/central/UserCenterApp.java b/zlt-business/user-center/src/main/java/com/central/UserCenterApp.java index c54b21c65044bef47575c5b6be65f9a5dbe8c40a..4ba97bcd446549a1cb3bf97555890ba2c3bd00f9 100644 --- a/zlt-business/user-center/src/main/java/com/central/UserCenterApp.java +++ b/zlt-business/user-center/src/main/java/com/central/UserCenterApp.java @@ -1,6 +1,5 @@ package com.central; -import com.central.common.annotation.EnableLoginArgResolver; import com.central.common.ribbon.annotation.EnableFeignInterceptor; import com.central.search.annotation.EnableSearchClient; import org.springframework.boot.SpringApplication; @@ -11,7 +10,6 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; /** * @author 作者 owen E-mail: 624191343@qq.com */ -@EnableLoginArgResolver @EnableDiscoveryClient @EnableSearchClient @EnableTransactionManagement diff --git a/zlt-business/user-center/src/main/java/com/central/user/config/WebMvcConfig.java b/zlt-business/user-center/src/main/java/com/central/user/config/WebMvcConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..4db93cde8c6e50bd5725b0201e95eb2ff8d04da6 --- /dev/null +++ b/zlt-business/user-center/src/main/java/com/central/user/config/WebMvcConfig.java @@ -0,0 +1,12 @@ +package com.central.user.config; + +import com.central.common.config.DefaultWebMvcConfig; +import org.springframework.context.annotation.Configuration; + +/** + * @author zlt + * @date 2019/8/5 + */ +@Configuration +public class WebMvcConfig extends DefaultWebMvcConfig { +} diff --git a/zlt-business/user-center/src/main/java/com/central/user/mapper/SysUserRoleMapper.java b/zlt-business/user-center/src/main/java/com/central/user/mapper/SysUserRoleMapper.java index 2565312be54f7e749c8ee891d36ff145206c6ed9..0de183f41eee8298c3ff24ed9a5c0b87351e40bb 100644 --- a/zlt-business/user-center/src/main/java/com/central/user/mapper/SysUserRoleMapper.java +++ b/zlt-business/user-center/src/main/java/com/central/user/mapper/SysUserRoleMapper.java @@ -28,8 +28,7 @@ public interface SysUserRoleMapper extends SuperMapper { * @param userId * @return */ - @Select("select r.* from sys_role_user ru inner join sys_role r on r.id = ru.role_id where ru.user_id = #{userId}") - List findRolesByUserId(Long userId); + List findRolesByUserId(@Param("userId") Long userId); /** * 根据用户ids 获取 diff --git a/zlt-business/user-center/src/main/resources/application.yml b/zlt-business/user-center/src/main/resources/application.yml index 62d6c243b1fe890433f9798dc90e12aae4e18ca2..8ee59ae148dd297fafd92e8ab2c041f2ee639989 100644 --- a/zlt-business/user-center/src/main/resources/application.yml +++ b/zlt-business/user-center/src/main/resources/application.yml @@ -26,3 +26,10 @@ zlt: second: 300 - key: user second: 1800 + #多租户配置 + tenant: + enable: true + ignoreTables: + - sys_user + - sys_role_user + - sys_role_menu \ No newline at end of file diff --git a/zlt-business/user-center/src/main/resources/mapper/SysRoleMenuMapper.xml b/zlt-business/user-center/src/main/resources/mapper/SysRoleMenuMapper.xml index 9db38b8e3262c61946fe0bdb868ce7c2b11e9ee5..49d0e9788be0aa8cecf6eed8d40a22a59bb999a0 100644 --- a/zlt-business/user-center/src/main/resources/mapper/SysRoleMenuMapper.xml +++ b/zlt-business/user-center/src/main/resources/mapper/SysRoleMenuMapper.xml @@ -43,6 +43,7 @@ and t.type = #{type} + and t.hidden = 0 ORDER BY sort ASC \ No newline at end of file diff --git a/zlt-business/user-center/src/main/resources/mapper/SysUserRoleMapper.xml b/zlt-business/user-center/src/main/resources/mapper/SysUserRoleMapper.xml index df2e7cfce56d93d135a2403d78f0faa94ea8a273..b6010d5f6a512bf51017c954a4422186f2387e32 100644 --- a/zlt-business/user-center/src/main/resources/mapper/SysUserRoleMapper.xml +++ b/zlt-business/user-center/src/main/resources/mapper/SysUserRoleMapper.xml @@ -13,4 +13,9 @@ + + \ No newline at end of file diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/annotation/EnableLoginArgResolver.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/annotation/EnableLoginArgResolver.java deleted file mode 100644 index 92d974862fd77b6491b02e2aa748f86f2b3587a1..0000000000000000000000000000000000000000 --- a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/annotation/EnableLoginArgResolver.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.central.common.annotation; - -import com.central.common.config.LoginArgResolverConfig; -import org.springframework.context.annotation.Import; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * 在启动类上添加该注解来----开启自动登录用户对象注入 - * Token转化SysUser - * - * @author zlt - */ -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -@Import(LoginArgResolverConfig.class) -public @interface EnableLoginArgResolver { -} diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/config/DefaultWebMvcConfig.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/config/DefaultWebMvcConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..de5e5442eef0a3863af4ff8e825f1d9a48dd1266 --- /dev/null +++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/config/DefaultWebMvcConfig.java @@ -0,0 +1,45 @@ +package com.central.common.config; + +import com.central.common.feign.UserService; +import com.central.common.interceptor.TenantInterceptor; +import com.central.common.resolver.ClientArgumentResolver; +import com.central.common.resolver.TokenArgumentResolver; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; + +import java.util.List; + +/** + * 默认SpringMVC拦截器 + * + * @author zlt + * @date 2019/8/5 + */ +public class DefaultWebMvcConfig extends WebMvcConfigurationSupport { + @Autowired + private UserService userService; + + /** + * 配置SpringMVC拦截器,添加租户拦截器 + */ + @Override + protected void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new TenantInterceptor()).addPathPatterns("/**"); + super.addInterceptors(registry); + } + + /** + * Token参数解析 + * + * @param argumentResolvers 解析类 + */ + @Override + public void addArgumentResolvers(List argumentResolvers) { + //注入用户信息 + argumentResolvers.add(new TokenArgumentResolver(userService)); + //注入应用信息 + argumentResolvers.add(new ClientArgumentResolver()); + } +} diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/CommonConstant.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/CommonConstant.java index b3ed8d0e15f272f42d1323c55ed4173ff5479469..39ae330bd9b2acd00583b19debe456945df77289 100644 --- a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/CommonConstant.java +++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/CommonConstant.java @@ -97,4 +97,9 @@ public interface CommonConstant { String DEF_USER_PASSWORD = "123456"; String LOCK_KEY_PREFIX = "LOCK_KEY:"; + + /** + * 租户id参数 + */ + String TENANT_ID_PARAM = "tenantId"; } diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/SecurityConstants.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/SecurityConstants.java index 0072234f00749ac59ad5f72ca5f3237841f939a4..16659d5032d2ce69468c60ec2b940cda8e99ef3c 100644 --- a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/SecurityConstants.java +++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/SecurityConstants.java @@ -27,8 +27,9 @@ public interface SecurityConstants { String ROLE_HEADER = "x-role-header"; /** - * 应用信息头 + * 租户信息头(应用) */ + String TENANT_HEADER = "x-tenant-header"; String CLIENT_HEADER = "x-client-header"; /** diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/interceptor/TenantInterceptor.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/interceptor/TenantInterceptor.java new file mode 100644 index 0000000000000000000000000000000000000000..50529fea5446a7caaa9305f0623f2ad963ce41f5 --- /dev/null +++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/interceptor/TenantInterceptor.java @@ -0,0 +1,32 @@ +package com.central.common.interceptor; + +import cn.hutool.core.util.StrUtil; +import com.central.common.constant.CommonConstant; +import com.central.common.constant.SecurityConstants; +import com.central.common.utils.TenantContextHolder; +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * 租户拦截器 + * + * @author zlt + * @date 2019/8/5 + */ +public class TenantInterceptor implements HandlerInterceptor { + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { + //优先获取请求参数中的tenantId值 + String tenantId = request.getParameter(CommonConstant.TENANT_ID_PARAM); + if (StrUtil.isEmpty(tenantId)) { + tenantId = request.getHeader(SecurityConstants.TENANT_HEADER); + } + //保存租户id + if(StrUtil.isNotEmpty(tenantId)){ + TenantContextHolder.setTenant(tenantId); + } + return true; + } +} diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/properties/TenantProperties.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/properties/TenantProperties.java new file mode 100644 index 0000000000000000000000000000000000000000..e749a1f33baa584d324e7a81138722839413d34d --- /dev/null +++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/properties/TenantProperties.java @@ -0,0 +1,36 @@ +package com.central.common.properties; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.context.config.annotation.RefreshScope; + +import java.util.ArrayList; +import java.util.List; + +/** + * 多租户配置 + * @author zlt + * @date 2019/8/5 + */ +@Setter +@Getter +@ConfigurationProperties(prefix = "zlt.tenant") +@RefreshScope +public class TenantProperties { + /** + * 是否开启多租户 + */ + private Boolean enable = false; + + /** + * 配置不进行多租户隔离的表名 + */ + private List ignoreTables = new ArrayList<>(); + + /** + * 配置不进行多租户隔离的sql + * 需要配置mapper的全路径如:com.central.user.mapper.SysUserMapper.findList + */ + private List ignoreSqls = new ArrayList<>(); +} diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/resolver/ClientArgumentResolver.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/resolver/ClientArgumentResolver.java index aeb21b0af10ad7ac642b8c41b5265556df69abf1..4278a0580bb78cde7747cab674f150ee7147ccf3 100644 --- a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/resolver/ClientArgumentResolver.java +++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/resolver/ClientArgumentResolver.java @@ -44,7 +44,7 @@ public class ClientArgumentResolver implements HandlerMethodArgumentResolver { NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) { HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class); - String clientId = request.getHeader(SecurityConstants.CLIENT_HEADER); + String clientId = request.getHeader(SecurityConstants.TENANT_HEADER); if (StrUtil.isBlank(clientId)) { log.warn("resolveArgument error clientId is empty"); } diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/utils/TenantContextHolder.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/utils/TenantContextHolder.java new file mode 100644 index 0000000000000000000000000000000000000000..b21ee2422b1a5123be3f02626130cba9635ec7ad --- /dev/null +++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/utils/TenantContextHolder.java @@ -0,0 +1,23 @@ +package com.central.common.utils; + +/** + * 租户holder + * + * @author zlt + * @date 2019/8/5 + */ +public class TenantContextHolder { + private static final ThreadLocal CONTEXT = new ThreadLocal<>(); + + public static void setTenant(String tenant) { + CONTEXT.set(tenant); + } + + public static String getTenant() { + return CONTEXT.get(); + } + + public static void clear() { + CONTEXT.remove(); + } +} diff --git a/zlt-commons/zlt-db-spring-boot-starter/pom.xml b/zlt-commons/zlt-db-spring-boot-starter/pom.xml index dbdcc9aa2c9c9fb17132029861c1f15dabec8558..ba59c8b6e1ee95878a787afc7108c16c29087457 100644 --- a/zlt-commons/zlt-db-spring-boot-starter/pom.xml +++ b/zlt-commons/zlt-db-spring-boot-starter/pom.xml @@ -12,6 +12,11 @@ zlt-db-spring-boot-starter 数据库通用组件 + + com.zlt + zlt-common-spring-boot-starter + + com.baomidou mybatis-plus-boot-starter diff --git a/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/DefaultMybatisPlusConfig.java b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/DefaultMybatisPlusConfig.java index 50f0097f66fbdc9b530450e2f45db767c37209cf..91f49190f6a855b501ee48d3246a3f969b101b7f 100644 --- a/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/DefaultMybatisPlusConfig.java +++ b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/DefaultMybatisPlusConfig.java @@ -1,7 +1,14 @@ package com.central.db.config; +import cn.hutool.core.collection.CollUtil; +import com.baomidou.mybatisplus.core.parser.ISqlParserFilter; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor; +import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler; +import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser; + +import com.central.common.properties.TenantProperties; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Profile; @@ -13,12 +20,30 @@ import org.springframework.context.annotation.Profile; */ @Import(DateMetaObjectHandler.class) public class DefaultMybatisPlusConfig { + @Autowired + private TenantHandler tenantHandler; + + @Autowired + private ISqlParserFilter sqlParserFilter; + + @Autowired + private TenantProperties tenantProperties; + /** * 分页插件,自动识别数据库类型 */ @Bean public PaginationInterceptor paginationInterceptor() { - return new PaginationInterceptor(); + PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); + boolean enableTenant = tenantProperties.getEnable(); + //是否开启多租户隔离 + if (enableTenant) { + TenantSqlParser tenantSqlParser = new TenantSqlParser() + .setTenantHandler(tenantHandler); + paginationInterceptor.setSqlParserList(CollUtil.toList(tenantSqlParser)); + paginationInterceptor.setSqlParserFilter(sqlParserFilter); + } + return paginationInterceptor; } /** diff --git a/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/TenantAutoConfigure.java b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/TenantAutoConfigure.java new file mode 100644 index 0000000000000000000000000000000000000000..28ea534dc1ad4c97e166357bb0c373bb0d872201 --- /dev/null +++ b/zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/TenantAutoConfigure.java @@ -0,0 +1,75 @@ +package com.central.db.config; + +import com.baomidou.mybatisplus.core.parser.ISqlParserFilter; +import com.baomidou.mybatisplus.core.parser.SqlParserHelper; +import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler; +import com.central.common.utils.TenantContextHolder; +import com.central.common.properties.TenantProperties; +import net.sf.jsqlparser.expression.Expression; +import net.sf.jsqlparser.expression.NullValue; +import net.sf.jsqlparser.expression.StringValue; +import org.apache.ibatis.mapping.MappedStatement; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; + +/** + * 多租户自动配置 + * + * @author zlt + * @date 2019/8/5 + */ +@EnableConfigurationProperties(TenantProperties.class) +public class TenantAutoConfigure { + @Autowired + private TenantProperties tenantProperties; + + @Bean + public TenantHandler tenantHandler() { + return new TenantHandler() { + /** + * 获取租户id + */ + @Override + public Expression getTenantId() { + String tenant = TenantContextHolder.getTenant(); + if (tenant != null) { + return new StringValue(TenantContextHolder.getTenant()); + } + return new NullValue(); + } + + /** + * 获取租户列名 + */ + @Override + public String getTenantIdColumn() { + return "tenant_id"; + } + + /** + * 过滤不需要根据租户隔离的表 + * @param tableName 表名 + */ + @Override + public boolean doTableFilter(String tableName) { + return tenantProperties.getIgnoreTables().stream().anyMatch( + (e) -> e.equalsIgnoreCase(tableName) + ); + } + }; + } + + /** + * 过滤不需要根据租户隔离的MappedStatement + */ + @Bean + public ISqlParserFilter sqlParserFilter() { + return metaObject -> { + MappedStatement ms = SqlParserHelper.getMappedStatement(metaObject); + return tenantProperties.getIgnoreSqls().stream().anyMatch( + (e) -> e.equalsIgnoreCase(ms.getId()) + ); + }; + } +} diff --git a/zlt-commons/zlt-db-spring-boot-starter/src/main/resources/META-INF/spring.factories b/zlt-commons/zlt-db-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000000000000000000000000000000000000..c928b6215051ce94447f8f9a69fcdeb862f0419a --- /dev/null +++ b/zlt-commons/zlt-db-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +com.central.db.config.TenantAutoConfigure diff --git a/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java index 9f6b9342bc1dfa55575598ad4a36219373dfb626..da49e1e8a6a6a490bae85063092e3f451fb48e5b 100644 --- a/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java +++ b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java @@ -3,6 +3,7 @@ package com.central.common.ribbon.config; import cn.hutool.core.util.StrUtil; import com.central.common.constant.CommonConstant; import com.central.common.constant.SecurityConstants; +import com.central.common.utils.TenantContextHolder; import feign.RequestInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.web.context.request.RequestContextHolder; @@ -28,14 +29,16 @@ public class FeignInterceptorConfig { .getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); - //传递access_token,无网关隔离时需要传递 - /*String token = extractHeaderToken(request); + //传递access_token,无网络隔离时需要传递 + /* + String token = extractHeaderToken(request); if (StrUtil.isEmpty(token)) { token = request.getParameter(CommonConstant.ACCESS_TOKEN); } if (StrUtil.isNotEmpty(token)) { template.header(CommonConstant.TOKEN_HEADER, CommonConstant.BEARER_TYPE + " " + token); - }*/ + } + */ //传递username String username = request.getHeader(SecurityConstants.USER_HEADER); @@ -50,9 +53,9 @@ public class FeignInterceptorConfig { } //传递client - String client = request.getHeader(SecurityConstants.CLIENT_HEADER); - if (StrUtil.isNotEmpty(client)) { - template.header(SecurityConstants.CLIENT_HEADER, client); + String tenant = TenantContextHolder.getTenant(); + if (StrUtil.isNotEmpty(tenant)) { + template.header(SecurityConstants.TENANT_HEADER, tenant); } }; return requestInterceptor; diff --git a/zlt-doc/sql/file-center.sql b/zlt-doc/sql/file-center.sql index 687296f814c542a67f6df563a932180f1f5abaeb..f1b39d35ddad5fc38b60e495b84cd75bed34432b 100644 --- a/zlt-doc/sql/file-center.sql +++ b/zlt-doc/sql/file-center.sql @@ -16,11 +16,13 @@ CREATE TABLE `file_info` ( `source` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, - PRIMARY KEY (`id`) USING BTREE, - INDEX `createTime`(`create_time`) USING BTREE + `tenant_id` varchar(32) DEFAULT '' COMMENT '租户字段', + PRIMARY KEY (`id`), + KEY `idx_create_time` (`create_time`), + KEY `idx_tenant_id` (`tenant_id`) ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of file_info -- ---------------------------- -INSERT INTO `file_info` VALUES ('2c95b54f4d8356cf8ab40802f496df83', '头像.png', 1, 'image/png', 1290, 'http://pkqtmn0p1.bkt.clouddn.com/头像.png', 'http://pkqtmn0p1.bkt.clouddn.com/头像.png', 'QINIU', '2019-01-08 17:05:36', '2019-01-08 17:05:36'); +INSERT INTO `file_info` VALUES ('2c95b54f4d8356cf8ab40802f496df83', '头像.png', 1, 'image/png', 1290, 'http://pkqtmn0p1.bkt.clouddn.com/头像.png', 'http://pkqtmn0p1.bkt.clouddn.com/头像.png', 'QINIU', '2019-01-08 17:05:36', '2019-01-08 17:05:36', 'webApp'); diff --git a/zlt-doc/sql/oauth-center.sql b/zlt-doc/sql/oauth-center.sql index 3d8bdcfbce27fc599ef8ebdf0a12772286ec4f02..aad6b7861c25de41592183af52fea89e598a7862 100644 --- a/zlt-doc/sql/oauth-center.sql +++ b/zlt-doc/sql/oauth-center.sql @@ -28,6 +28,6 @@ CREATE TABLE `oauth_client_details` ( -- ---------------------------- -- Records of oauth_client_details -- ---------------------------- -INSERT INTO `oauth_client_details` VALUES (1, 'app', NULL, '$2a$10$i3F515wEDiB4Gvj9ym9Prui0dasRttEUQ9ink4Wpgb4zEDCAlV8zO', 'app', 'app', 'password,refresh_token', NULL, NULL, 3600, NULL, '{}', 'true', NULL, NULL, '移动端'); -INSERT INTO `oauth_client_details` VALUES (2, 'webApp', NULL, '$2a$10$06msMGYRH8nrm4iVnKFNKOoddB8wOwymVhbUzw/d3ZixD7Nq8ot72', 'webApp', 'app', 'authorization_code,password,refresh_token,client_credentials', NULL, NULL, 3600, NULL, '{}', 'true', NULL, NULL, 'pc端'); +INSERT INTO `oauth_client_details` VALUES (1, 'webApp', NULL, '$2a$10$06msMGYRH8nrm4iVnKFNKOoddB8wOwymVhbUzw/d3ZixD7Nq8ot72', 'webApp', 'app', 'authorization_code,password,refresh_token,client_credentials', NULL, NULL, 3600, NULL, '{}', 'true', NULL, NULL, 'pc端'); +INSERT INTO `oauth_client_details` VALUES (2, 'app', NULL, '$2a$10$i3F515wEDiB4Gvj9ym9Prui0dasRttEUQ9ink4Wpgb4zEDCAlV8zO', 'app', 'app', 'password,refresh_token', NULL, NULL, 3600, NULL, '{}', 'true', NULL, NULL, '移动端'); INSERT INTO `oauth_client_details` VALUES (3, 'zlt', NULL, '$2a$10$/o.wuORzVcXaezmYVzwYMuoY7qeWXBALwQmkskXD/7C6rqfCyPrna', 'zlt', 'all', 'authorization_code,password,refresh_token,client_credentials', 'http://127.0.0.1:8080/singleLogin', NULL, 3600, 28800, '{}', 'true', '2018-12-27 00:50:30', '2018-12-27 00:50:30', '第三方应用'); \ No newline at end of file diff --git a/zlt-doc/sql/user-center.sql b/zlt-doc/sql/user-center.sql index 6ce68c2c3b3d853cd80b7eab6d91743d4eb3cfcc..9c34b1ebb19caaaf2d9f2365848284e3cfd6f17e 100644 --- a/zlt-doc/sql/user-center.sql +++ b/zlt-doc/sql/user-center.sql @@ -20,10 +20,10 @@ CREATE TABLE `sys_user` ( `company` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL, `open_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL, `is_del` tinyint(1) NOT NULL DEFAULT 0, - PRIMARY KEY (`id`) USING BTREE, - INDEX `username`(`username`) USING BTREE, - INDEX `mobile`(`mobile`) USING BTREE, - INDEX `open_id`(`open_id`) USING BTREE + PRIMARY KEY (`id`), + KEY `idx_username` (`username`), + KEY `idx_mobile` (`mobile`), + KEY `idx_open_id` (`open_id`) ) ENGINE = InnoDB AUTO_INCREMENT = 27 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- @@ -52,17 +52,20 @@ CREATE TABLE `sys_role` ( `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '角色名', `create_time` datetime(0) NULL DEFAULT NULL, `update_time` datetime(0) NULL DEFAULT NULL, - PRIMARY KEY (`id`) USING BTREE, - INDEX `code`(`code`) USING BTREE + `tenant_id` varchar(32) DEFAULT '' COMMENT '租户字段', + PRIMARY KEY (`id`), + KEY `idx_code` (`code`), + KEY `idx_tenant_id` (`tenant_id`) ) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of sys_role -- ---------------------------- -INSERT INTO `sys_role` VALUES (1, 'ADMIN', '管理员', '2017-11-17 16:56:59', '2018-09-19 09:39:10'); -INSERT INTO `sys_role` VALUES (2, 'test', '测试', '2018-09-17 10:15:51', '2018-11-15 01:49:14'); -INSERT INTO `sys_role` VALUES (3, '11', '11', '2018-11-15 01:49:19', '2018-11-15 01:49:19'); -INSERT INTO `sys_role` VALUES (4, '123', '1235', '2018-11-15 23:33:39', '2018-12-24 23:53:33'); +INSERT INTO `sys_role` VALUES (1, 'ADMIN', '管理员', '2017-11-17 16:56:59', '2018-09-19 09:39:10', 'webApp'); +INSERT INTO `sys_role` VALUES (2, 'test', '测试', '2018-09-17 10:15:51', '2018-11-15 01:49:14', 'webApp'); +INSERT INTO `sys_role` VALUES (3, '11', '11', '2018-11-15 01:49:19', '2018-11-15 01:49:19', 'webApp'); +INSERT INTO `sys_role` VALUES (4, 'shop_admin', '商城管理员', '2019-08-06 20:02:12.604', '2019-08-06 20:02:12.604', 'zlt'); +INSERT INTO `sys_role` VALUES (5, 'app_admin', '移动管理员', '2019-08-06 20:02:12.604', '2019-08-06 20:02:12.604', 'app'); -- ---------------------------- -- Table structure for sys_role_user @@ -71,7 +74,7 @@ DROP TABLE IF EXISTS `sys_role_user`; CREATE TABLE `sys_role_user` ( `user_id` int(11) NOT NULL, `role_id` int(11) NOT NULL, - PRIMARY KEY (`user_id`, `role_id`) USING BTREE + PRIMARY KEY (`user_id`, `role_id`) ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- @@ -88,7 +91,7 @@ INSERT INTO `sys_role_user` VALUES (8, 2); INSERT INTO `sys_role_user` VALUES (9, 3); INSERT INTO `sys_role_user` VALUES (10, 3); INSERT INTO `sys_role_user` VALUES (11, 4); -INSERT INTO `sys_role_user` VALUES (12, 4); +INSERT INTO `sys_role_user` VALUES (12, 5); -- ---------------------------- -- Table structure for sys_menu @@ -107,40 +110,47 @@ CREATE TABLE `sys_menu` ( `update_time` datetime(0) NULL, `type` tinyint(1) NOT NULL, `hidden` tinyint(1) NOT NULL DEFAULT 0, - PRIMARY KEY (`id`) USING BTREE, - INDEX `parent_id`(`parent_id`) USING BTREE + `tenant_id` varchar(32) DEFAULT '' COMMENT '租户字段', + PRIMARY KEY (`id`), + KEY `idx_parent_id` (`parent_id`), + KEY `idx_tenant_id` (`tenant_id`) ) ENGINE = InnoDB AUTO_INCREMENT = 62 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of sys_menu -- ---------------------------- -INSERT INTO `sys_menu` VALUES (2, 12, '用户管理', '#!user', 'system/user.html', NULL, 'layui-icon-friends', 2, '2017-11-17 16:56:59', '2018-09-19 11:26:14', 1, 0); -INSERT INTO `sys_menu` VALUES (3, 12, '角色管理', '#!role', 'system/role.html', NULL, 'layui-icon-user', 3, '2017-11-17 16:56:59', '2019-01-14 15:34:40', 1, 0); -INSERT INTO `sys_menu` VALUES (4, 12, '菜单管理', '#!menus', 'system/menus.html', NULL, 'layui-icon-menu-fill', 4, '2017-11-17 16:56:59', '2018-09-03 02:23:47', 1, 0); -INSERT INTO `sys_menu` VALUES (9, 37, '文件中心', '#!files', 'files/files.html', NULL, 'layui-icon-file', 3, '2017-11-17 16:56:59', '2019-01-17 20:18:44', 1, 0); -INSERT INTO `sys_menu` VALUES (10, 37, '文档中心', '#!swagger', 'http://127.0.0.1:9900/swagger-ui.html', NULL, 'layui-icon-app', 4, '2017-11-17 16:56:59', '2019-01-17 20:18:48', 1, 0); -INSERT INTO `sys_menu` VALUES (11, 12, '我的信息', '#!myInfo', 'system/myInfo.html', NULL, '', 10, '2017-11-17 16:56:59', '2018-09-02 06:12:24', 1, 1); -INSERT INTO `sys_menu` VALUES (12, -1, '认证管理', 'javascript:;', '', NULL, 'layui-icon-set', 1, '2017-11-17 16:56:59', '2018-12-13 15:02:49', 1, 0); -INSERT INTO `sys_menu` VALUES (35, 12, '应用管理', '#!app', 'attestation/app.html', NULL, 'layui-icon-link', 5, '2017-11-17 16:56:59', '2019-01-14 15:35:15', 1, 0); -INSERT INTO `sys_menu` VALUES (37, -1, '系统管理', 'javascript:;', '', NULL, 'layui-icon-set', 2, '2018-08-25 10:41:58', '2019-01-23 14:01:58', 1, 0); -INSERT INTO `sys_menu` VALUES (62, 63, '应用监控', '#!admin', 'http://127.0.0.1:6500/#/wallboard', NULL, 'layui-icon-chart-screen', 3, '2019-01-08 15:32:19', '2019-01-17 20:22:44', 1, 0); -INSERT INTO `sys_menu` VALUES (63, -1, '系统监控', 'javascript:;', '', NULL, 'layui-icon-set', 2, '2019-01-10 18:35:05', '2019-01-10 18:35:05', 1, 0); -INSERT INTO `sys_menu` VALUES (64, 63, '系统日志', '#!sysLog', 'log/sysLog.html', NULL, 'layui-icon-file-b', 1, '2019-01-10 18:35:55', '2019-01-12 00:27:20', 1, 0); -INSERT INTO `sys_menu` VALUES (65, 37, '代码生成器', '#!generator', 'generator/list.html', NULL, 'layui-icon-template', 2, '2019-01-14 00:47:36', '2019-01-23 14:06:31', 1, 0); -INSERT INTO `sys_menu` VALUES (66, 63, '慢查询SQL', '#!slowQueryLog', 'log/slowQueryLog.html', NULL, 'layui-icon-snowflake', 2, '2019-01-16 12:00:27', '2019-01-16 15:32:31', 1, 0); -INSERT INTO `sys_menu` VALUES (67, -1, '任务管理', '#!job', 'http://127.0.0.1:8081/', NULL, 'layui-icon-date', 3, '2019-01-17 20:18:22', '2019-01-23 14:01:53', 1, 0); -INSERT INTO `sys_menu` VALUES (68, 63, '应用吞吐量监控', '#!sentinel', 'http://127.0.0.1:6999', NULL, 'layui-icon-chart', 4, '2019-01-22 16:31:55', '2019-01-22 16:34:03', 1, 0); -INSERT INTO `sys_menu` VALUES (69, 37, '配置中心', '#!nacos', 'http://127.0.0.1:8848/nacos', NULL, 'layui-icon-tabs', 1, '2019-01-23 14:06:10', '2019-01-23 14:06:10', 1, 0); -INSERT INTO `sys_menu` VALUES (70, 63, 'APM监控', '#!apm', 'http://127.0.0.1:8080', null, 'layui-icon-engine', 5, '2019-02-27 10:31:55', '2019-02-27 10:31:55', 1, 0); -INSERT INTO `sys_menu` VALUES (71, -1, '搜索管理', 'javascript:;', '', NULL, 'layui-icon-set', 3, '2018-08-25 10:41:58', '2019-01-23 15:07:07', 1, 0); -INSERT INTO `sys_menu` VALUES (72, 71, '索引管理', '#!index', 'search/index_manager.html', NULL, 'layui-icon-template', 1, '2019-01-10 18:35:55', '2019-01-12 00:27:20', 1, 0); -INSERT INTO `sys_menu` VALUES (73, 71, '用户搜索', '#!userSearch', 'search/user_search.html', NULL, 'layui-icon-user', 2, '2019-01-10 18:35:55', '2019-01-12 00:27:20', 1, 0); -INSERT INTO `sys_menu` VALUES (74, 12, 'Token管理', '#!tokens', 'system/tokens.html', NULL, 'layui-icon-unlink', 6, '2019-07-11 16:56:59', '2019-07-11 16:56:59', 1, 0); -INSERT INTO `sys_menu` VALUES (75, 2, '用户列表', '/api-user/users', 'user-list', 'GET', null, 1, '2019-07-29 16:56:59', '2019-07-29 16:56:59', 2, 0); -INSERT INTO `sys_menu` VALUES (76, 2, '查询用户角色', '/api-user/roles', 'user-roles', 'GET', null, 2, '2019-07-29 16:56:59', '2019-07-29 16:56:59', 2, 0); -INSERT INTO `sys_menu` VALUES (77, 2, '用户添加', '/api-user/users/saveOrUpdate', 'user-btn-add', 'POST', null, 3, '2019-07-29 16:56:59', '2019-07-29 16:56:59', 2, 0); -INSERT INTO `sys_menu` VALUES (78, 2, '用户导出', '/api-user/users/export', 'user-btn-export', 'POST', null, 4, '2019-07-29 16:56:59', '2019-07-29 16:56:59', 2, 0); -INSERT INTO `sys_menu` VALUES (79, 2, '用户导入', '/api-user/users/import', 'user-btn-import', 'POST', null, 5, '2019-07-29 16:56:59', '2019-07-29 16:56:59', 2, 0); +INSERT INTO `sys_menu` VALUES (2, 12, '用户管理', '#!user', 'system/user.html', NULL, 'layui-icon-friends', 2, '2017-11-17 16:56:59', '2018-09-19 11:26:14', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (3, 12, '角色管理', '#!role', 'system/role.html', NULL, 'layui-icon-user', 3, '2017-11-17 16:56:59', '2019-01-14 15:34:40', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (4, 12, '菜单管理', '#!menus', 'system/menus.html', NULL, 'layui-icon-menu-fill', 4, '2017-11-17 16:56:59', '2018-09-03 02:23:47', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (9, 37, '文件中心', '#!files', 'files/files.html', NULL, 'layui-icon-file', 3, '2017-11-17 16:56:59', '2019-01-17 20:18:44', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (10, 37, '文档中心', '#!swagger', 'http://127.0.0.1:9900/swagger-ui.html', NULL, 'layui-icon-app', 4, '2017-11-17 16:56:59', '2019-01-17 20:18:48', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (11, 12, '我的信息', '#!myInfo', 'system/myInfo.html', NULL, '', 10, '2017-11-17 16:56:59', '2018-09-02 06:12:24', 1, 1, 'webApp'); +INSERT INTO `sys_menu` VALUES (12, -1, '认证管理', 'javascript:;', '', NULL, 'layui-icon-set', 1, '2017-11-17 16:56:59', '2018-12-13 15:02:49', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (35, 12, '应用管理', '#!app', 'attestation/app.html', NULL, 'layui-icon-link', 5, '2017-11-17 16:56:59', '2019-01-14 15:35:15', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (37, -1, '系统管理', 'javascript:;', '', NULL, 'layui-icon-set', 2, '2018-08-25 10:41:58', '2019-01-23 14:01:58', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (62, 63, '应用监控', '#!admin', 'http://127.0.0.1:6500/#/wallboard', NULL, 'layui-icon-chart-screen', 3, '2019-01-08 15:32:19', '2019-01-17 20:22:44', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (63, -1, '系统监控', 'javascript:;', '', NULL, 'layui-icon-set', 2, '2019-01-10 18:35:05', '2019-01-10 18:35:05', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (64, 63, '系统日志', '#!sysLog', 'log/sysLog.html', NULL, 'layui-icon-file-b', 1, '2019-01-10 18:35:55', '2019-01-12 00:27:20', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (65, 37, '代码生成器', '#!generator', 'generator/list.html', NULL, 'layui-icon-template', 2, '2019-01-14 00:47:36', '2019-01-23 14:06:31', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (66, 63, '慢查询SQL', '#!slowQueryLog', 'log/slowQueryLog.html', NULL, 'layui-icon-snowflake', 2, '2019-01-16 12:00:27', '2019-01-16 15:32:31', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (67, -1, '任务管理', '#!job', 'http://127.0.0.1:8081/', NULL, 'layui-icon-date', 3, '2019-01-17 20:18:22', '2019-01-23 14:01:53', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (68, 63, '应用吞吐量监控', '#!sentinel', 'http://127.0.0.1:6999', NULL, 'layui-icon-chart', 4, '2019-01-22 16:31:55', '2019-01-22 16:34:03', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (69, 37, '配置中心', '#!nacos', 'http://127.0.0.1:8848/nacos', NULL, 'layui-icon-tabs', 1, '2019-01-23 14:06:10', '2019-01-23 14:06:10', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (70, 63, 'APM监控', '#!apm', 'http://127.0.0.1:8080', null, 'layui-icon-engine', 5, '2019-02-27 10:31:55', '2019-02-27 10:31:55', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (71, -1, '搜索管理', 'javascript:;', '', NULL, 'layui-icon-set', 3, '2018-08-25 10:41:58', '2019-01-23 15:07:07', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (72, 71, '索引管理', '#!index', 'search/index_manager.html', NULL, 'layui-icon-template', 1, '2019-01-10 18:35:55', '2019-01-12 00:27:20', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (73, 71, '用户搜索', '#!userSearch', 'search/user_search.html', NULL, 'layui-icon-user', 2, '2019-01-10 18:35:55', '2019-01-12 00:27:20', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (74, 12, 'Token管理', '#!tokens', 'system/tokens.html', NULL, 'layui-icon-unlink', 6, '2019-07-11 16:56:59', '2019-07-11 16:56:59', 1, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (75, 2, '用户列表', '/api-user/users', 'user-list', 'GET', null, 1, '2019-07-29 16:56:59', '2019-07-29 16:56:59', 2, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (76, 2, '查询用户角色', '/api-user/roles', 'user-roles', 'GET', null, 2, '2019-07-29 16:56:59', '2019-07-29 16:56:59', 2, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (77, 2, '用户添加', '/api-user/users/saveOrUpdate', 'user-btn-add', 'POST', null, 3, '2019-07-29 16:56:59', '2019-07-29 16:56:59', 2, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (78, 2, '用户导出', '/api-user/users/export', 'user-btn-export', 'POST', null, 4, '2019-07-29 16:56:59', '2019-07-29 16:56:59', 2, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (79, 2, '用户导入', '/api-user/users/import', 'user-btn-import', 'POST', null, 5, '2019-07-29 16:56:59', '2019-07-29 16:56:59', 2, 0, 'webApp'); +INSERT INTO `sys_menu` VALUES (80, -1, '用户管理', '#!user', '', NULL, NULL, 1, '2019-08-06 20:02:12.604', '2019-08-06 20:02:12.604', 1, 0, 'zlt'); +INSERT INTO `sys_menu` VALUES (81, -1, '商品管理', '#!product', '', NULL, NULL, 2, '2019-08-06 20:02:12.604', '2019-08-06 20:02:12.604', 1, 0, 'zlt'); +INSERT INTO `sys_menu` VALUES (82, -1, '支付管理', '#!pay', '', NULL, NULL, 3, '2019-08-06 20:02:12.604', '2019-08-06 20:02:12.604', 1, 0, 'zlt'); +INSERT INTO `sys_menu` VALUES (83, -1, '交易管理', '#!trading', '', NULL, NULL, 4, '2019-08-06 20:02:12.604', '2019-08-06 20:02:12.604', 1, 0, 'zlt'); +INSERT INTO `sys_menu` VALUES (84, -1, '系统管理', '#!system', '', NULL, NULL, 1, '2019-08-06 20:02:12.604', '2019-08-06 20:02:12.604', 1, 0, 'app'); -- ---------------------------- -- Table structure for sys_role_menu @@ -149,7 +159,7 @@ DROP TABLE IF EXISTS `sys_role_menu`; CREATE TABLE `sys_role_menu` ( `role_id` int(11) NOT NULL, `menu_id` int(11) NOT NULL, - PRIMARY KEY (`role_id`, `menu_id`) USING BTREE + PRIMARY KEY (`role_id`, `menu_id`) ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- @@ -191,4 +201,10 @@ INSERT INTO `sys_role_menu` VALUES (2, 35); INSERT INTO `sys_role_menu` VALUES (3, 2); INSERT INTO `sys_role_menu` VALUES (3, 3); INSERT INTO `sys_role_menu` VALUES (3, 4); -INSERT INTO `sys_role_menu` VALUES (3, 12); \ No newline at end of file +INSERT INTO `sys_role_menu` VALUES (3, 12); +INSERT INTO `sys_role_menu` VALUES (3, 12); +INSERT INTO `sys_role_menu` VALUES (4, 80); +INSERT INTO `sys_role_menu` VALUES (4, 81); +INSERT INTO `sys_role_menu` VALUES (4, 82); +INSERT INTO `sys_role_menu` VALUES (4, 83); +INSERT INTO `sys_role_menu` VALUES (5, 84); \ No newline at end of file diff --git a/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java b/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java index a121487d1d7c37ead22711dcbae5692c12cb0312..301d92f3abb290150850c76b8a1cb1e7d2744d2c 100644 --- a/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java +++ b/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java @@ -51,7 +51,7 @@ public class UserInfoHeaderFilter extends ZuulFilter { RequestContext ctx = RequestContext.getCurrentContext(); ctx.addZuulRequestHeader(SecurityConstants.USER_ID_HEADER, String.valueOf(userId)); ctx.addZuulRequestHeader(SecurityConstants.USER_HEADER, username); - ctx.addZuulRequestHeader(SecurityConstants.CLIENT_HEADER, clientId); + ctx.addZuulRequestHeader(SecurityConstants.TENANT_HEADER, clientId); ctx.addZuulRequestHeader(SecurityConstants.ROLE_HEADER, CollectionUtil.join(authentication.getAuthorities(), ",")); } return null; diff --git a/zlt-uaa/src/main/java/com/central/UaaServerApp.java b/zlt-uaa/src/main/java/com/central/UaaServerApp.java index bef59c534f2d70f2566919ff42afa77a2b7a2adc..e58bbca173fed13ad54a80b2e8a54e67349bf70b 100644 --- a/zlt-uaa/src/main/java/com/central/UaaServerApp.java +++ b/zlt-uaa/src/main/java/com/central/UaaServerApp.java @@ -1,6 +1,6 @@ package com.central; -import com.central.common.annotation.EnableLoginArgResolver; +import com.central.common.ribbon.annotation.EnableFeignInterceptor; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @@ -11,9 +11,9 @@ import org.springframework.session.data.redis.config.annotation.web.http.EnableR * @author zlt */ @EnableFeignClients +@EnableFeignInterceptor @EnableDiscoveryClient @EnableRedisHttpSession -@EnableLoginArgResolver @SpringBootApplication public class UaaServerApp { public static void main(String[] args) { diff --git a/zlt-uaa/src/main/java/com/central/oauth/config/KaptchaConfig.java b/zlt-uaa/src/main/java/com/central/oauth/config/KaptchaConfig.java deleted file mode 100644 index 7694c2d028fe52c6a0d8183378b312e9276ae59d..0000000000000000000000000000000000000000 --- a/zlt-uaa/src/main/java/com/central/oauth/config/KaptchaConfig.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.central.oauth.config; - -import com.central.common.constant.SecurityConstants; -import com.google.code.kaptcha.impl.DefaultKaptcha; -import com.google.code.kaptcha.util.Config; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import java.util.Properties; - -/** - * @author zlt - * @date 2018-12-21 21:12:18 - */ -@Configuration -public class KaptchaConfig { - private static final String KAPTCHA_BORDER = "kaptcha.border"; - private static final String KAPTCHA_TEXTPRODUCER_FONT_COLOR = "kaptcha.textproducer.font.color"; - private static final String KAPTCHA_TEXTPRODUCER_CHAR_SPACE = "kaptcha.textproducer.char.space"; - private static final String KAPTCHA_IMAGE_WIDTH = "kaptcha.image.width"; - private static final String KAPTCHA_IMAGE_HEIGHT = "kaptcha.image.height"; - private static final String KAPTCHA_TEXTPRODUCER_CHAR_LENGTH = "kaptcha.textproducer.char.length"; - private static final Object KAPTCHA_IMAGE_FONT_SIZE = "kaptcha.textproducer.font.size"; - - @Bean - public DefaultKaptcha producer() { - Properties properties = new Properties(); - properties.put(KAPTCHA_BORDER, SecurityConstants.DEFAULT_IMAGE_BORDER); - properties.put(KAPTCHA_TEXTPRODUCER_FONT_COLOR, SecurityConstants.DEFAULT_COLOR_FONT); - properties.put(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, SecurityConstants.DEFAULT_CHAR_SPACE); - properties.put(KAPTCHA_IMAGE_WIDTH, SecurityConstants.DEFAULT_IMAGE_WIDTH); - properties.put(KAPTCHA_IMAGE_HEIGHT, SecurityConstants.DEFAULT_IMAGE_HEIGHT); - properties.put(KAPTCHA_IMAGE_FONT_SIZE, SecurityConstants.DEFAULT_IMAGE_FONT_SIZE); - properties.put(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, SecurityConstants.DEFAULT_IMAGE_LENGTH); - Config config = new Config(properties); - DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); - defaultKaptcha.setConfig(config); - return defaultKaptcha; - } -} diff --git a/zlt-uaa/src/main/java/com/central/oauth/config/WebMvcConfig.java b/zlt-uaa/src/main/java/com/central/oauth/config/WebMvcConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..b98efd9a326a756acd5bb103dfdd620f6bb90d8f --- /dev/null +++ b/zlt-uaa/src/main/java/com/central/oauth/config/WebMvcConfig.java @@ -0,0 +1,12 @@ +package com.central.oauth.config; + +import com.central.common.config.DefaultWebMvcConfig; +import org.springframework.context.annotation.Configuration; + +/** + * @author zlt + * @date 2019/8/5 + */ +@Configuration +public class WebMvcConfig extends DefaultWebMvcConfig { +} diff --git a/zlt-uaa/src/main/java/com/central/oauth/controller/ClientController.java b/zlt-uaa/src/main/java/com/central/oauth/controller/ClientController.java index ae19c75259fbbbf0e077bd65c68d37050e787934..ae49878a683335c108456e852877a2814ac83568 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/controller/ClientController.java +++ b/zlt-uaa/src/main/java/com/central/oauth/controller/ClientController.java @@ -40,9 +40,9 @@ public class ClientController { @GetMapping("/all") @ApiOperation(value = "所有应用") - public List allClient() { + public Result> allClient() { PageResult page = clientService.listClent(Maps.newHashMap(), false); - return page.getData(); + return Result.succeed(page.getData()); } @DeleteMapping("/{id}") diff --git a/zlt-uaa/src/main/java/com/central/oauth/controller/OAuth2Controller.java b/zlt-uaa/src/main/java/com/central/oauth/controller/OAuth2Controller.java index 3cedf62f8c3c338e43769d3635693077f083a0a7..2c655a73fb747a202160487bca9b29baa1b1c7d1 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/controller/OAuth2Controller.java +++ b/zlt-uaa/src/main/java/com/central/oauth/controller/OAuth2Controller.java @@ -2,6 +2,7 @@ package com.central.oauth.controller; import com.central.common.constant.SecurityConstants; import com.central.common.utils.ResponseUtil; +import com.central.common.utils.TenantContextHolder; import com.central.oauth2.common.token.MobileAuthenticationToken; import com.central.oauth2.common.token.OpenIdAuthenticationToken; import com.central.oauth2.common.util.AuthUtils; @@ -90,6 +91,8 @@ public class OAuth2Controller { String clientSecret = clientInfos[1]; ClientDetails clientDetails = getClient(clientId, clientSecret); + //保存租户id + TenantContextHolder.setTenant(clientId); TokenRequest tokenRequest = new TokenRequest(MapUtils.EMPTY_MAP, clientId, clientDetails.getScope(), "customer"); OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails); Authentication authentication = authenticationManager.authenticate(token); diff --git a/zlt-uaa/src/main/java/com/central/oauth/controller/TokensController.java b/zlt-uaa/src/main/java/com/central/oauth/controller/TokensController.java index 258a4c592f8e4cee96e0a3cfe30cb3ce6556ba5e..53611a4c01c70ed34a7812fd7ffcbc3fe0bf276f 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/controller/TokensController.java +++ b/zlt-uaa/src/main/java/com/central/oauth/controller/TokensController.java @@ -23,9 +23,9 @@ public class TokensController { @Autowired private ITokensService tokensService; - @GetMapping("/list") + @GetMapping("") @ApiOperation(value = "token列表") - public PageResult list(@RequestParam Map params, @LoginClient String clientId) { - return tokensService.listTokens(params, clientId); + public PageResult list(@RequestParam Map params, String tenantId) { + return tokensService.listTokens(params, tenantId); } } diff --git a/zlt-uaa/src/main/resources/mapper/ClientMapper.xml b/zlt-uaa/src/main/resources/mapper/ClientMapper.xml index 5827e0981698a42691f1d255777d93fffce2e0b8..6a23273d69d45460546f51d6e89a26cbf774adac 100644 --- a/zlt-uaa/src/main/resources/mapper/ClientMapper.xml +++ b/zlt-uaa/src/main/resources/mapper/ClientMapper.xml @@ -41,15 +41,13 @@ and client_id like concat('%', #{params.searchKey}, '%') - \ No newline at end of file diff --git a/zlt-web/back-web/src/main/resources/static/login.html b/zlt-web/back-web/src/main/resources/static/login.html index dc07fb9cac9690639985164bcfb0f9d30f955705..a344328c53de8a1963ad57829f50c2a892f35918 100644 --- a/zlt-web/back-web/src/main/resources/static/login.html +++ b/zlt-web/back-web/src/main/resources/static/login.html @@ -87,7 +87,7 @@ data: obj.field, type: 'POST', beforeSend: function (xhr) { - xhr.setRequestHeader('Authorization', 'Basic ' + window.btoa("webApp:webApp")); + xhr.setRequestHeader('Authorization', 'Basic ' + window.btoa(config.clientId + ":" + config.clientSecret)); }, success: function (data) { //console.log(JSON.stringify(data)); diff --git a/zlt-web/back-web/src/main/resources/static/module/config.js b/zlt-web/back-web/src/main/resources/static/module/config.js index dab6c0e228cadc9257ebe65a1aa22e9b1e39126b..7e2584da3c0ee678478a3409b7e326894d88d312 100644 --- a/zlt-web/back-web/src/main/resources/static/module/config.js +++ b/zlt-web/back-web/src/main/resources/static/module/config.js @@ -18,6 +18,8 @@ layui.define(function (exports) { var config = { base_server: apiUrl, tableName: 'easyweb', // 存储表名 + clientId: 'webApp', // 应用id + clientSecret: 'webApp', // 应用秘钥 autoRender: false, // 窗口大小改变后是否自动重新渲染表格,解决layui数据表格非响应式的问题,目前实现的还不是很好,暂时关闭该功能 pageTabs: true, // 是否开启多标签 // 获取缓存的token diff --git a/zlt-web/back-web/src/main/resources/static/pages/system/menus.html b/zlt-web/back-web/src/main/resources/static/pages/system/menus.html index 3382fd4c2669b076bff4d65acfccebc0c7743dbb..d678b13fb199d6cc1c2a31cf0d3141fb234e9ad9 100644 --- a/zlt-web/back-web/src/main/resources/static/pages/system/menus.html +++ b/zlt-web/back-web/src/main/resources/static/pages/system/menus.html @@ -8,7 +8,8 @@
- 搜索:  + 所属应用: +  搜索: @@ -30,14 +31,22 @@ \ No newline at end of file diff --git a/zlt-web/back-web/src/main/resources/static/pages/system/menus_form.html b/zlt-web/back-web/src/main/resources/static/pages/system/menus_form.html index 35add74a119adf198950d741512f33222f6672a0..d2a52e5b766b849d71be6df5c2f37f8d28544cf9 100644 --- a/zlt-web/back-web/src/main/resources/static/pages/system/menus_form.html +++ b/zlt-web/back-web/src/main/resources/static/pages/system/menus_form.html @@ -95,16 +95,16 @@ form.render('select', 'menus-form'); }; + // 回显menu数据 + let menus = admin.getTempData('t_menus'); // 获取一级菜单 layer.load(2); - admin.req('api-user/menus/findOnes', {}, function (data) { + admin.req('api-user/menus/findOnes', {tenantId: menus.tenantId}, function (data) { layer.closeAll('loading'); if (0 == data.code) { $.each(data.data,function(index,item){ $('#parentId').append(new Option(item.name,item.id));//往下拉菜单里添加元素 }) - // 回显menu数据 - var menus = admin.getTempData('t_menus'); $('#menus-form').attr('method', 'POST'); if (menus) { form.val('menus-form', menus); @@ -119,8 +119,8 @@ // 表单提交事件 form.on('submit(menus-form-submit)', function (data) { layer.load(2); - admin.req('api-user/menus/saveOrUpdate', JSON.stringify(data.field), function (data) { - if (data.resp_code == 0) { + admin.req('api-user/menus/saveOrUpdate?tenantId='+menus.tenantId, JSON.stringify(data.field), function (data) { + if (data.resp_code === 0) { layer.closeAll('loading'); layer.msg(data.resp_msg, {icon: 1, time: 500}); admin.finishPopupCenter(); diff --git a/zlt-web/back-web/src/main/resources/static/pages/system/role.html b/zlt-web/back-web/src/main/resources/static/pages/system/role.html index fd05113d78d7cedc17dc3d3bd914c00b43811ced..9660b99f3828b3eeee6bdabb312d43991926b757 100644 --- a/zlt-web/back-web/src/main/resources/static/pages/system/role.html +++ b/zlt-web/back-web/src/main/resources/static/pages/system/role.html @@ -10,7 +10,8 @@
- 搜索: + 所属应用: +  搜索: -
    -
    - -
    - - -
    - - - - - - - - - - \ No newline at end of file diff --git a/zlt-web/back-web/src/main/resources/static/pages/system/role_form.html b/zlt-web/back-web/src/main/resources/static/pages/system/role_form.html deleted file mode 100644 index afe8a7ac3e3fb1c0bf8eee916d8e58abfbfa837b..0000000000000000000000000000000000000000 --- a/zlt-web/back-web/src/main/resources/static/pages/system/role_form.html +++ /dev/null @@ -1,69 +0,0 @@ - -
    - - -
    - -
    - -
    -
    - - -
    - - \ No newline at end of file diff --git a/zlt-web/back-web/src/main/resources/static/pages/system/tokens.html b/zlt-web/back-web/src/main/resources/static/pages/system/tokens.html index 092b2d10ebe6eb442075bfd7fea33cf6bbfb1eca..02dfada65c738becbcf0da04348ea85890836d36 100644 --- a/zlt-web/back-web/src/main/resources/static/pages/system/tokens.html +++ b/zlt-web/back-web/src/main/resources/static/pages/system/tokens.html @@ -8,7 +8,8 @@
    - 搜索:  + 所属应用: +  搜索:
    @@ -23,19 +24,21 @@ \ No newline at end of file