openresty 集成 keycloak-oauth-oidc
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的更多相关文章
- 集成基于OAuth协议的单点登陆
在之前的一篇文章中,我们已经介绍了如何为一个应用添加对CAS协议的支持,进而使得我们的应用可以与所有基于CAS协议的单点登陆服务通讯.但是现在的单点登陆服务实际上并不全是通过实现CAS协议来完成的.例 ...
- 在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client
在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client 阅读目录 验证代码流程 Refreshing a Token Built-In Providers 这个包能够让你 ...
- VUE集成keycloak和Layui集成keycloak
一:KEYCLOAK配置部分: 1,下载keycloak,官网地址:https://www.keycloak.org/downloads.html.下载第一个就行 2,下载完毕之后,打开文件,访问 b ...
- pushpin Server-sent events && openresty 集成试用
前边有写过一个简单pushpin 集成stream 的demo,这次测试下sse 的功能 备注: 环境依然使用的是docker-compose运行 环境准备 docker-compose 文件 ver ...
- jenkins 集成 keycloak 认证
keycloak 是很不错的sso 工具,当然也有Jenkins 的插件,我们可以使用jenkins 插件,方便用户账户的管理 环境准别 docker-compose version: "3 ...
- gearman openresty 集成试用
很简单使用了一个openresty 的lua 模块 环境准备 docker-compose 文件 详细配置可以参考 https://github.com/rongfengliang/gearmango ...
- pushpin openresty 集成试用
pushpin 是一个很不错的将restapi 转换为reailtime api 的proxy,openresty 具有很强的nginx 控制能力 可以方便的用来进行api 的开发,默认其他语言pus ...
- openresty 集成 sentry 异常系统
sentry 是一个方便的错误异常追踪系统,同时社区也提供了openresty 的lua 包,使用docker-compose 进行测试 备注: sentry 部分的配置来自官方文档 环境准备 doc ...
- openresty 集成lua-resty-mail +smtp2http 扩展灵活的mail 服务
lua-resty-mail 是一个不错的openresty mail 扩展,我们可以用来进行邮件发送,支持附件功能 smtp2http 是一个smtp 服务,可以将smtp 请求数据转换为http ...
随机推荐
- Cracking The Coding Interview5.2
//Given a (decimal - e.g. 3.72) number that is passed in as a string, print the binary representatio ...
- FPGA的GTP(aurora 协议)高速串行接口数据收发(转)
reference:https://blog.csdn.net/qq_40261818/article/details/83039829 PG046-Aurora 8B/10B Logicore I ...
- CAN总线(1)--初探(更新中)
前言: CAN总线可以控制可以使用Xilinx中IP核来直接实现,也可以使用专用的CAN芯片(例如:SJA1000)通过单片机和FPGA驱动控制来实现: 目前是使用控制器SJA1000来进行实现: C ...
- [PyImageSearch] Ubuntu16.04下针对OCR安装Tesseract
今天的博文是安装和使用光学字符识别(OCR)的Tesseract库的两部分系列的第一部分. 本系列的第一部分将着重于在您的机器上安装和配置Tesseract,然后使用tesseract命令将OCR应用 ...
- 通过 onclick = "test()"事件定义的事件 , 如何触发.
<div onclick="test()" id="xxx">点击</div> function test() { alert('123 ...
- python自学第四天,字符串用法
String 的用法 names="张三 welcome {city}" print(names.capitalize())#首字母大写 print(names.count(&qu ...
- 解决 java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L的问题
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</a ...
- mysql取差集、交集、并集
mysql取差集.交集.并集 博客分类: Mysql数据库 需求:从两个不同的结果集(一个是子集,一个是父集),字段为电话号码phone_number,找出父集中缺少的电话号码,以明确用户身份. 结合 ...
- springboot默认创建的bean是单实还是多例
转:https://blog.csdn.net/q1512451239/article/details/53122687 springboot默认创建的bean是单实还是多例 曾经面试的时候有面试官问 ...
- placeholder兼容性问题
由于placeholder是H5新属性,IE9及以下都不支持 解决办法:给input添加一个背景图,背景图里面添加placeholder内容,当焦点落在输入框中,背景图隐藏,即可做出类似的效果 代码: ...