无法使用 keycloak 保护 python flask rest api 服务
not able to protect the python flask rest api service using keycloak
我在 docker (192.168.99.100:8080) 中有 keycloak 服务器 运行 和 python flask-oidc flask 应用程序 运行 本地( localhost:5000 ) 即使在获得 access_token 后,我也无法访问受保护的 Rest Api。有没有人试过这段代码。如果是这样,请帮助我解决这个问题。谢谢
this is my keycloak client using docker jboss/keycloak image
this is my newuser under the new realm
下面是我的 flask-application
app.py
from flask import Flask, g
from flask_oidc import OpenIDConnect
import requests
secret_key = os.urandom(24).hex()
print(secret_key)
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__)
app.config["OIDC_CLIENT_SECRETS"]="client_secrets.json"
app.config["OIDC_COOKIE_SECURE"]=False
app.config["OIDC_SCOPES"]=["openid","email","profile"]
app.config["SECRET_KEY"]=secret_key
app.config["TESTING"]=True
app.config["DEBUG"] = True
app.config["OIDC_ID_TOKEN_COOKIE_SECURE"]=False
app.config["OIDC_REQUIRED_VERIFIED_EMAIL"]=False
app.config["OIDC_INTROSPECTION_AUTH_METHOD"]='client_secret_post'
app.config["OIDC_USER_INFO_ENABLED"]=True
oidc = OpenIDConnect(app)
@app.route('/')
def hello_world():
if oidc.user_loggedin:
return ('Hello, %s, <a href="/private">See private</a> '
'<a href="/logout">Log out</a>') % \
oidc.user_getfield('preferred_username')
else:
return 'Welcome anonymous, <a href="/private">Log in</a>'
client_secrets.json
{
"web": {
"issuer": "http://192.168.99.100:8080/auth/realms/kariga",
"auth_uri": "http://192.168.99.100:8080/auth/realms/kariga/protocol/openid-connect/auth",
"client_id": "flask-app",
"client_secret": "eb11741d-3cb5-4457-8ff5-0202c6d6b250",
"redirect_uris": [
"http://localhost:5000/"
],
"userinfo_uri": "http://192.168.99.100:8080/auth/realms/kariga/protocol/openid-connect/userinfo",
"token_uri": "http://192.168.99.100:8080/auth/realms/kariga/protocol/openid-connect/token",
"token_introspection_uri": "http://192.168.99.100:8080/auth/realms/kariga/protocol/openid-connect/token/introspect"
}
}
当我在网络浏览器中启动 flask-app 时
我点击 登录 link
接下来它提示输入用户详细信息(在我的新领域下创建的用户)
它需要几秒钟然后将我重定向到错误页面
http://localhost:5000/oidc_callback?state=eyJjc3JmX3Rva2VuIjogIkZZbEpqb3ZHblZoUkhEbmJsdXhEVW
那说
httplib2.socks.HTTPError
httplib2.socks.HTTPError: (504, b'网关超时')
而且它正在重定向到 /oidc_callback,这在任何地方都没有提到
任何帮助将不胜感激
出现问题是因为 keycloak 服务器是 运行ning
在 docker(192.168.99.100)
无法访问本地 运行ning 的 flask 应用程序 服务器(localhost)
比 运行 更好,因为 services in docker 通过创建 docker-撰写文件
我在 docker (192.168.99.100:8080) 中有 keycloak 服务器 运行 和 python flask-oidc flask 应用程序 运行 本地( localhost:5000 ) 即使在获得 access_token 后,我也无法访问受保护的 Rest Api。有没有人试过这段代码。如果是这样,请帮助我解决这个问题。谢谢
this is my keycloak client using docker jboss/keycloak image
this is my newuser under the new realm
下面是我的 flask-application
app.py
from flask import Flask, g
from flask_oidc import OpenIDConnect
import requests
secret_key = os.urandom(24).hex()
print(secret_key)
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__)
app.config["OIDC_CLIENT_SECRETS"]="client_secrets.json"
app.config["OIDC_COOKIE_SECURE"]=False
app.config["OIDC_SCOPES"]=["openid","email","profile"]
app.config["SECRET_KEY"]=secret_key
app.config["TESTING"]=True
app.config["DEBUG"] = True
app.config["OIDC_ID_TOKEN_COOKIE_SECURE"]=False
app.config["OIDC_REQUIRED_VERIFIED_EMAIL"]=False
app.config["OIDC_INTROSPECTION_AUTH_METHOD"]='client_secret_post'
app.config["OIDC_USER_INFO_ENABLED"]=True
oidc = OpenIDConnect(app)
@app.route('/')
def hello_world():
if oidc.user_loggedin:
return ('Hello, %s, <a href="/private">See private</a> '
'<a href="/logout">Log out</a>') % \
oidc.user_getfield('preferred_username')
else:
return 'Welcome anonymous, <a href="/private">Log in</a>'
client_secrets.json
{
"web": {
"issuer": "http://192.168.99.100:8080/auth/realms/kariga",
"auth_uri": "http://192.168.99.100:8080/auth/realms/kariga/protocol/openid-connect/auth",
"client_id": "flask-app",
"client_secret": "eb11741d-3cb5-4457-8ff5-0202c6d6b250",
"redirect_uris": [
"http://localhost:5000/"
],
"userinfo_uri": "http://192.168.99.100:8080/auth/realms/kariga/protocol/openid-connect/userinfo",
"token_uri": "http://192.168.99.100:8080/auth/realms/kariga/protocol/openid-connect/token",
"token_introspection_uri": "http://192.168.99.100:8080/auth/realms/kariga/protocol/openid-connect/token/introspect"
}
}
当我在网络浏览器中启动 flask-app 时
我点击 登录 link
接下来它提示输入用户详细信息(在我的新领域下创建的用户)
它需要几秒钟然后将我重定向到错误页面
http://localhost:5000/oidc_callback?state=eyJjc3JmX3Rva2VuIjogIkZZbEpqb3ZHblZoUkhEbmJsdXhEVW
那说
httplib2.socks.HTTPError
httplib2.socks.HTTPError: (504, b'网关超时')
而且它正在重定向到 /oidc_callback,这在任何地方都没有提到
任何帮助将不胜感激
出现问题是因为 keycloak 服务器是 运行ning
在 docker(192.168.99.100)
无法访问本地 运行ning 的 flask 应用程序 服务器(localhost)
比 运行 更好,因为 services in docker 通过创建 docker-撰写文件