How to use Scala Protected Tag in PlayFramework?

I am trying to create a web application in Scala using the Play Framework . When using the Play Framework in Java, I can use the Secure Module for authentication for pages that require logins. This is a common problem in many web applications, and I would like to use a common solution for my web application.

I tried mixing Controllers using Traits with the Secure example, but my trait doesn't even compile, and I don’t know, I don’t understand what happened.

I created an attribute from an example and saved it to 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 property in a simple one mysite\app\MySecretController.scala:

package controllers

import play._
import play.mvc._

object MySecretController extends Controller with Secure {
    def index = <h1>Hello</h1>
}

But when I visit the page, I get a compilation error:

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){

}

, ?


def, . not found: value User on:

case Some(username) => renderArgs += "user" -> ↓User(username)
+5
2

def .

@Before def checkSecurity = {

.

+6

, "not found: value User", . , . :

import models._

import play.mvc._

.

0

All Articles