duanqinjiao5244 2017-04-24 11:15
浏览 70
已采纳

将用户存储在上下文中还是其他地方?

I am having a hard time finding out if I am thinking about things in the best way. I want to know what to do with a user after they have been authenticated through some type of middleware, either BasicAuth or JWT.

Should I store all the relevant user fields in the context as a value? If I store them in the context then do I need a special function to pull them out? (which would use a user defined contextKey type to retrieve and convert its type back to the proper struct)

I can come up with something that works, but IDK if it is the most reasonable way to go about it.

  • 写回答

1条回答 默认 最新

  • doushang4293 2017-04-24 19:38
    关注

    Yes, context.Context is specifically for request scoped variables, among other things. (Notice, per request, so not suitable for storing data you mean to access between requests; for that you should consider some sort of session mechanism)

    the context.Value(key interface{}) interface{} interface function is how you retrieve values back out of the context.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源