Current Request/User details in Models in Scala Play! 2.5

喜欢而已 提交于 2019-12-11 13:39:46

问题


I would like to have access to the current user somewhere deep in my models of my Play app, for things like setting the author, checking that the user can actually save this type, etc.

Ideally, what I would like to use is Guice's @RequestScoped to inject the same UserIdentity across my request, wherever I need it. However, as far as I can tell, the Play! Framework only supports @Singleton and no-scope. So either we'd get the same UserIdentity injected across requests or a different one for every model/util we requested. Both are no-gos for obvious reasons.

Is there some way to utilise this behaviour in Play 2.5?

Other things I have tried

I've tried using a combination of Play's session and cache. But the problem I have is that session is immutable, so I can't add anything to it to reuse in that same request.

I've looked at a bunch of auth frameworks but they all seem to focus on securing actions, not providing me with a current User object.


回答1:


Check out my answer to a question on redirecting requests, where I given an example of getting the current user in each request.

In this case the authorization key is handed out on login and the client passes it in on every request thereafter.



来源:https://stackoverflow.com/questions/37086994/current-request-user-details-in-models-in-scala-play-2-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!