Merge pull request '未发布和不存在的文章跳转至404页面,修改配置文件的错误,增加退出登录api,登录设置顶顶domain' (#2) from sysnix into master

Reviewed-on: http://localhost:3000/sysnix/blog/pulls/2
This commit is contained in:
sysnix 2023-11-23 05:40:04 +08:00
commit 88d4039ff7
8 changed files with 24 additions and 13 deletions

View File

@ -19,7 +19,7 @@ type bootstrap struct {
// =========================================================
type iris struct {
Session session `yaml:"iris"`
Session session `yaml:"session"`
}
type session struct {

View File

@ -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

View File

@ -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")

View File

@ -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)

View File

@ -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
}

View File

@ -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("当前用户没有登录状态,无法退出登录")
})
})

View File

@ -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
}

View File

@ -49,8 +49,10 @@ func (*articleService) GetArticle(id string) *model.BlogArticle {
if err != nil {
logrus.Infoln(id, "文章的缓存不存在,读取数据库")
ret = repository.ArticleRepository.GetById(id)
if ret != nil {
client.RedisClient.Set(context.Background(), key, ret, time.Duration(0))
}
}
return ret
}