parent
79b5d4b8bd
commit
1ba5cde076
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,23 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func CorsHandler() gin.HandlerFunc {
|
||||
return func(context *gin.Context) {
|
||||
context.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
context.Header("Access-Control-Allow-Methods", "*")
|
||||
context.Header("Access-Control-Allow-Headers", "*")
|
||||
context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar") // 跨域关键设置 让浏览器可以解析
|
||||
context.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒
|
||||
context.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true
|
||||
context.Header("content-type", "application/json") // 设置返回格式是json
|
||||
//Release all option pre-requests
|
||||
if context.Request.Method == http.MethodOptions {
|
||||
context.JSON(http.StatusOK, "Options Request!")
|
||||
}
|
||||
context.Next()
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/common/token_verify"
|
||||
"Open_IM/pkg/utils"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func JWTAuth() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
ok, userID, errInfo := token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), "")
|
||||
// log.NewInfo("0", utils.GetSelfFuncName(), "userID: ", userID)
|
||||
c.Set("userID", userID)
|
||||
if !ok {
|
||||
log.NewError("", "GetUserIDFromToken false ", c.Request.Header.Get("token"))
|
||||
c.Abort()
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": 400, "errMsg": errInfo})
|
||||
return
|
||||
} else {
|
||||
if !utils.IsContain(userID, config.Config.Manager.AppManagerUid) {
|
||||
c.Abort()
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": 400, "errMsg": "user is not admin"})
|
||||
return
|
||||
}
|
||||
log.NewInfo("0", utils.GetSelfFuncName(), "failed: ", errInfo)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func CorsHandler() gin.HandlerFunc {
|
||||
return func(context *gin.Context) {
|
||||
context.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
context.Header("Access-Control-Allow-Methods", "*")
|
||||
context.Header("Access-Control-Allow-Headers", "*")
|
||||
context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar") // 跨域关键设置 让浏览器可以解析
|
||||
context.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒
|
||||
context.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true
|
||||
context.Header("content-type", "application/json") // 设置返回格式是json
|
||||
//Release all option pre-requests
|
||||
if context.Request.Method == http.MethodOptions {
|
||||
context.JSON(http.StatusOK, "Options Request!")
|
||||
}
|
||||
context.Next()
|
||||
}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gin.SetMode(gin.TestMode)
|
||||
}
|
||||
|
||||
func performRequest(r http.Handler, method, origin string) *httptest.ResponseRecorder {
|
||||
return performRequestWithHeaders(r, method, origin, http.Header{})
|
||||
}
|
||||
|
||||
func performRequestWithHeaders(r http.Handler, method, origin string, header http.Header) *httptest.ResponseRecorder {
|
||||
req, _ := http.NewRequest(method, "/", nil)
|
||||
// From go/net/http/request.go:
|
||||
// For incoming requests, the Host header is promoted to the
|
||||
// Request.Host field and removed from the Header map.
|
||||
req.Host = header.Get("Host")
|
||||
header.Del("Host")
|
||||
if len(origin) > 0 {
|
||||
header.Set("Origin", origin)
|
||||
}
|
||||
req.Header = header
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
return w
|
||||
}
|
||||
|
||||
func newTestRouter() *gin.Engine {
|
||||
router := gin.New()
|
||||
router.Use(CorsHandler())
|
||||
router.GET("/", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "get")
|
||||
})
|
||||
router.POST("/", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "post")
|
||||
})
|
||||
router.PATCH("/", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "patch")
|
||||
})
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
func Test_CorsHandler(t *testing.T) {
|
||||
router := newTestRouter()
|
||||
// no CORS request, origin == ""
|
||||
w := performRequest(router, "GET", "")
|
||||
assert.Equal(t, "get", w.Body.String())
|
||||
assert.Equal(t, w.Header().Get("Access-Control-Allow-Origin"), "*")
|
||||
assert.Equal(t, w.Header().Get("Access-Control-Allow-Methods"), "*")
|
||||
assert.Equal(t, w.Header().Get("Access-Control-Allow-Headers"), "*")
|
||||
assert.Equal(t, w.Header().Get("Access-Control-Expose-Headers"), "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar")
|
||||
assert.Equal(t, w.Header().Get("Access-Control-Max-Age"), "172800")
|
||||
assert.Equal(t, w.Header().Get("Access-Control-Allow-Credentials"), "false")
|
||||
assert.Equal(t, w.Header().Get("content-type"), "application/json")
|
||||
|
||||
w = performRequest(router, "OPTIONS", "")
|
||||
assert.Equal(t, w.Body.String(), "\"Options Request!\"")
|
||||
}
|
Loading…
Reference in new issue