Go Pentester - HTTP Servers(3)
Building Middleware with Negroni
Reasons use middleware, including logging requests, authenticating and authorizing users, and mapping resources.
Idiomatic HTTP Middleware for Golang. https://github.com/urfave/negroni
Install the negroni package.
- go get github.com/urfave/negroni
PS: How to solve the go get can not work in China. Following is the best solution so far. https://github.com/goproxy/goproxy.cn
- $ go env -w GO111MODULE=on
- $ go env -w GOPROXY=https://goproxy.cn,direct
Negroni example
- package main
- import (
- "github.com/gorilla/mux"
- "github.com/urfave/negroni"
- "net/http"
- )
- func main() {
- r := mux.NewRouter()
- n := negroni.Classic()
- n.UseHandler(r)
- http.ListenAndServe(":8000",n)
- }
Build and execute this program.
Create trivial middleware that prints a message and passes execution to the next middleware in the chain:
- package main
- import (
- "fmt"
- "github.com/gorilla/mux"
- "github.com/urfave/negroni"
- "net/http"
- )
- type trivial struct {
- }
- func (t *trivial) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
- fmt.Println("Executing trivial middleware")
- next(w, r)
- }
- func main() {
- r := mux.NewRouter()
- n := negroni.Classic()
- n.UseHandler(r)
- n.Use(&trivial{})
- http.ListenAndServe(":8000",n)
- }
Build and test this new program.
Adding Authentication with Negroni
Use of context, which can easily pass variables between functions.
- package main
- import (
- "context"
- "fmt"
- "net/http"
- "github.com/gorilla/mux"
- "github.com/urfave/negroni"
- )
- type badAuth struct {
- Username string
- Password string
- }
- func (b *badAuth) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
- username := r.URL.Query().Get("username")
- password := r.URL.Query().Get("password")
- if username != b.Username && password !=b.Password {
- http.Error(w, "Unauthorized", 401)
- return
- }
- ctx := context.WithValue(r.Context(), "username", username)
- r = r.WithContext(ctx)
- next(w, r)
- }
- func hello(w http.ResponseWriter, r * http.Request) {
- username := r.Context().Value("username").(string)
- fmt.Fprintf(w, "Hi %s\n", username)
- }
- func main() {
- r := mux.NewRouter()
- r.HandleFunc("/hello",hello).Methods("GET")
- n := negroni.Classic()
- n.Use(&badAuth{
- Username: "admin",
- Password: "password",
- })
- n.UseHandler(r)
- http.ListenAndServe(":8000", n)
- }
Build and excute this program. Then test it by sending a few requests to the server.
- curl -i http://localhost:8000/hello
- curl -i 'http://localhost:8000/hello?username=admin&password=password'
Logs on the server-side.
Go Pentester - HTTP Servers(3)的更多相关文章
- Go Pentester - HTTP Servers(2)
Routing with the gorilla/mux Package A powerful HTTP router and URL matcher for building Go web serv ...
- Go Pentester - HTTP Servers(1)
HTTP Server Basics Use net/http package and useful third-party packages by building simple servers. ...
- Coping with the TCP TIME-WAIT state on busy Linux servers
Coping with the TCP TIME-WAIT state on busy Linux servers 文章源自于:https://vincent.bernat.im/en/blog/20 ...
- How To Restart timer service on all servers in farm
[array]$servers= Get-SPServer | ? {$_.Role -eq "Application"} $farm = Get-SPFarm foreach ( ...
- eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Sertomcat
eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Serto ...
- coderforces #387 Servers(模拟)
Servers time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- Servers
Servers¶ Server interface. class novaclient.v1_1.servers.Server(manager, info, loaded=False) Bases: ...
- 使用servers 启动项目时 ,一直处于启动中, 最后出现无法的问题。
使用eclipse 中的servers 配置了一个server 来启动项目, 发现无法启动 排除法: 去掉项目配置,单独启动该server ,发现可以启动, 说明是项目出现问题 但是项目并没有报错, ...
- servers中添加server时,看不到运行环境的选择。
servers中添加server时,看不到运行环境的选择. 主要原因是tomcat目录中的配置文件格式不对.
随机推荐
- php 判断设备是手机还是平板还是pc
1 <?php 2 //获取USER AGENT 3 $agent = strtolower($_SERVER['HTTP_USER_AGENT']); 4 5 //分析数据 6 $is_pc ...
- php读取富文本处理html标签问题
thinkphp的一项配置会将富文本编辑器的内容中的html标签进行转义处理 'DEFAULT_FILTER' => 'htmlspecialchars', // 默认参数过滤方法使用htmls ...
- 一个老牌程序员说:做Java开发,怎么可以不会这 20 种类库和 API
- c++虚函数和虚继承
关键字virtual用于父类方法,如果传了一个子类对象,并且子类重写了父类的这个virtual方法,就会调用子类的方法.传谁就调用谁,这个就是多态.#include<iostream> u ...
- Linux系统结构详解(转)
Linux系统一般有4个主要部分: 内核.shell.文件系统和应用程序.内核.shell和文件系统一起形成了基本的操作系统结构,它们使得用户可以运行程序.管理文件并使用系统.部分层次结构如图1-1所 ...
- springMvc接口开发--对访问的restful api接口进行拦截实现功能扩展
1.视频参加Spring Security开发安全的REST服务\PART1\PART1 3-7 使用切片拦截REST服务三通it学院-www.santongit.com-.mp4 讲的比较的经典,后 ...
- Plugns
Lombok Translation Rainbow Brackets
- 一场由yield引发的连串拷问
最近在学习Python中生成器时,遇到了一个yield关键词,廖雪峰老师的官网中也没有详细的解释,经过一番查阅和研究,终于对它有了一些认识并做了总结(如有不对之处,还请大神指正). 首先先简单了解下生 ...
- 入门大数据---Flink开发环境搭建
一.安装 Scala 插件 Flink 分别提供了基于 Java 语言和 Scala 语言的 API ,如果想要使用 Scala 语言来开发 Flink 程序,可以通过在 IDEA 中安装 Scala ...
- MongoDB入门四
MongoDB针对实时位置 db.CallRecordInfo.find().count()db.SendInfo.find().count()db.RiderReaTimePositon.find( ...