Keycloak Gatekeeper:Keycloak通用代理

Intro

Keycloak支持OpenID Connect以及SAML 2.0,对于不同的编程语言有不同的适配,比如对于Java,Spring Boot和Spring Security可以适配OpenID Connect的安全协议。OpenID Connect (OIDC) 是一种OAuth 2.0的延伸,运用了Json Web Token (JWT) 标准,生成JSON格式的token。Keycloak Gatekeeper是用Go语言编写的使用OIDC的代理服务,主要应用是方便的把Keycloak同时运用在多个服务上。比如一个网站会用到多种使用不同编程语言的微服务:数据处理/图片处理/业务逻辑/log监控等,Gatekeeper是一个通用的适配器,可以轻松的给这些微服务统一都配置好Keycloak授权验证。

Download

  • 这里下载Gatekeeper并运行:
./keycloak-gatekeeper[.exe]
  • 更加建议在Docker里运行,更方便:
docker run -it --rm quay.io/keycloak/keycloak-gatekeeper

Configuration options

可以放在yml或者json文件来配置Gatekeeper。

# keycloak的url:<server>/auth/realm/<realm_name>
discovery-url: https://keycloak.example.com/auth/realms/<REALM_NAME>

# client id
client-id: <CLIENT_ID>

# client secret
client-secret: <CLIENT_SECRET>

# 监听端口
listen: :3000

# 各种配置,不止这些,按照字意选填即可
enable-refresh-tokens: true
enable-logout-redirect: true
enable-default-deny: true
upstream-keepalives: true
upstream-timeout: 500s
server-read-timeout: 500s
server-write-timeout: 500s
preserve-host: true
tls-cert:
tls-private-key:
encryption-key: <ENCRYPTION_KEY>

# 网站地址
redirection-url: http://127.0.0.1:3000

# 还要代理什么其它url
upstream-url: http://127.0.0.1:80

# 所有需要用到keycloak的url放在resources里面
resources:
- uri: /admin/test
  # 请求方式,如果methods不填,默认所有
  methods:
  - GET
  # user必须要同时有所有这些roles才能访问,如果roles不填,就是什么roles都可
  roles:
  - client:test1
  - client:test2
  - test
- uri: /admin/*
  methods:
  - GET
  roles:
  - client:test1
  - client:test2
- uri: /public/*
  # 白名单,都可以访问
  white-listed: true
- uri: /admin*
  methods:
  - GET
  roles:
  - client:test1
  - client:test2
  # 满足其中一个role即可
  require-any-role: true
  groups:
  - admins
  - users

也可以通过命令行配置:

bin/keycloak-gatekeeper \
    --discovery-url=https://keycloak.example.com/auth/realms/<REALM_NAME> \
    --client-id=<CLIENT_ID> \
    --client-secret=<SECRET> \
    --listen=127.0.0.1:3000 \ # unix sockets format unix://path
    --redirection-url=http://127.0.0.1:3000 \
    --enable-refresh-tokens=true \
    --config=/config.yaml \ # 可以结合之前的配置文件一起用
    --forbidden-page=/forbidden.html \
    --signin-page=/signin.html \ # 自定义登录界面
    --encryption-key=AgXa7xRcoClDEU0ZDSH4X0XhL5Qy2Z2j \
    --upstream-url=http://127.0.0.1:80 \
    --store-url=redis://USER:PASSWORD@HOST:PORT \ # 储存加密的refresh tokens
    --enable-default-deny=true \
    --resources="uri=/admin*|roles=test1,test2" \
    --resources="uri=/backend*|roles=test1"

还有一些其它配置,包括HTTPS重定向、自定义headers、配置Let’s Encrypt、自定义open-id provider等~

Resources

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值