Scala Play中的模型中的当前请求/用户详细信息! 2.5 [英] Current Request/User details in Models in Scala Play! 2.5

查看:102
本文介绍了Scala Play中的模型中的当前请求/用户详细信息! 2.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Play应用程序模型的深处访问当前用户,例如设置作者,检查用户是否可以保存此类型等.

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.

理想情况下,我想使用的是Guice的@RequestScoped,可以在我需要的任何地方注入相同的UserIdentity.但是,据我所知,Play!框架仅支持@Singleton和无作用域.因此,要么在请求中注入相同的UserIdentity,要么为请求的每种模型/实用程序都注入不同的UserIdentity.出于明显的原因,两个都没有执行.

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.

在Play 2.5中有某种方法可以利用这种行为吗?

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

我尝试过的其他事情

我尝试将Play的会话和缓存结合使用.但是我的问题是会话是不可变的,因此我无法在会话中添加任何内容以在同一请求中重复使用.

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.

我看过一堆auth框架,但是它们似乎都集中在保护操作上,而不是向我提供当前的User对象.

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.

推荐答案

查看

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.

这篇关于Scala Play中的模型中的当前请求/用户详细信息! 2.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