【发布时间】:2022-11-12 12:04:13
【问题描述】:
我遇到了问题,因为我必须在后端创建一个 cookie,稍后我将使用它从前端发送请求。两个应用程序都在同一个域上。 这是https://levelup.gitconnected.com/secure-frontend-authorization-67ae11953723 背后的总体思路。
前端代码 - 向后端发送获取请求 `
@app.get('/')
async def homepage(request: Request, response_class=HTMLResponse):
keycloak_code = 'sksdkssdk'
data = {'code': keycloak_code}
url_post = 'http://127.0.0.1:8002/keycloak_code'
post_token=requests.get(url=url_post, json = data )
return 'Sent'
if __name__ == '__main__':
uvicorn.run(app, host='local.me.me', port=7999,debug=True)
`
后端代码
`
@app.get("/keycloak_code")
def get_tokens(response: Response, data: dict):
code = data['code']
print(code)
....
requests.get(url='http://local.me.me:8002/set')
return True
@app.get("/set")
async def createcookie(response: Response):
r=response.set_cookie(key='tokic3', value='helloworld', httponly=True)
return True
if __name__ == '__main__':
uvicorn.run(app, host='local.me.me', port=8002, log_level="debug")
`
当我打开浏览器并访问“http://local.me.me:8002/set”时,可以看到创建了 Cookie。 但是,当我从我的前端向后端发出相同 URL 的获取请求时,它会收到我在终端中看到的请求,但不会创建 cookie,有人知道我可能做错了什么吗?
我尝试了 FASTapi 文档中的不同实现,但没有一个有类似的用例。
【问题讨论】:
标签: python cookies fastapi cookie-httponly