markdown 将JWT与Kong一起使用的简短示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 将JWT与Kong一起使用的简短示例相关的知识,希望对你有一定的参考价值。

### JWT Kong Example

* Get and Start Kong and Co
``` 
git clone git@github.com:Mashape/docker-kong.git
cd docker-kong/compose
docker-compose up
```
* Create Kong API Route
```
curl -X POST http://localhost:8001/apis/ \
   --data "request_path=/skywalkerDemo" \
   --data "upstream_url=https://www.finleap.com" \
   --data "strip_request_path=true"
```
* Enable JWT Plugin For Route

```
curl -X POST http://localhost:8001/apis/skywalkerDemo/plugins \
    --data "name=jwt" \ 
    --data "config.secret_is_base64=true"
```
* Create a Kong Consumer

```
curl -X POST http://localhost:8001/consumers \
   --data "username=bill"
```
* Create JWT Credentials for User and Note _key_ and _secret_ from response

```
curl -H "Content-Type: application/json" -X POST -d '{}' http://localhost:8001/consumers/bill/jwt

Response: {"secret":"b9f813fb8753440eabb1b44f9ba4da2f",
  "id":"33dacbca-ce7d-4305-a213-1ce7b8f7af71",
  "algorithm":"HS256",
  "created_at":1473085487000,
  "key":"cef1fe6937e444a6b18a26965d619718",
  "consumer_id":"67c90d83-636b-4c36-85ac-a14eac54ebe2"}
```
* Craft JWT using e.g. [http://jwtbuilder.jamiekurtz.com/](http://jwtbuilder.jamiekurtz.com/)
	* Issuer (key from above): cef1fe6937e444a6b18a26965d619718
	* Key (secret from above): b9f813fb8753440eabb1b44f9ba4da2f
	
	Results in JWT: `eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjZWYxZmU2OTM3ZTQ0NGE2YjE4YTI2OTY1ZDYxOTcxOCIsImlhdCI6bnVsbCwiZXhwIjpudWxsLCJhdWQiOiIiLCJzdWIiOiIifQ.50bnA9d3wjIYIx6m-fzxIHQHfXF6zuJRPMUdk8-4LTU`

* Issue request to API endpoint with JWT im header

```
curl http://localhost:8000/skywalkerDemo \
    -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjZWYxZmU2OTM3ZTQ0NGE2YjE4YTI2OTY1ZDYxOTcxOCIsImlhdCI6bnVsbCwiZXhwIjpudWxsLCJhdWQiOiIiLCJzdWIiOiIifQ.50bnA9d3wjIYIx6m-fzxIHQHfXF6zuJRPMUdk8-4LTU'

```


以上是关于markdown 将JWT与Kong一起使用的简短示例的主要内容,如果未能解决你的问题,请参考以下文章

具有自定义授权的 JWT 和 KONG

没有创建消费者的 Kong 和 JWT

用户注册+与kong集成的微服务的身份验证

Kong 和 JWT - API 应如何从令牌中提取信息

使用带有 KONG API 网关的 cookie 进行 JWT 身份验证

使用 Kong 进行身份验证