golang(beego)验证码控制器

golang(beego)验证码控制器,第1张

概述核心代码是封装的别人的 代码如下: /** * 用法 *1)注册一下路由 * beego.Router("/yzm", &controllers.YzmController{}, "Get:GetYzm") //验证码控制器 * beego.Router("/yzm/judgeyzm", &controllers.YzmController{}, "Post:Ju

核心代码是封装的别人的

代码如下:

/** * 用法 *1)注册一下路由 *  beego.Router("/yzm",&controllers.YzmController{},"Get:GetYzm")             //验证码控制器 *	beego.Router("/yzm/judgeyzm","Post:JudgeYzm") //验证码AJAX验证 * * 2)用法 *  <img src="/yzm" onclick="this.src='/yzm?'+Math.random()" > */package controllersimport (	crand "crypto/rand"	// "fmt"	"github.com/astaxIE/beego"	"github.com/astaxIE/beego/session"	"image"	"image/color"	"image/png"	"io"	"math/rand"	"net/http"	"strconv"	"time")var globalSessions *session.Managerfunc init() {	globalSessions,_ = session.NewManager("memory",`{"cookiename":"gosessionID","enablesetcookie,omitempty": true,"gclifetime":3600,"maxlifetime": 3600,"secure": false,"sessionIDHashFunc": "sha1","sessionIDHashKey": "","cookielifeTime": 3600,"provIDerConfig": ""}`)	go globalSessions.GC()}type YzmController struct {	beego.Controller}/** * 获取验证码 */func (this *YzmController) GetYzm() {	pic(this.Ctx.ResponseWriter,this.Ctx.Request)}/** * AJAX判断验证码 */func (this *YzmController) JudgeYzm() {	p := this.GetString("param")	rs := "对不起,输入" + p + "为错误!"	sess := globalSessions.SessionStart(this.Ctx.ResponseWriter,this.Ctx.Request)	if sess.Get("yzm") == p {		rs = "y" //返回y表示正确	}	this.Ctx.WriteString(rs)}const (	stDWIDth  = 100	stdHeight = 40	maxSkew   = 2)const (	FontWIDth  = 5	FontHeight = 8	blackChar  = 1)var Font = [][]byte{	{ // 0		0,1,},{ // 1		0,{ // 2		0,{ // 3		1,{ // 4		1,{ // 5		1,{ // 6		0,{ // 7		1,{ // 8		0,{ // 9		0,}type Image struct {	*image.NRGBA	color   *color.NRGBA	wIDth   int //a digit wIDth	height  int //a digit height	dotsize int}func init() {	rand.Seed(int64(time.Second))}func NewImage(digits []byte,wIDth,height int) *Image {	img := new(Image)	r := image.Rect(img.wIDth,img.height,stDWIDth,stdHeight)	img.NRGBA = image.NewNRGBA(r)	img.color = &color.NRGBA{		uint8(rand.Intn(129)),uint8(rand.Intn(129)),0xFF,}	// Draw background (10 random circles of random brightness)	img.calculateSizes(wIDth,height,len(digits))	img.fillWithCircles(10,img.dotsize)	maxx := wIDth - (img.wIDth+img.dotsize)*len(digits) - img.dotsize	maxy := height - img.height - img.dotsize*2	x := rnd(img.dotsize*2,maxx)	y := rnd(img.dotsize*2,maxy)	// Draw digits.	for _,n := range digits {		img.drawDigit(Font[n],x,y)		x += img.wIDth + img.dotsize	}	// Draw strike-through line.	img.strikeThrough()	return img}func (img *Image) Writeto(w io.Writer) (int64,error) {	return 0,png.Encode(w,img)}func (img *Image) calculateSizes(wIDth,ncount int) {	// Goal: fit all digits insIDe the image.	var border int	if wIDth > height {		border = height / 5	} else {		border = wIDth / 5	}	// Convert everything to floats for calculations.	w := float64(wIDth - border*2)  //268	h := float64(height - border*2) //48	// fw takes into account 1-dot spacing between digits.	fw := float64(FontWIDth) + 1 //6	fh := float64(FontHeight) //8	nc := float64(ncount)     //7	// Calculate the wIDth of a single digit taking into account only the	// wIDth of the image.	nw := w / nc //38	// Calculate the height of a digit from this wIDth.	nh := nw * fh / fw //51	// Digit too high?	if nh > h {		// Fit digits based on height.		nh = h //nh = 44		nw = fw / fh * nh	}	// Calculate dot size.	img.dotsize = int(nh / fh)	// Save everything,making the actual wIDth smaller by 1 dot to account	// for spacing between digits.	img.wIDth = int(nw)	img.height = int(nh) - img.dotsize}func (img *Image) fillWithCircles(n,maxradius int) {	color := img.color	maxx := img.Bounds().Max.X	maxy := img.Bounds().Max.Y	for i := 0; i < n; i++ {		setRandomBrightness(color,255)		r := rnd(1,maxradius)		img.drawCircle(color,rnd(r,maxx-r),maxy-r),r)	}}func (img *Image) drawHorizline(color color.color,fromX,toX,y int) {	for x := fromX; x <= toX; x++ {		img.Set(x,y,color)	}}func (img *Image) drawCircle(color color.color,radius int) {	f := 1 - radius	dfx := 1	dfy := -2 * radius	xx := 0	yy := radius	img.Set(x,y+radius,color)	img.Set(x,y-radius,color)	img.drawHorizline(color,x-radius,x+radius,y)	for xx < yy {		if f >= 0 {			yy--			dfy += 2			f += dfy		}		xx++		dfx += 2		f += dfx		img.drawHorizline(color,x-xx,x+xx,y+yy)		img.drawHorizline(color,y-yy)		img.drawHorizline(color,x-yy,x+yy,y+xx)		img.drawHorizline(color,y-xx)	}}func (img *Image) strikeThrough() {	r := 0	maxx := img.Bounds().Max.X	maxy := img.Bounds().Max.Y	y := rnd(maxy/3,maxy-maxy/3)	for x := 0; x < maxx; x += r {		r = rnd(1,img.dotsize/3)		y += rnd(-img.dotsize/2,img.dotsize/2)		if y <= 0 || y >= maxy {			y = rnd(maxy/3,maxy-maxy/3)		}		img.drawCircle(img.color,r)	}}func (img *Image) drawDigit(digit []byte,y int) {	skf := rand.float64() * float64(rnd(-maxSkew,maxSkew))	xs := float64(x)	minr := img.dotsize / 2               // minumum radius	maxr := img.dotsize/2 + img.dotsize/4 // maximum radius	y += rnd(-minr,minr)	for yy := 0; yy < FontHeight; yy++ {		for xx := 0; xx < FontWIDth; xx++ {			if digit[yy*FontWIDth+xx] != blackChar {				continue			}			// Introduce random variations.			or := rnd(minr,maxr)			ox := x + (xx * img.dotsize) + rnd(0,or/2)			oy := y + (yy * img.dotsize) + rnd(0,or/2)			img.drawCircle(img.color,ox,oy,or)		}		xs += skf		x = int(xs)	}}func setRandomBrightness(c *color.NRGBA,max uint8) {	minc := min3(c.R,c.G,c.B)	maxc := max3(c.R,c.B)	if maxc > max {		return	}	n := rand.Intn(int(max-maxc)) - int(minc)	c.R = uint8(int(c.R) + n)	c.G = uint8(int(c.G) + n)	c.B = uint8(int(c.B) + n)}func min3(x,z uint8) (o uint8) {	o = x	if y < o {		o = y	}	if z < o {		o = z	}	return}func max3(x,z uint8) (o uint8) {	o = x	if y > o {		o = y	}	if z > o {		o = z	}	return}// rnd returns a random number in range [from,to].func rnd(from,to int) int {	//println(to+1-from)	return rand.Intn(to+1-from) + from}const (	// Standard length of uniuri string to achive ~95 bits of entropy.	StdLen = 16	// Length of uniurl string to achive ~119 bits of entropy,closest	// to what can be losslessly converted to UUIDv4 (122 bits).	UUIDLen = 20)// Standard characters allowed in uniuri string.var StdChars = []byte("ABCDEFGHIJKLMnopQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")// New returns a new random string of the standard length,consisting of// standard characters.func New() string {	return NewLenChars(StdLen,StdChars)}// NewLen returns a new random string of the provIDed length,consisting of// standard characters.func NewLen(length int) string {	return NewLenChars(length,StdChars)}// NewLenChars returns a new random string of the provIDed length,consisting// of the provIDed byte slice of allowed characters (maximum 256).func NewLenChars(length int,chars []byte) string {	b := make([]byte,length)	r := make([]byte,length+(length/4)) // storage for random bytes.	clen := byte(len(chars))	maxrb := byte(256 - (256 % len(chars)))	i := 0	for {		if _,err := io.ReadFull(crand.Reader,r); err != nil {			panic("error reading from random source: " + err.Error())		}		for _,c := range r {			if c >= maxrb {				// Skip this number to avoID modulo bias.				continue			}			b[i] = chars[c%clen]			i++			if i == length {				return string(b)			}		}	}	panic("unreachable")}func pic(w http.ResponseWriter,req *http.Request) {	d := make([]byte,4)	s := NewLen(4)	ss := ""	d = []byte(s)	for v := range d {		d[v] %= 10		ss += strconv.FormatInt(int64(d[v]),32)	}	w.header().Set("Content-Type","image/png")	sess := globalSessions.SessionStart(w,req)	defer sess.SessionRelease(w)	sess.Delete("yzm")	sess.Set("yzm",ss)	//fmt.Println("-----------------------------------yzm:",sess.Get("yzm"))	NewImage(d,100,40).Writeto(w)}func index(w http.ResponseWriter,req *http.Request) {	str := "<Meta charset=\"utf-8\"><h3>golang 图片验证码例子</h3><img border=\"1\" src=\"/pic\" alt=\"图片验证码\" onclick=\"this.src='/pic'\" />"	w.header().Set("Content-Type","text/HTML")	w.Write([]byte(str))}
总结

以上是内存溢出为你收集整理的golang(beego)验证码控制器全部内容,希望文章能够帮你解决golang(beego)验证码控制器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1289427.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-09
下一篇2022-06-09

发表评论

登录后才能评论

评论列表(0条)

    保存