From 4aabb57ff9df47211f8963221adf7b3f23ca4e3e Mon Sep 17 00:00:00 2001 From: sysnix <535420543@qq.com> Date: Thu, 23 Nov 2023 05:21:34 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9A=84=E9=94=99=E8=AF=AF=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=80=80=E5=87=BA=E7=99=BB=E5=BD=95api=EF=BC=8C?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E8=AE=BE=E7=BD=AE=E9=A1=B6=E9=A1=B6domain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bootstrap/bootstrap.go | 2 +- conf/bootstrap.yaml | 4 ++-- .../controller/adm_controller/admin_controller.go | 9 +++------ .../controller/adm_controller/login_controller.go | 2 +- internal/controller/router.go | 12 ++++++++++++ 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 5bfcb52..bbbd569 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -19,7 +19,7 @@ type bootstrap struct { // ========================================================= type iris struct { - Session session `yaml:"iris"` + Session session `yaml:"session"` } type session struct { diff --git a/conf/bootstrap.yaml b/conf/bootstrap.yaml index d7b8664..aa14d83 100644 --- a/conf/bootstrap.yaml +++ b/conf/bootstrap.yaml @@ -3,7 +3,7 @@ iris: session: address: localhost:6379 db: 0 - prefix: iris:session + prefix: "iris:session:" expires: 10 database: sqlite: @@ -11,4 +11,4 @@ database: filePath: ./db_file.db redis: addr: localhost:6379 - db: 1 + db: 0 diff --git a/internal/controller/adm_controller/admin_controller.go b/internal/controller/adm_controller/admin_controller.go index aacf3c0..170fa26 100644 --- a/internal/controller/adm_controller/admin_controller.go +++ b/internal/controller/adm_controller/admin_controller.go @@ -1,13 +1,12 @@ package adm_controller import ( - "Blog/internal/model" "Blog/internal/model/result" + "Blog/internal/utils" "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/mvc" "github.com/kataras/iris/v12/sessions" - "github.com/mitchellh/mapstructure" "github.com/sirupsen/logrus" ) @@ -21,10 +20,8 @@ type AdminController struct { func (ctrl *AdminController) Get() { session := ctrl.Session - var userMap map[string]any - if err := session.Decode("user", &userMap); err == nil { - var user model.SysUser - mapstructure.Decode(userMap, &user) + user := utils.SessionUtil.GetUser(session) + if user.Id != "" { logrus.Info("[", user.Username, "]进入后台管理") } ctrl.Ctx.View("/admin/index.html") diff --git a/internal/controller/adm_controller/login_controller.go b/internal/controller/adm_controller/login_controller.go index d2ab7a9..cd49e1f 100644 --- a/internal/controller/adm_controller/login_controller.go +++ b/internal/controller/adm_controller/login_controller.go @@ -129,7 +129,7 @@ func (ctrl *LoginController) Post() { session.Man.Destroy(ctrl.Ctx) newSession := session.Man.Start(ctrl.Ctx) newSession.Set("user", user) - ctrl.Ctx.SetCookieKV("session_id_cookie", newSession.ID()) + ctrl.Ctx.SetCookieKV("session_id_cookie", newSession.ID(), iris.CookieAllowSubdomains("www")) xFrowardedFor := ctrl.Ctx.GetHeader("X-Forwarded-For") logrus.Info("用户:", user.Username, "登录,IP为:", xFrowardedFor) diff --git a/internal/controller/router.go b/internal/controller/router.go index c2931a7..8ad1890 100644 --- a/internal/controller/router.go +++ b/internal/controller/router.go @@ -5,6 +5,7 @@ import ( "Blog/internal/controller/adm_controller" "Blog/internal/controller/cli_controller" "Blog/internal/middleware" + "Blog/internal/utils" "net/http" "time" @@ -54,6 +55,17 @@ func Router() { m.Party("/diary").Handle(new(cli_controller.DiaryController)) m.Party("/file").Handle(new(cli_controller.FileController)) m.Party("/admin/login").Handle(new(adm_controller.LoginController)) + m.Router.Get("/logout", func(ctx iris.Context) { + session := sessions.Get(ctx) + user := utils.SessionUtil.GetUser(session) + if user.Id != "" { + logrus.Info("[", user.Username, "]退出登录,清除Session") + session.Man.Destroy(ctx) + ctx.Redirect("") + return + } + logrus.Error("当前用户没有登录状态,无法退出登录") + }) }) From c64f52ed195b327f3fef0f3375f16faebb0d2839 Mon Sep 17 00:00:00 2001 From: sysnix <535420543@qq.com> Date: Thu, 23 Nov 2023 05:38:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9C=AA=E5=8F=91=E5=B8=83=E5=92=8C?= =?UTF-8?q?=E4=B8=8D=E5=AD=98=E5=9C=A8=E7=9A=84=E6=96=87=E7=AB=A0=E8=B7=B3?= =?UTF-8?q?=E8=BD=AC=E8=87=B3404=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/controller/cli_controller/article_controller.go | 2 +- internal/repository/base_repository.go | 2 +- internal/service/article_service.go | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/internal/controller/cli_controller/article_controller.go b/internal/controller/cli_controller/article_controller.go index 7c96c12..98611ff 100644 --- a/internal/controller/cli_controller/article_controller.go +++ b/internal/controller/cli_controller/article_controller.go @@ -66,7 +66,7 @@ func (ctrl *ArticleController) GetLatest() { func (ctrl *ArticleController) ViewArticle() { articleId := ctrl.Ctx.Params().Get("id") article := service.ArticleService.GetArticle(articleId) - if article.Id == "" { + if article == nil || article.State != consts.ARTICLE_STATE_PUBLISH { ctrl.Ctx.View("404.html") return } diff --git a/internal/repository/base_repository.go b/internal/repository/base_repository.go index d758e84..d0efca6 100644 --- a/internal/repository/base_repository.go +++ b/internal/repository/base_repository.go @@ -28,7 +28,7 @@ func (repository *baseRep[T]) GetById(id string) (ret *T) { logrus.Debug("执行的SQL:", sql) err := repository.Table(repository.TableName).First(ret, "id = ?", id).Error if err != nil { - logrus.Info(err) + logrus.Error(err) return nil } diff --git a/internal/service/article_service.go b/internal/service/article_service.go index d1cc219..92ddd28 100644 --- a/internal/service/article_service.go +++ b/internal/service/article_service.go @@ -49,7 +49,9 @@ func (*articleService) GetArticle(id string) *model.BlogArticle { if err != nil { logrus.Infoln(id, "文章的缓存不存在,读取数据库") ret = repository.ArticleRepository.GetById(id) - client.RedisClient.Set(context.Background(), key, ret, time.Duration(0)) + if ret != nil { + client.RedisClient.Set(context.Background(), key, ret, time.Duration(0)) + } } return ret }