未发布和不存在的文章跳转至404页面,修改配置文件的错误,增加退出登录api,登录设置顶顶domain #2

Merged
sysnix merged 2 commits from sysnix into master 2023-11-23 05:40:05 +08:00
8 changed files with 24 additions and 13 deletions

View File

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

View File

@ -3,7 +3,7 @@ iris:
session: session:
address: localhost:6379 address: localhost:6379
db: 0 db: 0
prefix: iris:session prefix: "iris:session:"
expires: 10 expires: 10
database: database:
sqlite: sqlite:
@ -11,4 +11,4 @@ database:
filePath: ./db_file.db filePath: ./db_file.db
redis: redis:
addr: localhost:6379 addr: localhost:6379
db: 1 db: 0

View File

@ -1,13 +1,12 @@
package adm_controller package adm_controller
import ( import (
"Blog/internal/model"
"Blog/internal/model/result" "Blog/internal/model/result"
"Blog/internal/utils"
"github.com/kataras/iris/v12" "github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc" "github.com/kataras/iris/v12/mvc"
"github.com/kataras/iris/v12/sessions" "github.com/kataras/iris/v12/sessions"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -21,10 +20,8 @@ type AdminController struct {
func (ctrl *AdminController) Get() { func (ctrl *AdminController) Get() {
session := ctrl.Session session := ctrl.Session
var userMap map[string]any user := utils.SessionUtil.GetUser(session)
if err := session.Decode("user", &userMap); err == nil { if user.Id != "" {
var user model.SysUser
mapstructure.Decode(userMap, &user)
logrus.Info("[", user.Username, "]进入后台管理") logrus.Info("[", user.Username, "]进入后台管理")
} }
ctrl.Ctx.View("/admin/index.html") ctrl.Ctx.View("/admin/index.html")

View File

@ -129,7 +129,7 @@ func (ctrl *LoginController) Post() {
session.Man.Destroy(ctrl.Ctx) session.Man.Destroy(ctrl.Ctx)
newSession := session.Man.Start(ctrl.Ctx) newSession := session.Man.Start(ctrl.Ctx)
newSession.Set("user", user) 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") xFrowardedFor := ctrl.Ctx.GetHeader("X-Forwarded-For")
logrus.Info("用户:", user.Username, "登录,IP为:", xFrowardedFor) logrus.Info("用户:", user.Username, "登录,IP为:", xFrowardedFor)

View File

@ -66,7 +66,7 @@ func (ctrl *ArticleController) GetLatest() {
func (ctrl *ArticleController) ViewArticle() { func (ctrl *ArticleController) ViewArticle() {
articleId := ctrl.Ctx.Params().Get("id") articleId := ctrl.Ctx.Params().Get("id")
article := service.ArticleService.GetArticle(articleId) article := service.ArticleService.GetArticle(articleId)
if article.Id == "" { if article == nil || article.State != consts.ARTICLE_STATE_PUBLISH {
ctrl.Ctx.View("404.html") ctrl.Ctx.View("404.html")
return return
} }

View File

@ -5,6 +5,7 @@ import (
"Blog/internal/controller/adm_controller" "Blog/internal/controller/adm_controller"
"Blog/internal/controller/cli_controller" "Blog/internal/controller/cli_controller"
"Blog/internal/middleware" "Blog/internal/middleware"
"Blog/internal/utils"
"net/http" "net/http"
"time" "time"
@ -54,6 +55,17 @@ func Router() {
m.Party("/diary").Handle(new(cli_controller.DiaryController)) m.Party("/diary").Handle(new(cli_controller.DiaryController))
m.Party("/file").Handle(new(cli_controller.FileController)) m.Party("/file").Handle(new(cli_controller.FileController))
m.Party("/admin/login").Handle(new(adm_controller.LoginController)) 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) logrus.Debug("执行的SQL:", sql)
err := repository.Table(repository.TableName).First(ret, "id = ?", id).Error err := repository.Table(repository.TableName).First(ret, "id = ?", id).Error
if err != nil { if err != nil {
logrus.Info(err) logrus.Error(err)
return nil return nil
} }

View File

@ -49,7 +49,9 @@ func (*articleService) GetArticle(id string) *model.BlogArticle {
if err != nil { if err != nil {
logrus.Infoln(id, "文章的缓存不存在,读取数据库") logrus.Infoln(id, "文章的缓存不存在,读取数据库")
ret = repository.ArticleRepository.GetById(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 return ret
} }