我试图在与OAuth2服务器相同的Docker网络中运行测试OAuth2客户端。这个想法是允许对整个应用程序进行测试,而不必运行外部OAuth2客户端。
我遇到的问题是,传递到OAuth2客户端的OAuth2服务器端点仅在本地计算机上的Docker网络外部可用(我正在使用一个简单的nginx代理来处理此问题,请参见下文)。因此,当客户端在调用回调时尝试获取令牌时,它无法解析主机名(因为客户端容器上下文中的“ localhost”是指该容器的本地网络,而在我的浏览器中是“ localhost”指Docker网络本身)。理想情况下,我想在开发过程中继续使用nginx作为更好的主机名的代理。
为了阐明这一点,目前是该过程的工作方式:
这是我的docker-compose,为简洁起见,删除了不相关的部分:
version: '3'
services:
hydra:
image: oryd/hydra:v1.0.0-beta.9
depends_on:
- hydra-migrate
command:
serve all --dangerous-force-http
environment:
- OAUTH2_ISSUER_URL=http://auth.localhost/
- OAUTH2_CONSENT_URL=http://auth.localhost/consent
- OAUTH2_LOGIN_URL=http://auth.localhost/auth/login
- DATABASE_URL=postgres://hydra@postgres:5432/hydra?sslmode=disable
- SYSTEM_SECRET=youReallyNeedToChangeThis
- OAUTH2_SHARE_ERROR_DEBUG=1
- OIDC_SUBJECT_TYPES_SUPPORTED=public,pairwise
- OIDC_SUBJECT_TYPE_PAIRWISE_SALT=youReallyNeedToChangeThis
nginx:
image: nginx
ports:
- "80:80"
volumes:
- ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- hydra
- oauth2-test
oauth2-test:
build: https://github.com/chatlogs/oauth2-simple-client.git
environment:
- APP_URL=http://app.localhost
- OAUTH2_URL=http://auth.localhost
- OAUTH2_CLIENT_ID=test
- OAUTH2_CLIENT_SECRET=secret
depends_on:
- hydra-create-client
这是我的nginx.conf:
events {
worker_connections 1024;
}
http {
# Proxy ChatLogs Auth and Hydra OAuth server
server {
listen 80;
server_name auth.localhost;
location ^~ / {
proxy_pass http://chatlogs-auth:3000;
}
location ^~ /oauth2 {
proxy_pass http://hydra:4444/oauth2;
}
}
server {
listen 80;
server_name app.localhost;
location / {
proxy_pass http://oauth2-test:3000;
}
}
}
这是oauth2-test显示的错误:
oauth2-test_1 | { Error: getaddrinfo ENOTFOUND auth.localhost auth.localhost:80
oauth2-test_1 | at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)
oauth2-test_1 | errno: 'ENOTFOUND',
oauth2-test_1 | code: 'ENOTFOUND',
oauth2-test_1 | syscall: 'getaddrinfo',
oauth2-test_1 | hostname: 'auth.localhost',
oauth2-test_1 | host: 'auth.localhost',
oauth2-test_1 | port: 80,
oauth2-test_1 | trace:
oauth2-test_1 | [ { method: 'POST', url: 'http://auth.localhost/oauth2/token' } ],
oauth2-test_1 | isBoom: true,
oauth2-test_1 | isServer: true,
oauth2-test_1 | data: null,
oauth2-test_1 | output:
oauth2-test_1 | { statusCode: 502,
oauth2-test_1 | payload:
oauth2-test_1 | { message:
oauth2-test_1 | 'Client request error: getaddrinfo ENOTFOUND auth.localhost auth.localhost:80',
oauth2-test_1 | statusCode: 502,
oauth2-test_1 | error: 'Bad Gateway' },
oauth2-test_1 | headers: {} },
oauth2-test_1 | reformat: [Function] }
感谢您的帮助!