2017-07-27 58 views
1

我有我在玩2.5應用程序的大多數REST端點使用ActionBuilder:如何保護與授權ActionBuilder靜態文件在播放2.5框架

def IfAuthorized(auths: Authorizations*): ActionBuilder[AuthRequestWithAuthorization] = { 
    new MyAuthActionBuilder(auths.toList) 
    } 

,我可以爲使用:

def status = IfAuthorized(Editor).async { implicit authReq => 
     //eventually return Result 
    } 

現在我想要在同一個ActionBuilder中包裝靜態文件請求。目前我使用這個oneliner,這使我成爲HTML,JS,CSS等,全部返還/緩存沒有我的干擾:

GET   /ui/*file     controllers.Assets.versioned(path="/public", file: Asset) 

我不知道如何在ActionBuilder推。一個可憐的試圖返回初始HTML頁面包括:

def index()= IfAuthorized(Editor) { authReq => 
    val contentStream = this.getClass.getResourceAsStream("/public/index.html") 
    Ok.chunked(Enumerator.fromStream(contentStream)).as(HTML) 
    } 

Action只能夠返回一個特定的HTML文件,而我想從1條路線返回的所有靜態資產,通過我的自定義保護ActionBuilder

回答

1

您需要實現自己的資產控制器:路由

GET /auth/*file controllers.AuthAssets.at(path="/public", file: String) 

將指向以下端點:

class AuthAssets extends Controller { 
    def at(path: String, file: String) = IfAuthorized(Editor) { authReq => 
     val contentStream = this.getClass.getResourceAsStream(path + file) 
     Ok.chunked(Enumerator.fromStream(contentStream)) 
    } 
}