Browse Source

修复未登录时暂存用户bug

NorthLan 7 years ago
parent
commit
92352aee48

+ 9 - 3
zen-api/src/main/kotlin/com/gxzc/zen/sso/aop/CurrentUserHandler.kt

@@ -21,10 +21,16 @@ class CurrentUserHandler : HandlerInterceptorAdapter() {
         private val logger = LoggerFactory.getLogger(CurrentUserHandler::class.java)
     }
 
-    override fun preHandle(request: HttpServletRequest?, response: HttpServletResponse?, handler: Any?): Boolean {
+    override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
         if (handler is HandlerMethod) {
-            logger.debug("Caching Current User...")
-            SSOUtil.TL.set(SysUserUtil.getById(SSOUtil.getCurUserId(request!!)!!)!!)
+            val id = SSOUtil.getCurUserId(request)
+            val user = if (id == null) {
+                null
+            } else {
+                SysUserUtil.getById(id)
+            }
+            logger.debug("Caching Current User... ${user?.account}")
+            SSOUtil.TL.set(user)
         }
         return super.preHandle(request, response, handler)
     }

+ 6 - 2
zen-api/src/main/kotlin/com/gxzc/zen/umps/util/SSOUtil.kt

@@ -18,7 +18,11 @@ object SSOUtil {
     }
 
     fun getCurUserId(request: HttpServletRequest): Long? {
-        val token = SSOHelper.attrToken<SSOToken>(request) ?: SSOHelper.getToken(request)
+        val token = try {
+            SSOHelper.attrToken<SSOToken>(request) ?: SSOHelper.getToken(request)
+        } catch (e: Throwable) {
+            null
+        }
         return token?.uid?.toLong()
     }
 
@@ -34,7 +38,7 @@ object SSOUtil {
          */
         private val CURRENT_USER_TL = ThreadLocal<SysUser>()
 
-        fun set(entity: SysUser) {
+        fun set(entity: SysUser?) {
             CURRENT_USER_TL.set(entity)
         }