keycloak 是一个比较全,而且比较方便的sso 解决方案,同时为我们提供了灵活的扩展特性
备注: 测试使用docker-compose 运行,对于keycloak 使用pg 数据库做为后端存储

环境准备

  • docker-compose文件
version: "3"
services:
openresty:
build:
context: ./
dockerfile: ./Dockerfile
ports:
- "8090:80"
volumes:
- "./nginx_lua/:/opt/app/"
- "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
auth:
image: jboss/keycloak
ports:
- "8080:8080"
environment:
- "KEYCLOAK_USER=dalong"
- "KEYCLOAK_PASSWORD=dalongrong"
- "DB_VENDOR=postgres"
- "DB_ADDR=postgres"
- "DB_DATABASE=postgres"
- "DB_USER=postgres"
- "DB_PASSWORD=dalong"
- "PROXY_ADDRESS_FORWARDING=true"
postgres:
image: postgres:9.6
ports:
- "5432:5432"
environment:
- "POSTGRES_PASSWORD:dalong"
  • nginx 配置
    dockerfile: 主要是添加lua 包
FROM openresty/openresty:alpine-fat
LABEL author="1141591465@qq.com"
RUN /usr/local/openresty/luajit/bin/luarocks install lua-resty-openidc
EXPOSE 80

nginx.conf: 配置lua-resty-openidc 相关参数(注意realm 需要创建,同时需要创建client 以及添加一个可以登陆的用户)

worker_processes 1;
user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_code_cache off;
lua_need_request_body on;
gzip on;
# cache for discovery metadata documents
lua_shared_dict discovery 1m;
# cache for JWKs
lua_shared_dict jwks 1m;
resolver 127.0.0.11 ipv6=off;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
lua_package_path '/opt/app/?.lua;;';
server {
listen 80;
server_name localhost;
charset utf-8;
## 此参数比较重要,因为为了方便我关闭了lua code cache
set $session_secret 623q4hR325t36VsCD3g567922IC0073T;
default_type text/html;
access_by_lua_block {
require("oidc/acc")()
}
expires 0;
add_header Cache-Control private;
location / {
default_type text/plain;
index index.html index.htm;
}
location /redirect_uri {
default_type text/plain;
content_by_lua_block {
ngx.req.read_body()
require("oidc/init")()
}
}
location = /favicon.ico {
root /opt/app/static;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} }
}
  • lua 代码说明
local cjson = require("cjson")
function init()
-- 配置参数从keycloak系统获取
local opts = {
redirect_uri_path = "/redirect_uri",
accept_none_alg = true,
discovery = "http://auth:8080/auth/realms/nginx/.well-known/openid-configuration",
client_id = "nginx",
client_secret = "1fb2d094-3a66-4c30-9cdb-f4210939fb1d",
redirect_uri_scheme = "http",
logout_path = "/logout",
redirect_after_logout_uri = "http://auth:8080/auth/realms/nginx/protocol/openid-connect/logout?redirect_uri=http://localhost:8090/",
redirect_after_logout_with_id_token_hint = false,
session_contents = {id_token = true}
}
-- call introspect for OAuth 2.0 Bearer Access Token validation
local res, err = require("resty.openidc").authenticate(opts)
if err then
ngx.status = 403
ngx.say(err)
ngx.exit(ngx.HTTP_FORBIDDEN)
end
-- ngx.say(cjson.encode(res))
end return init

keycloak 配置信息

  • realm

    *client
  • user

启动&&测试

  • 启动
docker-compose up -d
  • 本地hosts 配置
    因为使用了dns 地址
/etc/hosts
添加
127.0.0.1 auth
  • 测试
    打开 http://localhost:8090 会重定向到keycloak 的登陆页面,输入添加的用户信息


    session 信息

说明

注意我nginx.conf 中注释的说明,因为lua-resty-openidc 使用了lua-resty-session 但是lua-resty-session 在关闭lua cache 的时候
每次session secreet 会重新生成,所以比较靠谱的方便是指定一个session secret ,参考上边nginx 配置

参考资料

https://developers.redhat.com/blog/2018/10/08/configuring-nginx-keycloak-oauth-oidc/
https://github.com/rongfengliang/keycloak-openresty-openidc

 
 
 
 

