如何将Python Flask CAS服务器连接到angular应用程序?

2024-06-02 12:33:41 发布

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

我已经启动并运行了python服务器(更准确地说是Flask),它正确地连接到我的CAS服务器,重定向到CAS登录,并在登录时返回正确的用户数据。这是flask服务器的相关代码:

app.secret_key = "SOMESECRETKEY"

cas_client = CASClient(
    version=3,
    service_url="http://localhost:5000/login?next=%2Fprofile",
    server_url='https://django-cas-ng-demo-server.herokuapp.com/cas/'

)

@app.route("/profile")
def profile(method=["GET"]):
    if "username" in session:
        jawt = jwt.encode(session["user_attributes"], app.secret_key, algorithm="HS256")
    return redirect("http://localhost:4200/?accessToken=" + jawt)

数据通过GET方法重定向返回到angular页面(参见上面的代码)。我不希望通过GET方法发送accessToken,而是希望将其存储在localStorage中。我如何从python中做到这一点,但不首先将其作为GET参数发送


Tags: 数据key代码服务器applocalhosthttpurl
1条回答
网友
1楼 · 发布于 2024-06-02 12:33:41

理想情况下,令牌由实现登录功能的任何路由返回,但如果不可能,您可以为此创建特定路由并将其保存在localStorage中:

from flask import jsonify

@app.route("/token")
def token(method=["GET"]):
    if "username" not in session:
        abort(403)
    token = jwt.encode(session["user_attributes"], app.secret_key, algorithm="HS256")
    return jsonify(token=token)

然后创建另一条路线以返回配置文件详细信息:

import requests
from flask import jsonify, request

@app.route("/profile")
def profile(method=["GET"]):
    token = get_token_from_authorization_header(request)
    qs = {"accessToken": token}
    resp = requests.get("http://localhost:4200", params=qs)
    if not resp.ok:
        abort(403)
    return resp.json()

相关问题 更多 >