码迷,mamicode.com
首页 > Web开发 > 详细

9.6 http中间件

时间:2018-03-27 01:47:05      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:user   use   中间件   post   方式   cat   利用   turn   news   

package main

import (
    "io"
    "log"
    "net/http"
)

type User string

func (u User) toString() string {
    return string(u)
}

type AuthHandler func(u User, w http.ResponseWriter, r *http.Request)

func main() {

    // Secured API
    mux := http.NewServeMux()
    mux.HandleFunc("/api/users", Secure(func(w http.ResponseWriter, r *http.Request) {
        io.WriteString(w, `[{"id":"1","login":"ffghi"},{"id":"2","login":"ffghj"}]`)
    }))
    mux.HandleFunc("/api/profile", WithUser(func(u User, w http.ResponseWriter, r *http.Request) {
        log.Println(u.toString())
        io.WriteString(w, "{\"user\":\""+u.toString()+"\"}")
    }))

    http.ListenAndServe(":8080", mux)

}

func WithUser(h AuthHandler) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        user := r.Header.Get("X-User")
        if len(user) == 0 {
            w.WriteHeader(http.StatusUnauthorized)
            return
        }
        h(User(user), w, r)
    }

}

func Secure(h http.HandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        sec := r.Header.Get("X-Auth")
        if sec != "authenticated" {
            w.WriteHeader(http.StatusUnauthorized)
            return
        }
        h(w, r) // use the handler
    }

}

/*
(sx3.5.3) ?  ~ curl -X GET -I http://127.0.0.1:8080/api/users
HTTP/1.1 401 Unauthorized
Date: Mon, 26 Mar 2018 15:42:50 GMT
Content-Length: 0
Content-Type: text/plain; charset=utf-8



curl -X GET -w "\nStatus: %{http_code}\n"  http://127.0.0.1:8080/api/users
Status: 401


curl -X GET -w "\nStatus: %{http_code}\n"  http://127.0.0.1:8080/api/users  -H "X-Auth: authenticated"
[{"id":"1","login":"ffghi"},{"id":"2","login":"ffghj"}]
Status: 200

(sx3.5.3) ?  ~ curl -X GET -w "\nStatus: %{http_code}\n"  http://127.0.0.1:8080/api/profile  -H "X-User: zrd"
{"user":"zrd"}
Status: 200
*/

在前面的例子中中间件的实现利用函数为一等公民的Golang特色。原handlerfunc包装成一个handlerfunc检查x-auth头。安全功能是用来保护handlerfunc,用于servemux的handlefunc方法。

请注意,这只是一个简单的示例,但这种方式可以实现更复杂的解决方案。例如,用户的身份可以从标题标记和随后的提取,处理新类型可以定义为类AuthHandler func(U *用户,W http.responsewriter,R * HTTP请求)。功能的用户创建的servemux的handlerfunc。

9.6 http中间件

标签:user   use   中间件   post   方式   cat   利用   turn   news   

原文地址:https://www.cnblogs.com/zrdpy/p/8654939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!