SSOUtil.kt 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package com.gxzc.zen.umps.util
  2. import com.baomidou.kisso.SSOHelper
  3. import com.baomidou.kisso.SSOToken
  4. import com.gxzc.zen.api.sys.model.SysUser
  5. import com.gxzc.zen.common.util.HttpUtil
  6. import javax.servlet.http.HttpServletRequest
  7. /**
  8. * SSO 工具类
  9. * @author NorthLan
  10. * @date 2018/3/15
  11. * @url https://noahlan.com
  12. */
  13. object SSOUtil {
  14. /**
  15. * 从token中获取当前用户id
  16. */
  17. fun getCurUserIdByToken(): Long? {
  18. return getCurUserIdByToken(HttpUtil.getRequest())
  19. }
  20. /**
  21. * 从token中获取当前用户id
  22. */
  23. fun getCurUserIdByToken(request: HttpServletRequest): Long? {
  24. val token = try {
  25. SSOHelper.attrToken<SSOToken>(request) ?: SSOHelper.getToken(request)
  26. } catch (e: Throwable) {
  27. null
  28. }
  29. return token?.uid?.toLong()
  30. }
  31. /**
  32. * 获取当前用户
  33. */
  34. fun getCurUser(): SysUser? {
  35. return TL.get()
  36. }
  37. /**
  38. * 获取当前用户id
  39. */
  40. fun getCurUserId(): Long? {
  41. return getCurUser()?.id
  42. }
  43. object TL {
  44. /**
  45. * 180324
  46. * 于MetaObjectHandler执行前 每个Controller方法拦截 暂存当前用户引用
  47. */
  48. private val CURRENT_USER_TL = ThreadLocal<SysUser>()
  49. fun set(entity: SysUser?) {
  50. CURRENT_USER_TL.set(entity)
  51. }
  52. fun clear() {
  53. CURRENT_USER_TL.remove()
  54. }
  55. fun get(): SysUser? {
  56. return CURRENT_USER_TL.get()
  57. }
  58. }
  59. }