【kong系列五】之 基本认证Basic Authentication插件

来源:互联网 发布:华工电路二端口ucd=0.1 编辑:程序博客网 时间:2024/06/05 03:23

概述

基本认证

Basic Authentication

Add Basic Authentication to your APIs, with username and password protection. The plugin will check for valid credentials in the Proxy-Authorization and Authorization header (in this order).

使用用户名和密码,为你的apis接口集增加基本认证。该插件通过header头中的Authorization信息,校验用户有效认证信息。


Configuration

Configuring the plugin is straightforward, you can add it on top of an API by executing the following request on your Kong server:

$ curl -X POST http://kong:8001/apis/{api}/plugins \

    --data "name=basic-auth" \

    --data "config.hide_credentials=true"

api: The id or name of the API that this plugin configuration will target


配置该插件是简便的,你能够按照下面的操作,熟练地增加一个插件插件。

$ curl -X POST http://kong:8001/apis/{api}/plugins \

    --data "name=basic-auth" \

    --data "config.hide_credentials=true"

 --data 表示配置插件的参数。

{api}表示应用插件的目标api


You can also apply it for every API using the http://kong:8001/plugins/ endpoint. Read the Plugin Reference for more information.

你也能应用在全部的api上,使用http://kong:8001/plugins/这样的地址,表示应用到全局api。阅读插件指南获取更多信息。


Once applied, any user with a valid credential can access the service/API. To restrict usage to only some of the authenticated users, also add the ACL plugin (not covered here) and create whitelist or blacklist groups of users.


一旦应用,任何有效认证的用户都有权限调用该服务/api.为了限制某些用户使用,也可以为用户增加ACL插件,通过配置黑名单,白名单达到限制效果。



表单域参数介绍:

表单域名称       默认值        描述

name(必填)   插件名称,在这里该插件名称为:basic-auth

config.hide_credentials(选填)    false    boolean类型,告诉插件,是否对上游API服务隐藏认证信息。如果配置true,插件将会把认证信息清除,然后再把请求转发给上游api服务。

config.anonymous(选填)   String类型,用来作为匿名用户,如果认证失败。如果空,当请求失败时,返回一段4xx的错误认证信息。

1.创建api

api-name testBasicAuth

hosts10.110.2.3

访问url/test

urlhttp://10.110.2.54:8040/health

其他默认即可,也可视业务具体情况而定。

创建完毕后:客户端访问:http://10.110.2.3:8000/test 验证api是否可以成功调用。


2.创建用户

usernameconsumerOfBasicCustom IdcidBasic

3.为用户创建basic auth

创建basic-auth用户和秘钥:username=csOfBasicpassword=testkongpwd。注意,此处usernameconsumer username不是同一个,是username是用户的唯一公开用户名,basic-auth username则可以是多个。具体操作,如下图:


4.新增一个basic-auth插件

作用范围是全部api


5.调用示例

basic-auth用户和秘钥:username=csOfBasicpassword=testkongpwd按如下格式进行base64编码:

csOfBasic:testkongpwd   Y3NPZkJhc2ljOnRlc3Rrb25ncHdk

请复制csOfBasic:testkongpwd到你的base64编码代码中测试,编码后的密文,一定是Y3NPZkJhc2ljOnRlc3Rrb25ncHdk,如果不是,那你的base64编码代码一定是错误的。

以下方式验证成功!

验证1curl http://10.110.2.3:8000/test    -H 'Authorization: Basic Y3NPZkJhc2ljOnRlc3Rrb25ncHdk'   

验证2basic 认证,在header中添加key=Authorizationvalue= Basic Y3NPZkJhc2ljOnRlc3Rrb25ncHdk


原创粉丝点击