.net core

Kong + Oauth 密码模式设计

2018-11-06  本文已影响82人  GoddyWu

资料

图解流程

kong 官方密码模式的流程图


图中可见,kong的官方设计是需要我们自己开发一个backend(后端)项目,来做客户端与kong中间层,用于拼接完整的oauth请求信息并请求至kong。但是这里的webapp backend是没有被kong管理起来的,它也需要kong来提供管理服务,理想设计为下图。

本文设计的流程图

原图在资料第一条仓库中


图例:(线段是对应颜色节点的行为)

webapp backend 搭建

backend需要做的:

下图是一个简单版本的backend,去除了和数据库的交互。
注:这里使用的flask框架

java(reactor)版本的backend也在代码库中: https://gitlab.com/goddy-test/public/kong/blob/master/bot-auth/docker-compose.yml。有一个坑: https://stackoverflow.com/questions/7648872/can-i-override-the-host-header-where-using-javas-httpurlconnection-class。 以及java解析权限的示例项目:https://gitlab.com/goddy-test/public/kong/tree/dev/demo-resource

字段数据的来源:

构建流程

1.docker-compose启动服务

参考:https://github.com/Kong/docker-kong/tree/master/compose

version: '2.1'
services:
  kong-database:
    image: postgres:9.5
    environment:
      - POSTGRES_USER=kong
      - POSTGRES_DB=kong
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres"]
      interval: 30s
      timeout: 30s
      retries: 3
    restart: on-failure
  kong:
    image: kong:latest
    command: kong start --run-migrations
    depends_on:
      kong-database:
        condition: service_healthy
    healthcheck:
      test: "kong health"
      interval: 10s
      timeout: 5s
      retries: 5
    environment:
      - KONG_DATABASE=postgres
      - KONG_PG_HOST=kong-database
      - KONG_PG_DATABASE=kong
      - KONG_ADMIN_LISTEN=0.0.0.0:8001        
    ports:
      - "8000:8000"
      - "8001:8001" # 后续需要加密
      - "8443:8443"
      - "8444:8444" # 后续需要加密
    restart: on-failure
  kong-dashboard:
    image: pgbi/kong-dashboard
    depends_on:
      kong:
        condition: service_healthy
    entrypoint: ./docker/entrypoint_dev.sh
    ports:
      - "8081:8080" # 后续需要加密,可以不使用
  kong-backend:
    image: godbaby/kong-python:1.0
    volumes:
      - "/Users/goddy/test/kong/docker/app.py:/code/app.py" # 方便测试
    ports:
      - "3000:5000" # 这里不需要暴露,只是方便调试
  kong-resource:
    image: godbaby/kong-python:1.0
    volumes:
      - "/Users/goddy/test/kong/docker/resource.py:/code/app.py" # 方便测试
    ports:
      - "3001:5000" # 这里不需要暴露,只是方便调试

2.构建kong管理服务

注:这里也可以使用kong dashboard来操作。

3. 访问资源

上一篇 下一篇

猜你喜欢

热点阅读