我正在尝试使用Uploadify,一个Ajax文件上传器Play Framework。
Uploadify使用Flash对象与服务器通信...因此默认情况下它不会使用Play Cookie。我想要正确验证我的用户,所以我需要让uploadify发送一些自己的cookie。
有没有人有两个合作的工作示例,或者,失败了,一些指针?
答案 0 :(得分:0)
uploadify有一个名为scriptData的选项,您可以使用该选项发送您的authenticntityToken:
#{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>
答案 1 :(得分:0)
好吧,如果您正在使用httpOnly
配置(and you should!),那么就无法将Play的原生身份验证Cookie传递给uploadify。
我做的是:
<强> 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的常见问题(我可能会对此有所误解)。您可以在签名中添加到期日期以限制损坏。