热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

kong的多种认证方式设置

篇首语:本文由编程笔记#小编为大家整理,主要介绍了kong的多种认证方式设置相关的知识,希望对你有一定的参考价值。1.新建一个用于认证验证的consumer

篇首语:本文由编程笔记#小编为大家整理,主要介绍了kong的多种认证方式设置相关的知识,希望对你有一定的参考价值。



1.新建一个用于认证验证的consumer


(1).新建consumer

curl -X POST --url http://localhost:8001/consumers/ --data "username=auth_user"

返回如下


"custom_id":null,
"created_at":1566380171,
"username":"auth_user",
"id":"f1b6c168-f6e3-482e-a477-2d09d14dce8b"


(2).为该consumer生成key-auth认证秘钥

curl -X POST --url http://localhost:8001/consumers/auth_user/key-auth/

返回


"key": "PDKhYhkbjfZueFBQ7qe3nOYuWiUdOiiN",
"created_at": 1566382283,
"consumer":
"id": "f1b6c168-f6e3-482e-a477-2d09d14dce8b"
,
"id": "f8edga25-5e18-4ec1-a41d-b4fc9ea20208"

可以通过接口/key-auths查看所有消费者的秘钥,通过/consumers/consumer/key-auth接口查看指定消费者的秘钥。


(3).为该consumer生成jwt认证秘钥

curl -X POST --url http://localhost:8001/consumers/auth_user/jwt -H "Content-Type: application/x-www-form-urlencoded"

返回


"rsa_public_key": null,
"created_at": 1566382780,
"consumer":
"id": "f1b6c168-f6e3-482e-a477-2d09d14dce8b"
,
"id": "c0296bae-b9bd-4835-b433-95dff237bb4b",
"algorithm": "HS256",
"secret": "EGUUr9v99DMGwIk9SpBxvigjzFi5GsBZ",
"key": "GiweuLOEAwSO9cxkibug7MaBfdJE4NPB"


2.再新建一个consumer用于识别匿名用户

curl -X POST --url http://localhost:8001/consumers/ --data "username=anonymous"

返回


"custom_id": null,
"created_at": 1566388995,
"username": "anonymous",
"id": "958e85d7-e39d-4d2c-b8a9-888e25dbeed5"


3.为route或service 启用多个认证插件


1.为服务example-service 启用 key-auth 认证

curl -i -X POST \\
--url http://localhost:8001/services/example-service/plugins/ \\
--data 'name=key-auth'

返回


"created_at": 1566381363,
"config":
"key_names": [
"apikey"
],
"run_on_preflight": true,
"anonymous": null,
"hide_credentials": false,
"key_in_body": false
,
"id": "947db416-1f3a-4a54-a5ff-75b6a55206d7",
...
"name": "key-auth"


2.为服务启用JWT认证

curl -i -X POST \\
--url http://localhost:8001/services/example-service/plugins/ \\
--data 'name=jwt'

返回


"created_at": 1566383438,
"config":
"secret_is_base64": false,
"key_claim_name": "iss",
"COOKIE_names": [],
"maximum_expiration": 0,
"claims_to_verify": null,
"anonymous": null,
"run_on_preflight": true,
"uri_param_names": [
"jwt"
]
,
"id": "fc54429b-73cd-4215-8f4d-35a21c6a389e",
...
"name": "jwt"


3.为添加的认证插件启用匿名访问

因为添加多个认证插件后,默认情况下是需要多个插件同时满足,才算认证成功的。所以如果要使多个认证的关系为“或”的关系,那么第一步就必须为插件启用匿名访问

key-auth插件启用匿名访问,插件id为947db416-1f3a-4a54-a5ff-75b6a55206d7,匿名用户id为958e85d7-e39d-4d2c-b8a9-888e25dbeed5

curl -X PATCH --url http://localhost:8001/services/example-service/plugins/947db416-1f3a-4a54-a5ff-75b6a55206d7/ \\
--data "config.anonymous=958e85d7-e39d-4d2c-b8a9-888e25dbeed5"

jwt插件启用匿名访问,插件id为fc54429b-73cd-4215-8f4d-35a21c6a389e,匿名用户id为958e85d7-e39d-4d2c-b8a9-888e25dbeed5

