gpt4 book ai didi

playframework - 在 playframework 中提供带有身份验证的静态文件

转载 作者:行者123 更新时间:2023-12-04 05:33:33 25 4
gpt4 key购买 nike

是否有一种简单/内置的方法让 Controller 检查连接是否被授权访问静态文件而不是服务器(使用数据库查找),然后在需要时提供访问权限。

有大视频文件,我想 a) 检查是否允许用户访问该文件,以及 b) 如果用户被授权,我想记录该视频已被观看。

最佳答案

有两种方法可以做到这一点。

第一个也是最优雅的方法是使用 Play 插件功能。

您可以在 How to extend the playframework? 找到更多信息这是javadoc: http://www.playframework.org/documentation/api/1.2.3/play/PlayPlugin.html

第二个是从路由提供文件

如果文件是基于路由/ Controller 的,那么您可以像保护任何其他 Controller 一样保护它们。如果我错了,请有人纠正我。

我会做类似的事情(注意这段代码还没有经过测试):

@With(Secure.class)保护

public static void getImage() {
File file = new File( "~/image.png" );
response.contentType = "image/png";
renderBinary( file );
}

然后是路线

GET     /static/image1    Application.getImage

或者你可以让它更优雅。

GET     /static/image/{fileName}   Application.getImage
GET /static/image/{id} Application.getImageById

   public static void getImage(String fileName) {
File file = new File( "~/" + fileName );
response.contentType = "image/png";
renderBinary( file );
}

或者更进一步。

public static void getImage( String fileName, String username ) {
File file = new File( "~/" + fileName );

if ( file.exists() ) {
User user = User.find( "byName", username ).fetch();
user.watch = true;
user.save();

response.contentType = "image/png";
renderBinary( file );
}
}

显然,如果该文件不存在,您将需要进行一些尝试和捕获。

祝你好运。

关于playframework - 在 playframework 中提供带有身份验证的静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7906709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com