openresty 集成 keycloak-oauth-oidc的更多相关文章

  1. 集成基于OAuth协议的单点登陆

    在之前的一篇文章中,我们已经介绍了如何为一个应用添加对CAS协议的支持,进而使得我们的应用可以与所有基于CAS协议的单点登陆服务通讯.但是现在的单点登陆服务实际上并不全是通过实现CAS协议来完成的.例 ...

  2. 在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client

    在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client   阅读目录 验证代码流程 Refreshing a Token Built-In Providers 这个包能够让你 ...

  3. VUE集成keycloak和Layui集成keycloak

    一:KEYCLOAK配置部分: 1,下载keycloak,官网地址:https://www.keycloak.org/downloads.html.下载第一个就行 2,下载完毕之后,打开文件,访问 b ...

  4. pushpin Server-sent events && openresty 集成试用

    前边有写过一个简单pushpin 集成stream 的demo,这次测试下sse 的功能 备注: 环境依然使用的是docker-compose运行 环境准备 docker-compose 文件 ver ...

  5. jenkins 集成 keycloak 认证

    keycloak 是很不错的sso 工具,当然也有Jenkins 的插件,我们可以使用jenkins 插件,方便用户账户的管理 环境准别 docker-compose version: "3 ...

  6. gearman openresty 集成试用

    很简单使用了一个openresty 的lua 模块 环境准备 docker-compose 文件 详细配置可以参考 https://github.com/rongfengliang/gearmango ...

  7. pushpin openresty 集成试用

    pushpin 是一个很不错的将restapi 转换为reailtime api 的proxy,openresty 具有很强的nginx 控制能力 可以方便的用来进行api 的开发,默认其他语言pus ...

  8. openresty 集成 sentry 异常系统

    sentry 是一个方便的错误异常追踪系统,同时社区也提供了openresty 的lua 包,使用docker-compose 进行测试 备注: sentry 部分的配置来自官方文档 环境准备 doc ...

  9. openresty 集成lua-resty-mail +smtp2http 扩展灵活的mail 服务

    lua-resty-mail 是一个不错的openresty mail 扩展,我们可以用来进行邮件发送,支持附件功能 smtp2http 是一个smtp 服务,可以将smtp 请求数据转换为http ...

随机推荐

  1. 一. Python基础(1)--语法

    一. Python基础(1)--语法 1. 应用程序 1.1 什么是计算机(Computer)? 组成 ①运算器 arithmetic unit; ※ Arithmetic unit and cont ...

  2. ubuntu多显示器单触摸屏校准

    多显示器单触摸屏屏幕校准 0.触摸屏重定向 sudo xinput map-to-output 13 DP1  #将触摸屏映射到指定的显示器 其中:13为触摸屏设备id,可通过 xinput命令查看 ...

  3. git中误删提交(commit)后,怎么恢复

    “xml文件存储数据”提交被我误操作,即使用reset  --hard删除了,然后又进行了三次提交,发现删除的提交有用,需要找回来, 于是找了好久,找到好方法: 1.进入工程下的.git文件下,git ...

  4. IBM MQ 集成CXF 发送JMS 消息

    0.POM依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

  5. system的共享内存实例

    system的共享内存指的是内核指定一块内存区域映射到虚拟地址空间供进程通信使用的机制 1\创建或打开共享内存块函数原型int shmget(key_t key, size_t size, int s ...

  6. FCC JS基础算法题(8):Slasher Flick(截断数组)

    题目描述: 返回一个数组被截断n个元素后还剩余的元素,截断从索引0开始. 这个题目有两个方法,都比较简单,用slice方法: function slasher(arr, howMany) { // 请 ...

  7. 【Python】unittest-2-断言

    Unittest中的断言 1.  python unintest单元测试框架提供了一整套内置的断言方法. (1)如果断言失败,则抛出一个AssertionError,并标识该测试为失败状态 (2)如果 ...

  8. 网络协议理论,http协议,数据结构,常用返回码

    一.网络协议理论 先是DNS协议 将域名转化成IP地址 这个你要知道 域名只是人记着方便 计算机记的是IP 然后是TCPIP协议 数据在传输过程中可能要经过陆游器 涉及到的是ARP协议 将IP地址转换 ...

  9. OSPF路由协议(二)

    实验要求:使用OSPF路由协议,使每个路由器都能收集到所有网段 拓扑如下: 配置如下: R1enableconfigure terminalinterface l0ip address 192.168 ...

  10. weex--手机调试

    Weex Playground 我的是小米应用市场,没有搜索到,不过这是官方的二维码,我是扫描这个二维码下载的.