curl -X PATCH --url http://localhost:8001/plugins/fc54429b-73cd-4215-8f4d-35a21c6a389e/ \\
--data "config.anonymous=958e85d7-e39d-4d2c-b8a9-888e25dbeed5"

4.拦截匿名访问

经过上面步骤后,虽然能够使多个认证之间为“或”的关系,但是也允许了匿名用户的访问(即不需认证也可以访问),所以还需要拦截匿名用户。这里使用request-termination插件


(1).启用插件

curl -X POST http://localhost:8001/services/example-service/plugins/ \\
--data "name=request-termination" \\
--data "config.status_code=401" \\
--data "config.content_type=application/json; charset=utf-8" \\
--data "config.body=\\"message\\": \\"Authentication required\\""

(2).匿名消费者anonymous启用该拦截插件

匿名消费者anonymous启用该拦截插件,插件id为e5ff19cf-006d-4fcd-ae00-5837bc5d6938,匿名消费者id为958e85d7-e39d-4d2c-b8a9-888e25dbeed5

curl -X PATCH http://localhost:8001/plugins/e5ff19cf-006d-4fcd-ae00-5837bc5d6938/ \\
--data "consumer.id=958e85d7-e39d-4d2c-b8a9-888e25dbeed5"

需要特别注意的是config.anonymous的值是第2步创建的匿名消费用户的id,而不是第一步创建的消费用户的id

官方文档

Multiple Authentication
Kong supports multiple authentication plugins for a given Service, allowing different clients to utilize different authentication methods to access a given Service or Route.
The behaviour of the auth plugins can be set to do either a logical AND, or a logical OR when evaluating multiple authentication credentials. The key to the behaviour is the config.anonymous property.
config.anonymous not set
If this property is not set (empty), then the auth plugins will always perform authentication and return a 40x response if not validated. This results in a logical AND when multiple auth plugins are being invoked.
config.anonymous set to a valid consumer id
In this case, the auth plugin will only perform authentication if it was not already authenticated. When authentication fails, it will not return a 40x response, but set the anonymous consumer as the consumer. This results in a logical OR + ‘anonymous access’ when multiple auth plugins are being invoked.
NOTE 1: Either all or none of the auth plugins must be configured for anonymous access. The behaviour is undefined if they are mixed.
NOTE 2: When using the AND method, the last plugin executed will be the one setting the credentials passed to the upstream service. With the OR method, it will be the first plugin that successfully authenticates the consumer, or the last plugin that will set its configured anonymous consumer.
NOTE 3: When using the OAuth2 plugin in an AND fashion, then also the OAuth2 endpoints for requesting tokens and so forth will require authentication by the other configured auth plugins.

推荐阅读
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 本文讨论了在使用PHP cURL发送POST请求时,请求体在node.js中没有定义的问题。作者尝试了多种解决方案,但仍然无法解决该问题。同时提供了当前PHP代码示例。 ... [详细]
  • springboot启动不了_Spring Boot + MyBatis 多模块搭建教程
    作者:枫本非凡来源:www.cnblogs.comorzlinp9717399.html一、前言1、创建父工程最近公司项目准备开始重构,框 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • Webmin远程命令执行漏洞复现及防护方法
    本文介绍了Webmin远程命令执行漏洞CVE-2019-15107的漏洞详情和复现方法,同时提供了防护方法。漏洞存在于Webmin的找回密码页面中,攻击者无需权限即可注入命令并执行任意系统命令。文章还提供了相关参考链接和搭建靶场的步骤。此外,还指出了参考链接中的数据包不准确的问题,并解释了漏洞触发的条件。最后,给出了防护方法以避免受到该漏洞的攻击。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法
    本文介绍了解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法,包括检查location配置是否正确、pass_proxy是否需要加“/”等。同时,还介绍了修改nginx的error.log日志级别为debug,以便查看详细日志信息。 ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  •     这里使用自己编译的hadoop-2.7.0版本部署在windows上,记得几年前,部署hadoop需要借助于cygwin,还需要开启ssh服务,最近发现,原来不需要借助cy ... [详细]
  • mysqldinitializeconsole失败_mysql03误删除了所有用户解决办法
    误删除了所有用户解决办法第一种方法(企业常用)1.将数据库down掉[rootdb03mysql]#etcinit.dmysqldstopShuttingdownMySQL..SU ... [详细]
author-avatar
书友70518356
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有