SSOUtil.kt 1.5 KB

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