request help: how to set redirect_uri, when i use openid-connect
Issue description
hello , i use spring-authorization-server as a OpenID authorization server. It requires a redirect_uri to limit the redirect . In apisix settings how to set it? here is my settings:
{
"name": "oauth",
"upstream_id": "386066599586562754",
"plugins": {
"openid-connect": {
"client_id": "login-client",
"client_secret": "{noop}openid-connect",
"disable": false,
"discovery": "http://auth-server:9999/.well-known/openid-configuration",
"redirect_uri": "http://172.0.0.1:9080/"
}
}
}
when use authorization_code , it can make the code,but apisix can't use it. so ,what's the correct redirect_uri?
Environment
- apisix version (cmd:
apisix version): - OS (cmd:
uname -a): - OpenResty / Nginx version (cmd:
nginx -Voropenresty -V): - etcd version, if have (cmd: run
curl http://127.0.0.1:9090/v1/server_infoto get the info from server-info API): - apisix-dashboard version, if have:
- the plugin runner version, if the issue is about a plugin runner (cmd: depended on the kind of runner):
- luarocks version, if the issue is about installation (cmd:
luarocks --version):
Hi @leslie-tsang, would you please take a look?
我主要是不懂将redirect url 设置成什么,我已经获得了code
Hi, @lizhouyang
Enable this plugin on the route, when the user request arrives, APISIX will get the session in the request, if it cannot find it, it will jump to the OIDC authentication address (where the default value of redirect_uri is the path of the current request), when the authentication is completed, it will carry the code and state back to the original request address, then APISIX will send the code and other information to the token endpoint to get the token, after that APISIX will cache the token and expiration time to the memory cache and release the legitimate request.
When a user holds a token, but it is not yet cached in APISIX, APISIX will send a request to introspection to get information about the token to confirm its validity.
Therefore, you don't need to do anything with the code. After the user returns from the OIDC authentication service, the user will visit such address xxx.com/xxx?code=xxx&state=xxx, and APISIX will automatically handle the subsequent steps, such as exchanging tokens and pass requests, etc.
在路由上开启这个插件后,当用户请求到达时,APISIX会获取请求中的session,如果无法找到,它将会跳转至OIDC认证地址(其中redirect_uri默认值为当前请求的路径),当认证完成后,将会携带code和state回到原有的请求地址,这时APISIX会将code等信息发送至token端点以获取token,之后APISIX会将token和过期时间缓存至内存缓存中,并放行合法请求。
当一个用户持有token,但APISIX中尚未缓存它时,APISIX将会向introspection发送请求以获取token的信息确认其是否有效。
因此,您不需要对code做任何操作,用户从OIDC认证服务返回后,用户将访问这样的地址xxx.com/xxx?code=xxx&state=xxx,而APISIX会自动处理后续步骤,如换取token和放行请求等。
还是不行,贴一下我的思路,您帮我看下:
- 建一个服务,提供OAuth的访问
{
"name": "oauth",
"upstream_id": "386066599586562754",
"plugins": {
"openid-connect": {
"client_id": "login-client",
"client_secret": "{noop}openid-connect",
"disable": false,
"discovery": "http://auth-server:9999/.well-known/openid-configuration",
"redirect_uri": "http://lizhouyang.cn:9080/web1/"
}
}
}
- 新建一个路由,将上面的服务关联到路由上。路由配置如下:
{
"uri": "/web1/*",
"name": "web1",
"methods": [
"GET",
"POST",
"PUT",
"DELETE",
"PATCH",
"HEAD",
"OPTIONS",
"CONNECT",
"TRACE"
],
"plugins": {
"proxy-rewrite": {
"regex_uri": [
"^/web1/*",
"/*"
]
}
},
"service_id": "386068954721813186",
"upstream_id": "386066599586562754",
"labels": {
"API_VERSION": "1.0"
},
"status": 1
}
- 浏览器访问`http://lizhouyang.cn:9080/web1/' 出现500错误。不跳转,如下图。域名已经映射到我个人MAC。

PS:补充一些材料
- apisix采用docker部署,采用example文件中的docker-compose.yml,只是加了HOSTS域名映射,修改了端口映射,yml如下:
version: "3"
services:
apisix-dashboard:
image: apache/apisix-dashboard:2.9.0
restart: always
volumes:
- ./dashboard_conf/conf.yaml:/usr/local/apisix-dashboard/conf/conf.yaml
ports:
- "9000:9000"
networks:
apisix:
apisix:
image: apache/apisix:2.10.0-alpine
restart: always
volumes:
- ./apisix_log:/usr/local/apisix/logs
- ./apisix_conf/config.yaml:/usr/local/apisix/conf/config.yaml:ro
depends_on:
- etcd
##network_mode: host
ports:
- "9080:9080/tcp"
- "9081:9091/tcp"
- "9082:9443/tcp"
- "9083:9092/tcp"
extra_hosts:
- "auth-server:192.168.65.2"
networks:
apisix:
etcd:
image: bitnami/etcd:3.4.15
restart: always
volumes:
- etcd_data:/bitnami/etcd
environment:
ETCD_ENABLE_V2: "true"
ALLOW_NONE_AUTHENTICATION: "yes"
ETCD_ADVERTISE_CLIENT_URLS: "http://0.0.0.0:2379"
ETCD_LISTEN_CLIENT_URLS: "http://0.0.0.0:2379"
ports:
- "9084:2379/tcp"
networks:
apisix:
web1:
image: nginx:1.19.0-alpine
restart: always
volumes:
- ./upstream/web1.conf:/etc/nginx/nginx.conf
ports:
- "9085:80/tcp"
environment:
- NGINX_PORT=80
networks:
apisix:
web2:
image: nginx:1.19.0-alpine
restart: always
volumes:
- ./upstream/web2.conf:/etc/nginx/nginx.conf
ports:
- "9086:80/tcp"
environment:
- NGINX_PORT=80
networks:
apisix:
prometheus:
image: prom/prometheus:v2.25.0
restart: always
volumes:
- ./prometheus_conf/prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9087:9090"
networks:
apisix:
grafana:
image: grafana/grafana:7.3.7
restart: always
ports:
- "9088:3000"
volumes:
- "./grafana_conf/provisioning:/etc/grafana/provisioning"
- "./grafana_conf/dashboards:/var/lib/grafana/dashboards"
- "./grafana_conf/config/grafana.ini:/etc/grafana/grafana.ini"
networks:
apisix:
networks:
apisix:
driver: bridge
volumes:
etcd_data:
driver: local
- 在容器中可以访问.well-known文件,如下:

- OpenID服务采用 spring-authorization-server服务,回调地址加了
http://lizhouyang.cn:9080/web1/。自己写的client端可以正常登录。
经试验,发现如果采用"http://lizhouyang.cn:9080/web1/"路径,就会出现上面的问题。如果采用”http://lizhouyang.cn:9080/web1/1“就正常。是不是路由配置的问题?
Hi @leslie-tsang, do you have some hints on this?
"proxy-rewrite": { "regex_uri": [ "^/web1/*", "/*" ]
It looks related to this plugin, can you remove this plugin and try again? From the problem you describe, it looks like this plugin is a distraction.
thanks,i think about it