sessions.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * @description: session判断中间件
  3. * @Author: CP
  4. * @Date: 2020-08-28 14:17:23
  5. * @FilePath: \construction_management\web\middleware\sessions.go
  6. */
  7. package middleware
  8. import (
  9. "net/url"
  10. "strconv"
  11. "github.com/kataras/iris/v12"
  12. "go.mod/comm"
  13. "go.mod/conf"
  14. "go.mod/services"
  15. )
  16. func SessionsAuth(ctx iris.Context) {
  17. // 获得cookie
  18. cookie, err := ctx.Request().Cookie("cm")
  19. if err != nil {
  20. comm.Redirect(ctx.ResponseWriter(), "/login")
  21. }
  22. // 格式化
  23. params, err := url.ParseQuery(cookie.Value)
  24. if err != nil {
  25. comm.Redirect(ctx.ResponseWriter(), "/login")
  26. }
  27. // 解密用户标识
  28. identity, err := comm.AesDecrypt(params.Get("identity"), conf.CookieSecret)
  29. if err != nil {
  30. comm.Redirect(ctx.ResponseWriter(), "/login")
  31. }
  32. digitalToken := comm.CreateSign(conf.CookieSecret + identity)
  33. // 获得用户信息--TODO 存放redis
  34. npaSer := services.NewProjectAccountService()
  35. identityId, err := strconv.Atoi(identity)
  36. if err != nil {
  37. comm.Redirect(ctx.ResponseWriter(), "/login")
  38. }
  39. accountInfo := npaSer.Get(identityId)
  40. if accountInfo.Id == "0" {
  41. comm.Redirect(ctx.ResponseWriter(), "/login")
  42. }
  43. ctx.Values().Set("accountId", identity)
  44. projectId, err := comm.AesDecrypt(accountInfo.ProjectId, conf.SignSecret)
  45. if err != nil {
  46. comm.Redirect(ctx.ResponseWriter(), "/login")
  47. }
  48. ctx.Values().Set("projectId", projectId)
  49. // 设置viewData
  50. ctx.ViewData("Account", accountInfo)
  51. // npaDao := dao.NewProjectAccountDao(datasource.InstanceDbMaster())
  52. // identityId, err := strconv.Atoi(identity)
  53. // if err != nil {
  54. // comm.Redirect(ctx.ResponseWriter(), "/login")
  55. // }
  56. // accountInfo := npaDao.Get(identityId)
  57. // if accountInfo.Id == 0 {
  58. // comm.Redirect(ctx.ResponseWriter(), "/login")
  59. // }
  60. // ctx.Values().Set("accountId", identity)
  61. // 设置viewData
  62. // accountView := viewmodels.ProjectAccount{}
  63. // accountView.Account = accountInfo.Account
  64. // accountView.Name = accountInfo.Name
  65. // accountView.Company = accountInfo.Company
  66. // accountView.Role = accountInfo.Role
  67. // accountView.Mobile = accountInfo.Mobile
  68. // accountView.Telephone = accountInfo.Telephone
  69. // ctx.ViewData("Account", accountView)
  70. // 比对数字证书
  71. if digitalToken != params.Get("digitalToken") {
  72. comm.Redirect(ctx.ResponseWriter(), "/login")
  73. }
  74. // TODO 分布式session
  75. //通过后执行下一步
  76. ctx.Next()
  77. }