无法使用密钥保护python rest api服务

2024-10-02 12:33:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在docker(192.168.99.100:8080)中运行了key斗篷服务器,并且在本地运行python flask oidc flask应用程序(本地主机:5000)即使在获得访问令牌之后,我也无法访问受保护的restapi。有人试过这个密码吗。如果是的话,请帮我解决这个问题。谢谢

this is my keycloak client using docker jboss/keycloak image

this is my newuser under the new realm

下面是我的申请表

应用程序类型

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>'

客户_机密.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"
}
}

当我在web浏览器中启动flask应用程序时
我点击登录链接
接下来,它会提示输入用户详细信息(在我的新领域下创建的用户)
几秒钟后,它会将我重定向到错误页
http://localhost:5000/oidc_callback?state=eyJjc3JmX3Rva2VuIjogIkZZbEpqb3ZHblZoUkhEbmJsdXhEVW
上面说

httplib2。socks.HTTPError错误

httplib2。socks.HTTPError错误:(504,b‘网关超时’)

而且它还重定向到/oidc\u回调,这在任何地方都没有提到
任何帮助都将不胜感激


Tags: keyclientauthconfigapphttpflasksecret
1条回答
网友
1楼 · 发布于 2024-10-02 12:33:27

出现此问题的原因是正在运行的keydove服务器 在docker中(192.168.99.100
无法访问本地运行的flask应用程序服务器(本地主机

通过创建docker compose文件,更好地在docker中作为服务运行

相关问题 更多 >

    热门问题