美文网首页
rirs BasicAuth基础认证

rirs BasicAuth基础认证

作者: 944f671bfb68 | 来源:发表于2021-07-16 11:08 被阅读0次
  1. 实现原理
  2. 代码实现
  3. 自建基础认证中间件
  4. 优化点

1.实现原理

BasicAuth是一种浏览器端实现的简单认证方式,在请求Headers中检查Authorization字段,是否存在,并且在有效期内,如果不满足条件。返回401 Unauthorised ,浏览器端弹窗输入用户名密码。
步骤1. 客户端请求
步骤2. 服务端验证是否包含字段Authorization
步骤3.服务端验证,包含Authorization,验证为空,验证用户密码是否和服务端信息一致
步骤4.返回信息,验证通过直接返回,不通过返回
code 401
header:WWW-Authenticate:Basic realm=access info

2. 代码实现

package main

import (
    "github.com/kataras/iris/v12"
    "github.com/kataras/iris/v12/middleware/basicauth"
    "time"
)

func newApp() *iris.Application {
    app := iris.New()
    // 配置信息  users{username:password} 可配置多个
    basicAuthConfig := basicauth.Config{
        Users: map[string]string{"admin":"admin"},
        Expires: time.Duration(30) * time.Minute,
    }
    authentication := basicauth.New(basicAuthConfig)

    authRouter := app.Party("/",authentication)
    authRouter.Get("/ping", func(ctx iris.Context) {
        ctx.WriteString("pong")
    })
    authRouter.Get("/profile", h)

    return app
}
func main()  {
    app := newApp()
    app.Run(iris.Addr(":8080"))
}
func h(ctx iris.Context)  {
    username,password,ok :=ctx.Request().BasicAuth()
    if ok {
        ctx.JSON(iris.Map{
            "username":username,
            "password": password,
        })
    }
}

3. 自建基础认证中间件

package basicauth

import (
    "encoding/base64"
    "fmt"
    "github.com/kataras/iris/v12/context"
    "strings"
)

//type BasicAuthMiddleware context.Handler

type BasicAuthConfig struct {
    Users  map[string]string
    Realme string
}
// todo func DefaultConfig
// todo 配置信息 绑定到basicauth上
// todo 过期时间

func NewBasicAuth(config BasicAuthConfig) context.Handler {
    return func(ctx context.Context) {
        //查看Authorization是否存在
        Authorization := ctx.GetHeader("Authorization")
        Authorization = strings.TrimPrefix(Authorization, "Basic ")
        if Authorization == "" {
            NoAuth(ctx, config.Realme)
            ctx.StopExecution()
            return
        }
        //解析Authorization
        res := parseAuthorization(Authorization, config.Users)
        if !res {
            NoAuth(ctx, config.Realme)
            ctx.StopExecution()
            return
        }
        ctx.Next()
    }

}

func parseAuthorization(Authorization string, users map[string]string) bool {
    userinfo, err := base64.StdEncoding.DecodeString(Authorization)
    if err != nil {
        return false
    }
    fmt.Println(string(userinfo))
    user := strings.Split(string(userinfo), ":")
    name := user[0]
    password := user[1]

    if pass, ok := users[name]; ok && pass == password {
        return true
    }

    return false
}
func NoAuth(ctx context.Context, realm string) {
    ctx.StatusCode(401)
    if realm == "" {
        ctx.Header("WWW-Authenticate", "Basic realm=access App")
    } else {
        ctx.Header("WWW-Authenticate", "Basic realm="+realm)
    }

}

4. 代码优化点

  1. 配置信息,创建默认配置方法,调用时先使用默认配置,然后根据传递的配置信息,再修改
  2. 定义basicauth结构体,汇总信息

相关文章

  • rirs BasicAuth基础认证

    实现原理 代码实现 自建基础认证中间件 优化点 1.实现原理 BasicAuth是一种浏览器端实现的简单认证方式,...

  • Jira和Wiki登录认证BasicAuth

    Jira和Wiki同属于Atlassian 公司,要访问其API首先要有用户登录状态。直接使用BasicAuth即...

  • basicAuth和jwt

    basicAuth前端提交方式: 》业务逻辑:前端通过basicAuth携带token请求AP,后端接受到请求,通...

  • 做微博需要做的事情

    一、基础资料 二、认证 认证分为:身份认证、兴趣认证、自媒体认证、金V认证、超话认证、故事红人认证。 3、发文 4...

  • 办理中国CCC认证需要提交哪些资料?

    CCC认证属于中国强制认证,是产品的基础安全认证非产品质量认证。 办理CCC认证需要提交资料: 1、办理CCC认证...

  • AEO认证问题集合Q&A

    了让更多参与AEO认证项目的同仁了解掌握AEO认证的基础概念,内容和一些核心要素,我们整理了AEO认证基础知识普及...

  • 2018-04-08 新媒体运营干货02

    微博运营的基础搭建 微博认证: 入口:导航栏---V认证 1.个人认证:面向个人用户的认证方式,通过认证后,头像部...

  • 2018-08-22

    什么是SCRUM认证体系 ? Scrum认证是一个针对个人职业发展的认证体系,基础级认证主要面向Scrum的三个角...

  • 广东深圳cqc和ccc认证的介绍

    很多人以为ccc认证就是质量认证,其实不是,ccc认证是一种基础的安全认证,在列入《实施强制性产品认证的产品目录》...

  • TOGAF认证自学宝典

    TOGAF认证介绍 Togaf认证考试分为基础级(Foundation)和鉴定级(Certified)两个级别,在...

网友评论

      本文标题:rirs BasicAuth基础认证

      本文链接:https://www.haomeiwen.com/subject/fdtmpltx.html