From 83005acfc12ef7a261964ae24f0d3d16de6eff90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Sun, 16 Sep 2018 18:58:59 +0800 Subject: [PATCH] =?UTF-8?q?:bookmark:=20=E5=8F=91=E5=B8=83=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E7=89=88=E6=9C=AC=E3=80=82=20v=201.6.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/config/WebSecurityConfigurer.java | 2 + .../pigx/auth/endpoint/PigxTokenEndpoint.java | 30 +++++---- .../common/core/constant/CommonConstant.java | 24 +------ pigx-common/pigx-common-security/pom.xml | 6 ++ .../feign/PigxFeignTenantConfiguration.java | 35 +++++++++++ .../feign/PigxFeignTenantInterceptor.java | 40 ++++++++++++ .../security/filter/TenantIdTtlFilter.java | 62 +++++++++++++++++++ .../common/security/service/PigxUser.java | 12 +++- .../service/PigxUserDetailsServiceImpl.java | 4 +- .../common/security/util/SecurityUtils.java | 31 ++++++++-- .../main/resources/META-INF/spring.factories | 2 + .../interceptor/TxManagerInterceptor.java | 13 ++-- .../main/resources/config/pigx-auth-dev.yml | 2 +- .../resources/config/pigx-codegen-dev.yml | 2 +- .../main/resources/config/pigx-daemon-dev.yml | 4 +- .../main/resources/config/pigx-upms-dev.yml | 2 +- .../pigx/admin/api/dto/UserInfo.java | 6 +- .../pigx/admin/api/entity/SysUser.java | 5 ++ .../pig4cloud/pigx/admin/api/vo/UserVO.java | 6 ++ .../admin/config/MybatisPlusConfigurer.java | 40 +++++++++++- .../config/ResourceServerConfigurer.java | 7 ++- .../pigx/admin/controller/MenuController.java | 21 ++----- .../pigx/admin/mapper/SysMenuMapper.java | 4 +- .../pigx/admin/service/SysMenuService.java | 5 +- .../service/impl/SysMenuServiceImpl.java | 6 +- .../service/impl/SysUserServiceImpl.java | 16 ++--- .../main/resources/mapper/SysMenuMapper.xml | 23 ++++--- .../main/resources/mapper/SysUserMapper.xml | 6 ++ pom.xml | 1 + 29 files changed, 313 insertions(+), 104 deletions(-) create mode 100644 pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeignTenantConfiguration.java create mode 100644 pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeignTenantInterceptor.java create mode 100644 pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/filter/TenantIdTtlFilter.java diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java index 24674832d..40a7b06e0 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java @@ -20,6 +20,7 @@ package com.pig4cloud.pigx.auth.config; import com.fasterxml.jackson.databind.ObjectMapper; +import com.pig4cloud.pigx.common.security.filter.TenantIdTtlFilter; import com.pig4cloud.pigx.common.security.mobile.MobileLoginSuccessHandler; import com.pig4cloud.pigx.common.security.mobile.MobileSecurityConfigurer; import com.pig4cloud.pigx.common.security.service.PigxUserDetailsService; @@ -35,6 +36,7 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur import org.springframework.security.crypto.factory.PasswordEncoderFactories; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.oauth2.provider.ClientDetailsService; +import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter; import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java index 142b71fe6..4735664c4 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java @@ -24,6 +24,7 @@ import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.plugins.Page; import com.pig4cloud.pigx.common.core.util.R; import com.pig4cloud.pigx.common.security.service.PigxUser; +import com.pig4cloud.pigx.common.security.util.SecurityUtils; import lombok.AllArgsConstructor; import org.springframework.data.redis.core.ConvertingCursor; import org.springframework.data.redis.core.Cursor; @@ -128,19 +129,14 @@ public class PigxTokenEndpoint { if (authentication instanceof UsernamePasswordAuthenticationToken) { UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) authentication; - - if (authenticationToken.getPrincipal() instanceof PigxUser) { - PigxUser user = (PigxUser) authenticationToken.getPrincipal(); - map.put("user_id", user.getId() + ""); - map.put("user_name", user.getUsername() + ""); + if (extractToken(map, authenticationToken.getPrincipal())) { + continue; } } else if (authentication instanceof PreAuthenticatedAuthenticationToken) { //刷新token方式 PreAuthenticatedAuthenticationToken authenticationToken = (PreAuthenticatedAuthenticationToken) authentication; - if (authenticationToken.getPrincipal() instanceof PigxUser) { - PigxUser user = (PigxUser) authenticationToken.getPrincipal(); - map.put("user_id", user.getId() + ""); - map.put("user_name", user.getUsername() + ""); + if (extractToken(map, authenticationToken.getPrincipal())) { + continue; } } list.add(map); @@ -152,12 +148,22 @@ public class PigxTokenEndpoint { return result; } + private boolean extractToken(Map map, Object principal) { + if (principal instanceof PigxUser) { + PigxUser user = (PigxUser) principal; + if (!user.getTenantId().equals(SecurityUtils.getTenantId())) { + return true; + } + map.put("user_id", user.getId() + ""); + map.put("user_name", user.getUsername() + ""); + } + return false; + } + private List findKeysForPage(String patternKey, int pageNum, int pageSize) { ScanOptions options = ScanOptions.scanOptions().match(patternKey).build(); RedisSerializer redisSerializer = (RedisSerializer) redisTemplate.getKeySerializer(); - Cursor cursor = (Cursor) redisTemplate.executeWithStickyConnection(redisConnection -> { - return new ConvertingCursor<>(redisConnection.scan(options), redisSerializer::deserialize); - }); + Cursor cursor = (Cursor) redisTemplate.executeWithStickyConnection(redisConnection -> new ConvertingCursor<>(redisConnection.scan(options), redisSerializer::deserialize)); List result = new ArrayList<>(); int tmpIndex = 0; int startIndex = (pageNum - 1) * pageSize; diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CommonConstant.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CommonConstant.java index 5b0e0c472..335d63a87 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CommonConstant.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CommonConstant.java @@ -25,19 +25,9 @@ package com.pig4cloud.pigx.common.core.constant; */ public interface CommonConstant { /** - * token请求头名称 + * header 中租户ID */ - String REQ_HEADER = "Authorization"; - - /** - * token分割符 - */ - String TOKEN_SPLIT = "Bearer "; - - /** - * jwt签名 - */ - String SIGN_KEY = "PIG"; + String TENANT_ID = "TENANT_ID"; /** * 删除 */ @@ -76,14 +66,4 @@ public interface CommonConstant { * JSON 资源 */ String CONTENT_TYPE = "application/json; charset=utf-8"; - - /** - * 阿里大鱼 - */ - String ALIYUN_SMS = "aliyun_sms"; - - /** - * 路由信息Redis保存的key - */ - String ROUTE_KEY = "_ROUTE_KEY"; } diff --git a/pigx-common/pigx-common-security/pom.xml b/pigx-common/pigx-common-security/pom.xml index 5d60b469a..0733aa287 100644 --- a/pigx-common/pigx-common-security/pom.xml +++ b/pigx-common/pigx-common-security/pom.xml @@ -51,5 +51,11 @@ pigx-upms-api ${pigx.version} + + + com.alibaba + transmittable-thread-local + ${ttl.version} + diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeignTenantConfiguration.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeignTenantConfiguration.java new file mode 100644 index 000000000..2fc4af44a --- /dev/null +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeignTenantConfiguration.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.security.feign; + +import feign.RequestInterceptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author lengleng + * @date 2018/9/14 + * feign 租户信息拦截 + */ +@Configuration +public class PigxFeignTenantConfiguration { + @Bean + public RequestInterceptor PigxFeignTenantInterceptor() { + return new PigxFeignTenantInterceptor(); + } +} diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeignTenantInterceptor.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeignTenantInterceptor.java new file mode 100644 index 000000000..d4c91281b --- /dev/null +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/feign/PigxFeignTenantInterceptor.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.security.feign; + +import com.pig4cloud.pigx.common.core.constant.CommonConstant; +import com.pig4cloud.pigx.common.security.util.SecurityUtils; +import feign.RequestInterceptor; +import feign.RequestTemplate; +import lombok.extern.slf4j.Slf4j; + +/** + * @author lengleng + * @date 2018/9/14 + */ +@Slf4j +public class PigxFeignTenantInterceptor implements RequestInterceptor { + @Override + public void apply(RequestTemplate requestTemplate) { + if (SecurityUtils.getTenantId() == null) { + log.error("TTL 中的 租户ID为空,feign拦截器 >> 增强失败"); + return; + } + requestTemplate.header(CommonConstant.TENANT_ID, SecurityUtils.getTenantId().toString()); + } +} diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/filter/TenantIdTtlFilter.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/filter/TenantIdTtlFilter.java new file mode 100644 index 000000000..24851a1d0 --- /dev/null +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/filter/TenantIdTtlFilter.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.security.filter; + +import cn.hutool.core.util.StrUtil; +import com.pig4cloud.pigx.common.core.constant.CommonConstant; +import com.pig4cloud.pigx.common.security.util.SecurityUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; +import org.springframework.web.filter.GenericFilterBean; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * @author lengleng + * @date 2018/9/13 + */ +@Slf4j +@Component +@Order(Ordered.HIGHEST_PRECEDENCE) +public class TenantIdTtlFilter extends GenericFilterBean { + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + HttpServletRequest request = (HttpServletRequest) servletRequest; + HttpServletResponse response = (HttpServletResponse) servletResponse; + + String tenantId = request.getHeader(CommonConstant.TENANT_ID); + log.debug("获取header中的租户ID为:{}", tenantId); + + if (StrUtil.isNotBlank(tenantId)) { + SecurityUtils.setTenantId(Integer.parseInt(tenantId)); + } else { + SecurityUtils.setTenantId(1); + } + + filterChain.doFilter(request, response); + } +} diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUser.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUser.java index e69b617fc..cae1f7f50 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUser.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUser.java @@ -41,12 +41,19 @@ public class PigxUser extends User { @Getter private Integer deptId; + /** + * 租户ID + */ + @Getter + private Integer tenantId; + /** * Construct the User with the details required by * {@link DaoAuthenticationProvider}. * * @param id 用户ID * @param deptId 部门ID + * @param tenantId 租户ID * @param username the username presented to the * DaoAuthenticationProvider * @param password the password that should be presented to the @@ -61,9 +68,10 @@ public class PigxUser extends User { * @throws IllegalArgumentException if a null value was passed either as * a parameter or as an element in the GrantedAuthority collection */ - public PigxUser(Integer id, Integer deptId, String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection authorities) { + public PigxUser(Integer id, Integer deptId, Integer tenantId, String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection authorities) { super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities); this.id = id; - this.deptId = id; + this.deptId = deptId; + this.tenantId = tenantId; } } diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUserDetailsServiceImpl.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUserDetailsServiceImpl.java index db6f18550..255ea7d2f 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUserDetailsServiceImpl.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUserDetailsServiceImpl.java @@ -90,7 +90,7 @@ public class PigxUserDetailsServiceImpl implements PigxUserDetailsService { Set dbAuthsSet = new HashSet<>(); if (ArrayUtil.isNotEmpty(info.getRoles())) { // 获取角色 - Arrays.stream(info.getRoles()).forEach(role -> dbAuthsSet.add(SecurityConstants.ROLE + role)); + Arrays.stream(info.getRoles()).forEach(roleId -> dbAuthsSet.add(SecurityConstants.ROLE + roleId)); // 获取资源 dbAuthsSet.addAll(Arrays.asList(info.getPermissions())); @@ -101,7 +101,7 @@ public class PigxUserDetailsServiceImpl implements PigxUserDetailsService { boolean enabled = StrUtil.equals(user.getDelFlag(), CommonConstant.STATUS_NORMAL); // 构造security用户 - return new PigxUser(user.getUserId(), user.getDeptId(), user.getUsername(), SecurityConstants.BCRYPT + user.getPassword(), enabled, + return new PigxUser(user.getUserId(), user.getDeptId(), user.getTenantId(), user.getUsername(), SecurityConstants.BCRYPT + user.getPassword(), enabled, true, true, true, authorities); } } diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/util/SecurityUtils.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/util/SecurityUtils.java index 8e93f4297..6aa9c6a9a 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/util/SecurityUtils.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/util/SecurityUtils.java @@ -21,6 +21,7 @@ package com.pig4cloud.pigx.common.security.util; import cn.hutool.core.util.StrUtil; +import com.alibaba.ttl.TransmittableThreadLocal; import com.pig4cloud.pigx.common.core.constant.SecurityConstants; import com.pig4cloud.pigx.common.security.service.PigxUser; import org.springframework.security.core.Authentication; @@ -38,6 +39,7 @@ import java.util.List; * @author L.cm */ public class SecurityUtils { + private static final ThreadLocal THREAD_LOCAL_TENANT = new TransmittableThreadLocal<>(); /** * 获取Authentication @@ -82,14 +84,35 @@ public class SecurityUtils { * * @return 角色集合 */ - public static List getRoles() { + public static List getRoles() { Authentication authentication = getAuthentication(); Collection authorities = authentication.getAuthorities(); - List roles = new ArrayList<>(); + List roleIds = new ArrayList<>(); authorities.stream() .filter(granted -> StrUtil.startWith(granted.getAuthority(), SecurityConstants.ROLE)) - .forEach(granted -> roles.add(StrUtil.removePrefix(granted.getAuthority(), SecurityConstants.ROLE))); - return roles; + .forEach(granted -> { + String id = StrUtil.removePrefix(granted.getAuthority(), SecurityConstants.ROLE); + roleIds.add(Integer.parseInt(id)); + }); + return roleIds; + } + + /** + * TTL 设置租户ID + * + * @param tenantId + */ + public static void setTenantId(Integer tenantId) { + THREAD_LOCAL_TENANT.set(tenantId); + } + + /** + * 获取TTL中的租户ID + * + * @return + */ + public static Integer getTenantId() { + return THREAD_LOCAL_TENANT.get(); } } diff --git a/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories b/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories index d54329f51..714b4a0b0 100644 --- a/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories +++ b/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories @@ -1,9 +1,11 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.pig4cloud.pigx.common.security.filter.TenantIdTtlFilter,\ com.pig4cloud.pigx.common.security.component.PermissionService,\ com.pig4cloud.pigx.common.security.component.PigAccessDeniedHandler,\ com.pig4cloud.pigx.common.security.component.ResourceAuthExceptionEntryPoint,\ com.pig4cloud.pigx.common.security.component.PigxResourceServerTokenRelayAutoConfiguration,\ com.pig4cloud.pigx.common.security.feign.PigxFeignClientConfiguration,\ + com.pig4cloud.pigx.common.security.feign.PigxFeignTenantConfiguration,\ com.pig4cloud.pigx.common.security.service.PigxUserDetailsServiceImpl,\ com.pig4cloud.pigx.common.security.social.QqSocialConfig,\ com.pig4cloud.pigx.common.security.social.WxSocialConfig diff --git a/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/tx/springcloud/interceptor/TxManagerInterceptor.java b/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/tx/springcloud/interceptor/TxManagerInterceptor.java index bfae5a915..78aaf85af 100755 --- a/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/tx/springcloud/interceptor/TxManagerInterceptor.java +++ b/pigx-common/pigx-common-transaction/src/main/java/com/pig4cloud/pigx/common/transaction/tx/springcloud/interceptor/TxManagerInterceptor.java @@ -28,8 +28,7 @@ import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; /** - *@author LCN on 2017/6/7. - * + * @author LCN on 2017/6/7. * @author LCN * @since 4.1.0 */ @@ -41,13 +40,9 @@ public class TxManagerInterceptor { private AspectBeforeService aspectBeforeService; public Object around(ProceedingJoinPoint point) throws Throwable { - String groupId = null; - try { - RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes(); - HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest(); - groupId = request.getHeader("tx-group"); - } catch (Exception e) { - } + RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes(); + HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest(); + String groupId = request.getHeader("tx-group"); return aspectBeforeService.around(groupId, point); } } diff --git a/pigx-config/src/main/resources/config/pigx-auth-dev.yml b/pigx-config/src/main/resources/config/pigx-auth-dev.yml index 0c63c3fc4..f8a9b6464 100644 --- a/pigx-config/src/main/resources/config/pigx-auth-dev.yml +++ b/pigx-config/src/main/resources/config/pigx-auth-dev.yml @@ -5,4 +5,4 @@ spring: driver-class-name: com.mysql.jdbc.Driver username: root password: Bjyjht2017!@# - url: jdbc:mysql://127.0.0.1:3306/pigx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false + url: jdbc:mysql://114.116.30.176:3306/pigx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false diff --git a/pigx-config/src/main/resources/config/pigx-codegen-dev.yml b/pigx-config/src/main/resources/config/pigx-codegen-dev.yml index 1fb8367b9..46304f871 100644 --- a/pigx-config/src/main/resources/config/pigx-codegen-dev.yml +++ b/pigx-config/src/main/resources/config/pigx-codegen-dev.yml @@ -13,7 +13,7 @@ spring: driver-class-name: com.mysql.jdbc.Driver username: root password: Bjyjht2017!@# - url: jdbc:mysql://127.0.0.1:3306/pigx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false + url: jdbc:mysql://114.116.30.176:3306/pigx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false jackson: time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss diff --git a/pigx-config/src/main/resources/config/pigx-daemon-dev.yml b/pigx-config/src/main/resources/config/pigx-daemon-dev.yml index 855d61369..effd2ac2c 100644 --- a/pigx-config/src/main/resources/config/pigx-daemon-dev.yml +++ b/pigx-config/src/main/resources/config/pigx-daemon-dev.yml @@ -14,11 +14,11 @@ spring: driver-class-name: com.mysql.jdbc.Driver username: root password: Bjyjht2017!@# - url: jdbc:mysql://127.0.0.1:3306/pigx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false + url: jdbc:mysql://114.116.30.176:3306/pigx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false elasticjob: # 分布式任务协调依赖zookeeper zookeeper: - server-lists: 127.0.0.1:13181 + server-lists: 114.116.45.246:13181 namespace: pigx-daemon # 普通任务 simples: diff --git a/pigx-config/src/main/resources/config/pigx-upms-dev.yml b/pigx-config/src/main/resources/config/pigx-upms-dev.yml index c4e702b58..541b41bde 100644 --- a/pigx-config/src/main/resources/config/pigx-upms-dev.yml +++ b/pigx-config/src/main/resources/config/pigx-upms-dev.yml @@ -13,7 +13,7 @@ spring: driver-class-name: com.mysql.jdbc.Driver username: root password: Bjyjht2017!@# - url: jdbc:mysql://127.0.0.1:3306/pigx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false + url: jdbc:mysql://114.116.30.176:3306/pigx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false # mybaits 模块配置 mybatis-plus: diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/dto/UserInfo.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/dto/UserInfo.java index 448ef2ac8..e406ef716 100644 --- a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/dto/UserInfo.java +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/dto/UserInfo.java @@ -46,7 +46,7 @@ public class UserInfo implements Serializable { /** * 角色集合 */ - private String[] roles; + private Integer[] roles; public SysUser getSysUser() { return sysUser; @@ -64,11 +64,11 @@ public class UserInfo implements Serializable { this.permissions = permissions; } - public String[] getRoles() { + public Integer[] getRoles() { return roles; } - public void setRoles(String[] roles) { + public void setRoles(Integer[] roles) { this.roles = roles; } } diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysUser.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysUser.java index d5739ddc8..61d60f171 100644 --- a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysUser.java +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysUser.java @@ -87,6 +87,11 @@ public class SysUser implements Serializable { */ private Integer deptId; + /** + * 租户ID + */ + private Integer tenantId; + /** * 微信openid */ diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java index 6c6ff9745..ece6eb2eb 100644 --- a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java @@ -86,6 +86,12 @@ public class UserVO implements Serializable { * 部门ID */ private Integer deptId; + + /** + * 租户ID + */ + private Integer tenantId; + /** * 部门名称 */ diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/MybatisPlusConfigurer.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/MybatisPlusConfigurer.java index 9e3bbacd4..a05c02fc1 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/MybatisPlusConfigurer.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/MybatisPlusConfigurer.java @@ -19,19 +19,33 @@ package com.pig4cloud.pigx.admin.config; +import cn.hutool.core.util.ArrayUtil; import com.baomidou.mybatisplus.plugins.PaginationInterceptor; +import com.baomidou.mybatisplus.plugins.parser.ISqlParser; +import com.baomidou.mybatisplus.plugins.parser.tenant.TenantHandler; +import com.baomidou.mybatisplus.plugins.parser.tenant.TenantSqlParser; import com.pig4cloud.pigx.common.core.datascope.DataScopeInterceptor; +import com.pig4cloud.pigx.common.security.util.SecurityUtils; +import lombok.extern.slf4j.Slf4j; +import net.sf.jsqlparser.expression.Expression; +import net.sf.jsqlparser.expression.LongValue; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.ArrayList; +import java.util.List; + /** * @author lengleng * @date 2017/10/29 */ +@Slf4j @Configuration @MapperScan("com.pig4cloud.pigx.admin.mapper") public class MybatisPlusConfigurer { + private static final String[] IGNORETABLES = new String[]{"sys_dept_relation", "sys_role_dept", "sys_role_menu", "sys_user_role"}; + /** * 分页插件 * @@ -39,7 +53,31 @@ public class MybatisPlusConfigurer { */ @Bean public PaginationInterceptor paginationInterceptor() { - return new PaginationInterceptor(); + PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); + List sqlParserList = new ArrayList<>(); + TenantSqlParser tenantSqlParser = new TenantSqlParser(); + tenantSqlParser.setTenantHandler(new TenantHandler() { + @Override + public Expression getTenantId() { + Integer tenantId = SecurityUtils.getTenantId(); + log.error("当前租户为 >> {}", tenantId); + return new LongValue(tenantId); + } + + @Override + public String getTenantIdColumn() { + return "tenant_id"; + } + + @Override + public boolean doTableFilter(String tableName) { + log.error("当前租户表为 >> {}", tableName); + return ArrayUtil.contains(IGNORETABLES, tableName); + } + }); + sqlParserList.add(tenantSqlParser); + paginationInterceptor.setSqlParserList(sqlParserList); + return paginationInterceptor; } /** diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/ResourceServerConfigurer.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/ResourceServerConfigurer.java index 03f67f3a7..3b0cfdc17 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/ResourceServerConfigurer.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/ResourceServerConfigurer.java @@ -20,22 +20,27 @@ package com.pig4cloud.pigx.admin.config; import com.pig4cloud.pigx.common.security.component.BaseResourceServerConfigurerAdapter; +import com.pig4cloud.pigx.common.security.filter.TenantIdTtlFilter; +import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; +import org.springframework.security.web.access.channel.ChannelProcessingFilter; /** * @author lengleng * @date 2018/6/22 */ +@Slf4j @Configuration @EnableResourceServer @EnableGlobalMethodSecurity(prePostEnabled = true) public class ResourceServerConfigurer extends BaseResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { - http.authorizeRequests() + http + .authorizeRequests() .antMatchers("/actuator/**" , "/user/info/*" , "/social/info/**" diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/MenuController.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/MenuController.java index b6b5b499d..faff502f2 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/MenuController.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/MenuController.java @@ -46,17 +46,6 @@ public class MenuController { @Autowired private SysMenuService sysMenuService; - /** - * 通过角色名称查询用户菜单 - * - * @param role 角色名称 - * @return 菜单列表 - */ - @GetMapping("/findMenuByRole/{role}") - public List findMenuByRole(@PathVariable String role) { - return sysMenuService.findMenuByRoleCode(role); - } - /** * 返回当前用户的树形菜单集合 * @@ -66,7 +55,7 @@ public class MenuController { public List userMenu() { // 获取符合条件得菜单 Set all = new HashSet<>(); - SecurityUtils.getRoles().forEach(roleName -> all.addAll(sysMenuService.findMenuByRoleCode(roleName))); + SecurityUtils.getRoles().forEach(roleId -> all.addAll(sysMenuService.findMenuByRoleId(roleId))); List menuTreeList = new ArrayList<>(); all.forEach(menuVo -> { if (CommonConstant.MENU.equals(menuVo.getType())) { @@ -92,12 +81,12 @@ public class MenuController { /** * 返回角色的菜单集合 * - * @param roleName 角色名称 + * @param roleId 角色ID * @return 属性集合 */ - @GetMapping("/roleTree/{roleName}") - public List roleTree(@PathVariable String roleName) { - List menus = sysMenuService.findMenuByRoleCode(roleName); + @GetMapping("/roleTree/{roleId}") + public List roleTree(@PathVariable Integer roleId) { + List menus = sysMenuService.findMenuByRoleId(roleId); List menuList = new ArrayList<>(); for (MenuVO menuVo : menus) { menuList.add(menuVo.getMenuId()); diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/mapper/SysMenuMapper.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/mapper/SysMenuMapper.java index 5304ebca7..c0c0164f6 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/mapper/SysMenuMapper.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/mapper/SysMenuMapper.java @@ -38,10 +38,10 @@ public interface SysMenuMapper extends BaseMapper { /** * 通过角色编号查询菜单 * - * @param role 角色编号 + * @param roleId 角色ID * @return */ - List findMenuByRoleCode(String role); + List findMenuByRoleId(Integer roleId); /** * 通过角色ID查询权限 diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysMenuService.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysMenuService.java index 3800947a7..ba05b2e04 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysMenuService.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysMenuService.java @@ -23,6 +23,7 @@ package com.pig4cloud.pigx.admin.service; import com.baomidou.mybatisplus.service.IService; import com.pig4cloud.pigx.admin.api.entity.SysMenu; import com.pig4cloud.pigx.admin.api.vo.MenuVO; +import io.swagger.models.auth.In; import java.util.List; @@ -38,10 +39,10 @@ public interface SysMenuService extends IService { /** * 通过角色编号查询URL 权限 * - * @param role 角色编号 + * @param roleId 角色ID * @return 菜单列表 */ - List findMenuByRoleCode(String role); + List findMenuByRoleId(Integer roleId); /** * 级联删除菜单 diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysMenuServiceImpl.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysMenuServiceImpl.java index b5f398e5b..a5af90699 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysMenuServiceImpl.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysMenuServiceImpl.java @@ -47,9 +47,9 @@ public class SysMenuServiceImpl extends ServiceImpl impl private SysMenuMapper sysMenuMapper; @Override - @Cacheable(value = "menu_details", key = "#role + '_menu'") - public List findMenuByRoleCode(String role) { - return sysMenuMapper.findMenuByRoleCode(role); + @Cacheable(value = "menu_details", key = "#roleId + '_menu'") + public List findMenuByRoleId(Integer roleId) { + return sysMenuMapper.findMenuByRoleId(roleId); } @Override diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java index 5dbfab2c1..a9b73ef20 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java @@ -78,7 +78,7 @@ public class SysUserServiceImpl extends ServiceImpl impl * @return */ @Override - @Cacheable(value = "user_details", key = "#username",unless = "#result == null") + @Cacheable(value = "user_details", key = "#username", unless="#result == null") public UserInfo findUserInfo(String type, String username) { SysUser condition = new SysUser(); if (EnumLoginType.PWD.getType().equals(type)) { @@ -91,22 +91,22 @@ public class SysUserServiceImpl extends ServiceImpl impl SysUser sysUser = this.selectOne(new EntityWrapper<>(condition)); if (sysUser == null) { return null; - } + } UserInfo userInfo = new UserInfo(); userInfo.setSysUser(sysUser); - //设置角色列表 + //设置角色列表 (ID) List roleList = sysRoleService.findRolesByUserId(sysUser.getUserId()); - List roleCodes = new ArrayList<>(); + List roleIds = new ArrayList<>(); if (CollUtil.isNotEmpty(roleList)) { - roleList.forEach(sysRole -> roleCodes.add(sysRole.getRoleCode())); + roleList.forEach(sysRole -> roleIds.add(sysRole.getRoleId())); } - userInfo.setRoles(ArrayUtil.toArray(roleCodes, String.class)); + userInfo.setRoles(ArrayUtil.toArray(roleIds, Integer.class)); //设置权限列表(menu.permission) Set menuVoSet = new HashSet<>(); - for (String role : roleCodes) { - List menuVos = sysMenuService.findMenuByRoleCode(role); + for (Integer roleId : roleIds) { + List menuVos = sysMenuService.findMenuByRoleId(roleId); menuVoSet.addAll(menuVos); } Set permissions = new HashSet<>(); diff --git a/pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysMenuMapper.xml b/pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysMenuMapper.xml index 9eb4f10d3..278bebc20 100644 --- a/pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysMenuMapper.xml +++ b/pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysMenuMapper.xml @@ -53,18 +53,17 @@ - + SELECT + sys_menu.* + FROM + sys_menu + LEFT JOIN sys_role_menu ON sys_menu.menu_id = sys_role_menu.menu_id + WHERE + sys_menu.del_flag = 0 + AND sys_role_menu.role_id = #{roleId} + ORDER BY + sys_menu.sort DESC diff --git a/pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysUserMapper.xml b/pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysUserMapper.xml index f5b52b975..e35e97242 100644 --- a/pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysUserMapper.xml +++ b/pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysUserMapper.xml @@ -29,6 +29,7 @@ + @@ -50,6 +51,7 @@ + @@ -76,6 +78,7 @@ `user`.update_time AS uupdate_time, `user`.del_flag AS udel_flag, `user`.dept_id AS deptId, + `user`.tenant_id AS tenantId, r.role_id, r.role_name, r.role_code, @@ -106,6 +109,7 @@ `user`.create_time AS ucreate_time, `user`.update_time AS uupdate_time, `user`.del_flag AS udel_flag, + `user`.tenant_id AS tenantId, r.role_id, r.role_name, r.role_code, @@ -136,6 +140,7 @@ `user`.create_time AS ucreate_time, `user`.update_time AS uupdate_time, `user`.del_flag AS udel_flag, + `user`.tenant_id AS tenantId, r.role_id, r.role_name, r.role_code, @@ -165,6 +170,7 @@ avatar, salt, dept_id, + tenant_id, wx_openid, qq_openid, create_time, diff --git a/pom.xml b/pom.xml index d541a868c..4b5083a8f 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,7 @@ 1.7 4.1.0 2.1.0 + 2.6.1 2.1.5 2.3.3.RELEASE 2.9.6