【问题标题】:How to pass the session cookie to Play via Uploadify?如何通过 Uploadify 将会话 cookie 传递给 Play?
【发布时间】:2012-02-17 19:12:32
【问题描述】:

我正在尝试将 Ajax 文件上传器 UploadifyPlay Framework 一起使用。

Uploadify 使用 Flash 对象与服务器通信……所以默认情况下它不会使用 Play cookie。我想正确地验证我的用户,所以我需要让 uploadify 自己发送一些 cookie。

有没有人有两个一起工作的工作示例,或者,如果失败了,有一些指针?

【问题讨论】:

标签: java playframework uploadify


【解决方案1】:

uploadify 有一个名为 scriptData 的选项,您可以使用它来发送您的 authentityToken:

#{authenticityToken /}

<script>
var token = $('#input[name=authenticityToken]').val();
$('#file_upload').uploadify({
  'uploader'    : '/uploadify/uploadify.swf',
  'script'      : '/uploadify/uploadify.php',
  'cancelImg'   : '/uploadify/cancel.png',
  'folder'      : '/uploads',
  'scriptData'  : {'authenticyToken': token}
});
</script>

【讨论】:

  • 我知道 scriptData。所以你是说Play已经生成了一个名为authenticityToken的隐藏输入框?我认为会话是通过 cookie 处理的。
  • 啊,你的意思是使用playframework.org/documentation/current/tags#authenticityToken。还值得一提的是内置的#{form} 标签会自动添加它。 playframework.org/documentation/current/tags#form
  • 这还不够,至少对于 Secure 模块来说还不够(例如,Secure.checkAccess() 需要 username 才能在会话中)。也许我也应该复制整个会话 cookie?不知道我会怎么做。
  • 把你需要的任何东西放到scriptData中。 uploadify 使用 flash 上传文件。不幸的是,flash 无法访问浏览器会话,至少它不会通过网络上的 cookie 发送。
【解决方案2】:

好吧,如果您使用的是httpOnly 配置(and you should!),则无法通过 Play 的本机身份验证 cookie 进行上传。

我所做的是:

1.不使用@With(Secure.class) 保护图像控制器,而是使用before method

@Before(unless = "uploadPost")
public static void before() throws Throwable {
    Secure.checkAccess();
}

2。传递来自控制器的两个参数,该控制器呈现托管 uploadify 插件的页面:userId 和 signedUserId

String userIdSignature = Crypto.sign(Long.toString(user.id));
render(..., user.id, userIdSignature);

3.把这两个参数传给uploadify,传给uploadPost方法

public static void uploadPost(Upload upload, long userId, String userIdSignature) {
    assertEquals(userIdSignature, Crypto.sign(Long.toString(userId)),
        "Failed to authenticate user ID " + userId);

如果出于某种原因您不希望客户端知道其用户 ID,则签名的替代方法是加密用户 ID。

请注意,使用此方法您仍然会受到重放攻击,但我认为这是 Play 的普遍问题(我可能会误会)。您可以在签名中添加到期日期以限制损坏。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-19
    • 2023-04-01
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多