Skip to content

ServerFile不能正确获取SessionID() #184

@cognidim

Description

@cognidim

当ServerFile启用中间件功能时。中间件不能正确获取访问用户的SessionID()。

需求:SESSION控制ServerFile访问权限。

以下为测试代码。
APP级别中间件,输出SESSIONID。
访问/view路径时输出SESSIONID为空白。

2019-01-26 16:29:18

package main

import (
	"fmt"

	"github.com/devfeel/dotweb"
)

func main() {
	app := dotweb.New()
	// 中间件
	app.Use(NewSessionAuth())
	// 开启SESSION
	app.HttpServer.SetEnabledSession(true)
	// 开启ServerFile中间件支持
	app.HttpServer.SetEnabledStaticFileMiddleware(true)
	app.HttpServer.SetEnabledListDir(true)
	// 设置路由 输出字符串 Hello Dotweb
	app.HttpServer.GET("/", func(ctx dotweb.Context) error {
		method := ctx.Request().Method
		return ctx.WriteString("Hello Dotweb\n" + "Method:" + method)
	})
	// 单独设置中间件
	//app.HttpServer.ServerFile("/view/*", "D:/GoWork/GoTest/view").Use(NewSessionAuth())
	// 使用APP级别中间件
	app.HttpServer.ServerFile("/view/*", "D:/GoWork/GoTest/view")

	//开启服务 端口号
	fmt.Println("dotweb.StartServer => 8080")
	err := app.StartServer(8080)
	fmt.Println("dotweb.StartServer error => ", err)
}

// SessionAuth 结构体
type SessionAuth struct {
	dotweb.BaseMiddlware
}

// Handle 处理程序
func (m *SessionAuth) Handle(ctx dotweb.Context) error {
	fmt.Println(ctx.SessionID(), ctx.Request().RequestURI)
	return m.Next(ctx)
}

// NewSessionAuth New
func NewSessionAuth() *SessionAuth {
	sAuth := new(SessionAuth)
	return sAuth
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions