12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- /*
- * @description: session判断中间件
- * @Author: CP
- * @Date: 2020-08-28 14:17:23
- * @FilePath: \construction_management\web\middleware\sessions.go
- */
- package middleware
- import (
- "net/url"
- "strconv"
- "github.com/kataras/iris/v12"
- "go.mod/comm"
- "go.mod/conf"
- "go.mod/services"
- )
- func SessionsAuth(ctx iris.Context) {
- // 获得cookie
- cookie, err := ctx.Request().Cookie("cm")
- if err != nil {
- comm.Redirect(ctx.ResponseWriter(), "/login")
- }
- // 格式化
- params, err := url.ParseQuery(cookie.Value)
- if err != nil {
- comm.Redirect(ctx.ResponseWriter(), "/login")
- }
- // 解密用户标识
- identity, err := comm.AesDecrypt(params.Get("identity"), conf.CookieSecret)
- if err != nil {
- comm.Redirect(ctx.ResponseWriter(), "/login")
- }
- digitalToken := comm.CreateSign(conf.CookieSecret + identity)
- // 获得用户信息--TODO 存放redis
- npaSer := services.NewProjectAccountService()
- identityId, err := strconv.Atoi(identity)
- if err != nil {
- comm.Redirect(ctx.ResponseWriter(), "/login")
- }
- accountInfo := npaSer.Get(identityId)
- if accountInfo.Id == "0" {
- comm.Redirect(ctx.ResponseWriter(), "/login")
- }
- ctx.Values().Set("accountId", identity)
- projectId, err := comm.AesDecrypt(accountInfo.ProjectId, conf.SignSecret)
- if err != nil {
- comm.Redirect(ctx.ResponseWriter(), "/login")
- }
- ctx.Values().Set("projectId", projectId)
- // 设置viewData
- ctx.ViewData("Account", accountInfo)
- // npaDao := dao.NewProjectAccountDao(datasource.InstanceDbMaster())
- // identityId, err := strconv.Atoi(identity)
- // if err != nil {
- // comm.Redirect(ctx.ResponseWriter(), "/login")
- // }
- // accountInfo := npaDao.Get(identityId)
- // if accountInfo.Id == 0 {
- // comm.Redirect(ctx.ResponseWriter(), "/login")
- // }
- // ctx.Values().Set("accountId", identity)
- // 设置viewData
- // accountView := viewmodels.ProjectAccount{}
- // accountView.Account = accountInfo.Account
- // accountView.Name = accountInfo.Name
- // accountView.Company = accountInfo.Company
- // accountView.Role = accountInfo.Role
- // accountView.Mobile = accountInfo.Mobile
- // accountView.Telephone = accountInfo.Telephone
- // ctx.ViewData("Account", accountView)
- // 比对数字证书
- if digitalToken != params.Get("digitalToken") {
- comm.Redirect(ctx.ResponseWriter(), "/login")
- }
- // TODO 分布式session
- //通过后执行下一步
- ctx.Next()
- }
|