问题
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