Explorar el Código

优化SSOUtil 以及CurrentUserHandler保持器npe

NorthLan hace 7 años
padre
commit
48a218e6a5

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

@@ -23,7 +23,7 @@ class CurrentUserHandler : HandlerInterceptorAdapter() {
 
     override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
         if (handler is HandlerMethod) {
-            val id = SSOUtil.getCurUserId(request)
+            val id = SSOUtil.getCurUserIdByToken(request)
             val user = if (id == null) {
                 null
             } else {

+ 20 - 4
zen-api/src/main/kotlin/com/gxzc/zen/umps/util/SSOUtil.kt

@@ -13,11 +13,17 @@ import javax.servlet.http.HttpServletRequest
  * @url https://noahlan.com
  */
 object SSOUtil {
-    fun getCurUserId(): Long? {
-        return getCurUserId(HttpUtil.getRequest())
+    /**
+     * 从token中获取当前用户id
+     */
+    fun getCurUserIdByToken(): Long? {
+        return getCurUserIdByToken(HttpUtil.getRequest())
     }
 
-    fun getCurUserId(request: HttpServletRequest): Long? {
+    /**
+     * 从token中获取当前用户id
+     */
+    fun getCurUserIdByToken(request: HttpServletRequest): Long? {
         val token = try {
             SSOHelper.attrToken<SSOToken>(request) ?: SSOHelper.getToken(request)
         } catch (e: Throwable) {
@@ -26,10 +32,20 @@ object SSOUtil {
         return token?.uid?.toLong()
     }
 
+    /**
+     * 获取当前用户
+     */
     fun getCurUser(): SysUser? {
         return TL.get()
     }
 
+    /**
+     * 获取当前用户id
+     */
+    fun getCurUserId(): Long? {
+        return getCurUser()?.id
+    }
+
 
     object TL {
         /**
@@ -46,7 +62,7 @@ object SSOUtil {
             CURRENT_USER_TL.remove()
         }
 
-        fun get(): SysUser {
+        fun get(): SysUser? {
             return CURRENT_USER_TL.get()
         }
     }