我在处理范围(访问级别,OAuth2)和Scala时遇到问题。 我想有这些用例:
这段代码是我想要的一个例子:
package controllers
import scalaoauth2.provider.OAuth2Provider
import scala.concurrent.ExecutionContext.Implicits.global
class MyController extends GlobalAuthController
// define global scope
implicit val scope = Scope.User
// By default, if no parameter, the scope is the implicit value previously defined
def user = ActionWithAuth { request =>
Ok("hello user or admin")
}
def admin = ActionWithAuth(Scope.Admin) {
Ok("hello admin")
}
}
我使用nulab/scala-oauth2-provider
库。根据文件,我创造了这个:
trait OAuth2ActionComposition {
implicit val executionContext: ExecutionContext
def ActionWithAuth[U](implicit scope: Scope): ActionBuilder[({type λ[A] = AuthInfoRequest[A, User]})#λ] = {
Logger.info("Scope : " + scope.toString)
AuthorizedActionFunction(new Users(scope)) compose Action
}
}
object OAuth2ActionComposition extends OAuth2ActionComposition {
implicit val executionContext: ExecutionContext = play.api.libs.concurrent.Execution.defaultContext
}
但是使用该代码我收到了编译错误:第def user = ActionWithAuth { request =>
行see this image
当我显式传递隐式参数(例如def user = ActionWithAuth(scope) { request =>
)时,它可以正常工作。 WTF!但这并不方便。
我无法弄清楚为什么会出现此错误。我尝试了其他一些方法但没有成功。
答案 0 :(得分:0)
我"修复"感谢Silhouette内置com.mohiva.play.silhouette.api.Authorization
。
见http://silhouette.mohiva.com/docs/authorization。
的确,感谢Silhouette,可以定义授权。例如。 :
case class WithProvider(provider: String) extends Authorization[User, CookieAuthenticator] {
def isAuthorized[B](user: User, authenticator: CookieAuthenticator)(
implicit request: Request[B], messages: Messages) = {
Future.successful(user.loginInfo.providerID == provider)
}
}
然后,使用此"过滤器:
def myAction = SecuredAction(WithProvider("twitter")) { implicit request =>
// do something here
}
这些过滤器可以轻松组合。