Browse Source

修复多数据源下 业务数据库插入失败的问题

NorthLan 7 years ago
parent
commit
7cc02574ae

+ 27 - 0
zen-api/src/main/kotlin/com/gxzc/zen/sso/aop/CurrentUserHandler.kt

@@ -0,0 +1,27 @@
+package com.gxzc.zen.sso.aop
+
+import com.gxzc.zen.api.util.SysUserUtil
+import com.gxzc.zen.umps.util.SSOUtil
+import org.springframework.web.servlet.ModelAndView
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter
+import javax.servlet.http.HttpServletRequest
+import javax.servlet.http.HttpServletResponse
+
+/**
+ *
+ * @author NorthLan
+ * @date 2018/3/24
+ * @url https://noahlan.com
+ */
+class CurrentUserHandler : HandlerInterceptorAdapter() {
+
+    override fun preHandle(request: HttpServletRequest?, response: HttpServletResponse?, handler: Any?): Boolean {
+        SSOUtil.TL.set(SysUserUtil.getById(SSOUtil.getCurUserId()!!)!!)
+        return super.preHandle(request, response, handler)
+    }
+
+    override fun postHandle(request: HttpServletRequest?, response: HttpServletResponse?, handler: Any?, modelAndView: ModelAndView?) {
+        SSOUtil.TL.clear()
+        super.postHandle(request, response, handler, modelAndView)
+    }
+}

+ 21 - 0
zen-api/src/main/kotlin/com/gxzc/zen/sso/config/CurrentUserWebConfigurer.kt

@@ -0,0 +1,21 @@
+package com.gxzc.zen.sso.config
+
+import com.gxzc.zen.sso.aop.CurrentUserHandler
+import org.springframework.context.annotation.Configuration
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
+
+/**
+ *
+ * @author NorthLan
+ * @date 2018/3/24
+ * @url https://noahlan.com
+ */
+@Configuration
+class CurrentUserWebConfigurer : WebMvcConfigurerAdapter() {
+
+    override fun addInterceptors(registry: InterceptorRegistry?) {
+        registry?.addInterceptor(CurrentUserHandler())
+        super.addInterceptors(registry)
+    }
+}

+ 0 - 1
zen-api/src/main/kotlin/com/gxzc/zen/umps/config/KissoWebAppConfigurer.kt

@@ -1,6 +1,5 @@
 package com.gxzc.zen.umps.config
 
-import com.baomidou.kisso.web.interceptor.SSOPermissionInterceptor
 import com.baomidou.kisso.web.interceptor.SSOSpringInterceptor
 import org.springframework.context.annotation.Configuration
 import org.springframework.web.bind.annotation.ControllerAdvice

+ 24 - 5
zen-api/src/main/kotlin/com/gxzc/zen/umps/util/SSOUtil.kt

@@ -3,9 +3,7 @@ package com.gxzc.zen.umps.util
 import com.baomidou.kisso.SSOHelper
 import com.baomidou.kisso.SSOToken
 import com.gxzc.zen.api.sys.model.SysUser
-import com.gxzc.zen.api.sys.service.ISysUserService
 import com.gxzc.zen.common.util.HttpUtil
-import com.gxzc.zen.common.util.SpringContextHolder
 
 /**
  * SSO 工具类
@@ -14,14 +12,35 @@ import com.gxzc.zen.common.util.SpringContextHolder
  * @url https://noahlan.com
  */
 object SSOUtil {
-    private val sysUserService: ISysUserService = SpringContextHolder.getBean(ISysUserService::class.java)
-
     fun getCurUserId(): Long? {
         val token = SSOHelper.attrToken<SSOToken>(HttpUtil.getRequest()) ?: SSOHelper.getToken(HttpUtil.getRequest())
         return token?.uid?.toLong()
     }
 
     fun getCurUser(): SysUser? {
-        return sysUserService.getUserByIdCacheable(getCurUserId() ?: return null)
+        return TL.get()
     }
+
+
+    object TL {
+        /**
+         * 180324
+         * 于MetaObjectHandler执行前 每个Controller方法拦截 暂存当前用户引用
+         */
+        private val CURRENT_USER_TL = ThreadLocal<SysUser>()
+
+        fun set(entity: SysUser) {
+            CURRENT_USER_TL.set(entity)
+        }
+
+        fun clear() {
+            CURRENT_USER_TL.remove()
+        }
+
+        fun get(): SysUser {
+            return CURRENT_USER_TL.get()
+        }
+    }
+
+
 }

+ 0 - 4
zen-orm/src/main/kotlin/com/gxzc/zen/orm/DynamicMultipleDataSource.kt

@@ -24,10 +24,6 @@ class DynamicMultipleDataSource : AbstractRoutingDataSource() {
             DATA_SOURCE_KEY.set(key)
         }
 
-        fun getDataSource(): String {
-            return DATA_SOURCE_KEY.get()
-        }
-
         fun clear() {
             DATA_SOURCE_KEY.remove()
         }

+ 6 - 8
zen-web/src/main/kotlin/com/gxzc/zen/orm/CustomMetaObjectHandler.kt

@@ -20,24 +20,22 @@ class CustomMetaObjectHandler : MetaObjectHandler() {
     }
 
     override fun insertFill(metaObject: MetaObject?) {
-        val temp = DynamicMultipleDataSource.getDataSource()
-        logger.debug("公共字段自动填充: insertFill ${SSOUtil.getCurUser()?.account}")
+        val curUser = SSOUtil.getCurUser()
+        logger.debug("公共字段自动填充: insertFill ${curUser?.account}")
         if (getFieldValByName("createBy", metaObject) == null) {
-            setFieldValByName("createBy", SSOUtil.getCurUser()?.account, metaObject)
+            setFieldValByName("createBy", curUser?.account, metaObject)
         }
         if (getFieldValByName("createTime", metaObject) == null) {
             setFieldValByName("createTime", Date(), metaObject)
         }
-        DynamicMultipleDataSource.setDataSource(temp)
     }
 
     override fun updateFill(metaObject: MetaObject?) {
-        val temp = DynamicMultipleDataSource.getDataSource()
-        logger.debug("公共字段自动填充: updateFill ${SSOUtil.getCurUser()?.account}")
+        val curUser = SSOUtil.getCurUser()
+        logger.debug("公共字段自动填充: updateFill ${curUser?.account}")
         if (getFieldValByName("updateBy", metaObject) == null) {
-            setFieldValByName("updateBy", SSOUtil.getCurUser()?.account, metaObject)
+            setFieldValByName("updateBy", curUser?.account, metaObject)
         }
         setFieldValByName("updateTime", Date(), metaObject)
-        DynamicMultipleDataSource.setDataSource(temp)
     }
 }