如何在 PlayFramework 中使用 Scala Secure Trait?
我正在尝试使用 Play Framework 在 Scala 中构建 Web 应用程序。在 Java 中使用 Play Framework 时,我可以使用 安全模块 对页面进行身份验证需要登录的。这是许多 Web 应用程序中的常见问题,我想为我的 Web 应用程序使用通用解决方案。
我尝试遵循使用特征混合控制器与安全特征例如,但我的特质甚至无法编译,而且我不明白出了什么问题。
我已经从示例中创建了特征并将其保存在 mysite\app\Secure.scala
上:
package controllers
import play._
import play.mvc._
trait Secure {
self:Controller =>
@Before checkSecurity = {
session("username") match {
case Some(username) => renderArgs += "user" -> User(username)
Continue
case None => Action(Authentication.login)
}
}
def connectedUser = renderArgs("user").get
}
然后我在简单的 mysite\app\MySecretController.scala
中使用安全特征:
package controllers
import play._
import play.mvc._
object MySecretController extends Controller with Secure {
def index = <h1>Hello</h1>
}
但是当访问该页面时,我收到编译错误:
The file /app/Secure.scala could not be compiled. Error raised is : expected
start of definition
在这一行:
@Before ↓checkSecurity = {
我还创建了一个简单的 mysite/app/User
类:
package controllers
class User (name: String){
}
关于如何解决这个问题有什么建议吗?
更新
按照 Felipe 的建议添加 def
后。我在以下位置收到另一个错误 not found: value User
:
case Some(username) => renderArgs += "user" -> ↓User(username)
I'm trying to build a web application in Scala using Play Framework. When using Play Framework in Java I can use the Secure module to do authentication for pages that require logins. This is a common problem in many web applications, and I would like to use a general solution for my web application.
I have tried to follow Mixing controllers using Traits with a Secure trait example, but my trait doesn't even compile, and I don't understand what's wrong.
I have created the trait from the example and saved it on mysite\app\Secure.scala
:
package controllers
import play._
import play.mvc._
trait Secure {
self:Controller =>
@Before checkSecurity = {
session("username") match {
case Some(username) => renderArgs += "user" -> User(username)
Continue
case None => Action(Authentication.login)
}
}
def connectedUser = renderArgs("user").get
}
Then I use the Secure trait in a simple mysite\app\MySecretController.scala
:
package controllers
import play._
import play.mvc._
object MySecretController extends Controller with Secure {
def index = <h1>Hello</h1>
}
But when visiting the page I get Compilation error:
The file /app/Secure.scala could not be compiled. Error raised is : expected
start of definition
on this line:
@Before ↓checkSecurity = {
I also created a simple mysite/app/User
class:
package controllers
class User (name: String){
}
Any suggestion on how I can solve this?
UPDATE
After adding def
as suggested by Felipe. I get another error not found: value User
on:
case Some(username) => renderArgs += "user" -> ↓User(username)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在定义方法之前必须使用关键字 def。
应该解决这个问题。
You must use keyword def before defining an method.
should fix this.
您可能会收到“未找到:值用户”错误,因为未找到其定义。您需要确保导入该包。只需执行:
后。
导入所有模型
You're probably getting the 'not found: value User' error because its definition is not found. You need to make sure you import the package. Just do:
after
To import all models.