You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.1 KiB
46 lines
1.1 KiB
package CaptchaController
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"github.com/dchest/captcha"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func Serve(w http.ResponseWriter, r *http.Request, id string, width, height int) error {
|
|
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
w.Header().Set("Pragma", "no-cache")
|
|
w.Header().Set("Expires", "0")
|
|
var content bytes.Buffer
|
|
w.Header().Set("Content-Type", "image/png")
|
|
//设置到Redis
|
|
captcha.SetCustomStore(&RedisStoreBean)
|
|
captcha.WriteImage(&content, id, width, height)
|
|
if content.Bytes()==nil{
|
|
return captcha.ErrNotFound
|
|
}else {
|
|
http.ServeContent(w, r, id+".png", time.Time{}, bytes.NewReader(content.Bytes()))
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func ServeHTTP(w http.ResponseWriter, r *http.Request){
|
|
//GET方式传参
|
|
keys, ok := r.URL.Query()["captchaId"]
|
|
if !ok || len(keys) < 1 {
|
|
log.Println("Url Param 'key' is missing")
|
|
return
|
|
}
|
|
id := keys[0]
|
|
|
|
fmt.Println("reload : " + r.FormValue("reload"))
|
|
if r.FormValue("reload") != "" {
|
|
captcha.Reload(id)
|
|
}
|
|
if Serve(w, r, id, captcha.StdWidth, captcha.StdHeight) == captcha.ErrNotFound {
|
|
http.NotFound(w, r)
|
|
}
|
|
}
|