소스 검색

fix token

he2802 4 년 전
부모
커밋
f81133d9b6

+ 42 - 3
zhongzheng-admin-saas/src/main/java/com/zhongzheng/controller/system/SuperSysLoginController.java

@@ -2,20 +2,34 @@ package com.zhongzheng.controller.system;
 
 import com.zhongzheng.common.constant.Constants;
 import com.zhongzheng.common.core.domain.AjaxResult;
+import com.zhongzheng.common.core.domain.entity.SysUser;
 import com.zhongzheng.common.core.domain.entity.TopSysUser;
 import com.zhongzheng.common.core.domain.entity.SysMenu;
 import com.zhongzheng.common.core.domain.model.LoginBody;
+import com.zhongzheng.common.core.domain.model.LoginUser;
 import com.zhongzheng.common.core.domain.model.TopLoginUser;
+import com.zhongzheng.common.exception.CustomException;
+import com.zhongzheng.common.exception.user.UserPasswordNotMatchException;
+import com.zhongzheng.common.utils.MessageUtils;
 import com.zhongzheng.common.utils.ServletUtils;
+import com.zhongzheng.framework.manager.AsyncManager;
+import com.zhongzheng.framework.manager.factory.AsyncFactory;
+import com.zhongzheng.framework.web.service.TokenService;
 import com.zhongzheng.framework.web.service.TopSysLoginService;
 import com.zhongzheng.framework.web.service.TopTokenService;
 import com.zhongzheng.framework.web.service.SysPermissionService;
 import com.zhongzheng.modules.system.service.ISysMenuService;
+import com.zhongzheng.modules.system.service.ISysUserService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.authentication.BadCredentialsException;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.Authentication;
 import org.springframework.web.bind.annotation.*;
 
+import javax.annotation.Resource;
 import java.util.*;
 
 /**
@@ -37,8 +51,15 @@ public class SuperSysLoginController
     private SysPermissionService permissionService;
 
     @Autowired
-    private TopTokenService tokenService;
+    private TopTokenService topTokenService;
 
+
+
+    @Autowired
+    private TokenService tokenService;
+
+    @Autowired
+    private ISysUserService userService;
     /**
      * 登录方法
      *
@@ -66,7 +87,7 @@ public class SuperSysLoginController
     @GetMapping("getInfo")
     public AjaxResult getInfo()
     {
-        TopLoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
+        TopLoginUser loginUser = topTokenService.getLoginUser(ServletUtils.getRequest());
         TopSysUser user = loginUser.getUser();
         // 角色集合
         Set<String> roles = new HashSet<String>();
@@ -90,10 +111,28 @@ public class SuperSysLoginController
     @GetMapping("getRouters")
     public AjaxResult getRouters()
     {
-        TopLoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
+        TopLoginUser loginUser = topTokenService.getLoginUser(ServletUtils.getRequest());
         // 用户信息
         TopSysUser user = loginUser.getUser();
         List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
         return AjaxResult.success(menuService.buildMenus(menus));
     }
+
+    /**
+     * 获取子系统令牌
+     *
+     * @return 获取子系统令牌
+     */
+    @ApiOperation("获取子系统令牌")
+    @GetMapping("getChildToken")
+    public AjaxResult getChildToken()
+    {
+        //普通系统用户
+        SysUser user = userService.selectUserByUserName("admin");
+        LoginUser loginUser = new LoginUser(user, permissionService.getMenuPermission(user));
+        String token = tokenService.createToken(loginUser);
+        AjaxResult ajax = AjaxResult.success();
+        ajax.put(Constants.TOKEN, token);
+        return ajax;
+    }
 }

+ 1 - 1
zhongzheng-admin-saas/src/main/resources/application-dev.yml

@@ -62,7 +62,7 @@ spring:
         # 端口,默认为6379
         port: 6379
         # 数据库索引
-        database: 3
+        database: 0
         # 密码
         password: zhongzheng2021_redis
         # 连接超时时间

+ 1 - 1
zhongzheng-admin-saas/src/main/resources/application-prod.yml

@@ -62,7 +62,7 @@ spring:
         # 端口,默认为6379
         port: 6379
         # 数据库索引
-        database: 3
+        database: 0
         # 密码
         password: zhongzheng2021_redis
         # 连接超时时间

+ 0 - 1
zhongzheng-admin-saas/src/main/resources/application.yml

@@ -137,7 +137,6 @@ mybatis-plus:
   # 指定外部化 MyBatis Properties 配置,通过该配置可以抽离配置,实现不同环境的配置部署
   configurationProperties: null
   configuration:
-    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
     # 自动驼峰命名规则(camel case)映射
     # 如果您的数据库命名符合规则无需使用 @TableField 注解指定数据库字段名
     mapUnderscoreToCamelCase: true

+ 0 - 1
zhongzheng-common/src/main/java/com/zhongzheng/common/constant/Constants.java

@@ -103,7 +103,6 @@ public class Constants
 
     public static final String WX_LOGIN_USER_KEY = "wx_login_user_key";
 
