1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- 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.common.util.HttpUtil
- import javax.servlet.http.HttpServletRequest
- /**
- * SSO 工具类
- * @author NorthLan
- * @date 2018/3/15
- * @url https://noahlan.com
- */
- object SSOUtil {
- /**
- * 从token中获取当前用户id
- */
- fun getCurUserIdByToken(): Long? {
- return getCurUserIdByToken(HttpUtil.getRequest())
- }
- /**
- * 从token中获取当前用户id
- */
- fun getCurUserIdByToken(request: HttpServletRequest): Long? {
- val token = try {
- SSOHelper.attrToken<SSOToken>(request) ?: SSOHelper.getToken(request)
- } catch (e: Throwable) {
- null
- }
- return token?.uid?.toLong()
- }
- /**
- * 获取当前用户
- */
- fun getCurUser(): SysUser? {
- return TL.get()
- }
- /**
- * 获取当前用户id
- */
- fun getCurUserId(): Long? {
- return getCurUser()?.id
- }
- 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()
- }
- }
- }
|