-    public static final String SUPER_LOGIN_USER_KEY = "super_login_user_key";
 
     /**
      * 用户ID

+ 22 - 6
zhongzheng-framework/src/main/java/com/zhongzheng/framework/aspectj/LogAspect.java

@@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.zhongzheng.common.annotation.Log;
 import com.zhongzheng.common.core.domain.model.LoginUser;
+import com.zhongzheng.common.core.domain.model.TopLoginUser;
 import com.zhongzheng.common.enums.BusinessStatus;
 import com.zhongzheng.common.enums.HttpMethod;
 import com.zhongzheng.common.filter.XssHttpServletRequestWrapper;
@@ -17,6 +18,7 @@ import com.zhongzheng.common.utils.spring.SpringUtils;
 import com.zhongzheng.framework.manager.AsyncManager;
 import com.zhongzheng.framework.manager.factory.AsyncFactory;
 import com.zhongzheng.framework.web.service.TokenService;
+import com.zhongzheng.framework.web.service.TopTokenService;
 import com.zhongzheng.modules.system.domain.SysOperLog;
 import org.aspectj.lang.JoinPoint;
 import org.aspectj.lang.Signature;
@@ -27,6 +29,7 @@ import org.aspectj.lang.annotation.Pointcut;
 import org.aspectj.lang.reflect.MethodSignature;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 import org.springframework.validation.BindingResult;
 import org.springframework.web.multipart.MultipartFile;
@@ -50,6 +53,9 @@ public class LogAspect
 {
     private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
 
+    @Value("${mybatis-plus.tenant.enabled-tenant:true}")
+    private boolean enabledTenant;
+
     // 配置织入点
     @Pointcut("@annotation(com.zhongzheng.common.annotation.Log)")
     public void logPointCut()
@@ -89,9 +95,22 @@ public class LogAspect
             {
                 return;
             }
+            String username = null;
+            if(enabledTenant){
+                // 获取当前的用户
+                LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
+                if (loginUser != null)
+                {
+                    username = loginUser.getUsername();
+                }
+            }else{
+                TopLoginUser topLoginUser = SpringUtils.getBean(TopTokenService.class).getLoginUser(ServletUtils.getRequest());
+                if (topLoginUser != null)
+                {
+                    username = "SAAS##"+topLoginUser.getUsername();
+                }
+            }
 
-            // 获取当前的用户
-            LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
 
             // *========数据库日志=========*//
             SysOperLog operLog = new SysOperLog();
@@ -103,10 +122,7 @@ public class LogAspect
             operLog.setJsonResult(JSON.toJSONString(jsonResult));
 
             operLog.setOperUrl(ServletUtils.getRequest().getRequestURI());
-            if (loginUser != null)
-            {
-                operLog.setOperName(loginUser.getUsername());
-            }
+            operLog.setOperName(username);
 
             if (e != null)
             {

+ 8 - 3
zhongzheng-framework/src/main/java/com/zhongzheng/framework/security/filter/JwtAuthenticationTokenFilter.java

@@ -10,6 +10,7 @@ import com.zhongzheng.framework.web.service.TokenService;
 import com.zhongzheng.framework.web.service.WxTokenService;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
@@ -39,6 +40,9 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
     @Autowired
     private TopTokenService topTokenService;
 
+    @Value("${mybatis-plus.tenant.enabled-tenant:true}")
+    private boolean enabledTenant;
+
     @Override
     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
             throws ServletException, IOException
@@ -54,9 +58,8 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
                 SecurityContextHolder.getContext().setAuthentication(authenticationToken);
             }
         }else{
-            String topToken = topTokenService.getToken(request);
-            if(StringUtils.isNoneEmpty(topToken)){
-                //超级管理员
+            if(!enabledTenant){
+                //SAAS管理员
                 TopLoginUser top_loginUser = topTokenService.getLoginUser(request);
                 if (Validator.isNotNull(top_loginUser) && Validator.isNull(SecurityUtils.getAuthentication()))
                 {
@@ -66,6 +69,7 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
                     SecurityContextHolder.getContext().setAuthentication(authenticationToken);
                 }
             }else{
+                //子系统
                 LoginUser loginUser = tokenService.getLoginUser(request);
                 if (Validator.isNotNull(loginUser) && Validator.isNull(SecurityUtils.getAuthentication()))
                 {
@@ -75,6 +79,7 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
                     SecurityContextHolder.getContext().setAuthentication(authenticationToken);
                 }
             }
+
         }
         chain.doFilter(request, response);
     }

+ 1 - 1
zhongzheng-system/src/main/java/com/zhongzheng/modules/system/domain/SysTenant.java

@@ -25,7 +25,7 @@ private static final long serialVersionUID=1L;
 
 
     /** 租户ID */
-    @TableId(value = "tenant_id")
+    @TableId(value = "tenant_id",type= IdType.ASSIGN_ID)
     private Long tenantId;
 
     /** $column.columnComment */

+ 0 - 3
zhongzheng-system/src/main/java/com/zhongzheng/modules/system/service/impl/SysTenantServiceImpl.java

@@ -71,9 +71,6 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
     @Override
     public Boolean insertByAddBo(SysTenantAddBo bo) {
         SysTenant add = BeanUtil.toBean(bo, SysTenant.class);
-        //雪花算法产生账号ID
-        SnowflakeIdUtils idWorker = new SnowflakeIdUtils(3, 1);
-        add.setTenantId(idWorker.nextId());
         validEntityBeforeSave(add);
         add.setCreateTime(DateUtils.getNowTime());
         add.setUpdateTime(DateUtils.getNowTime());