Spray Authentication
在Spray中,如果需要对REST API添加认证,可以使用Spray提供的Authenticate功能。本质上,Authenticate属于安全指令(Security Directive)提供的功能。它的接口定义本质上为:
def authenticate[T](auth: => Future[Authentication[T]])(implicit executor: ExecutionContext): Directive1[T]
def authenticate[T](auth: ContextAuthenticator[T])(implicit executor: ExecutionContext): Directive1[T]
Spray使用了Magnet Pattern,使其可以编写出更符合DSL风格的API。所以我们看到的authenticate()方法的实现实际为:
trait SecurityDirectives extends scala.AnyRef {
def authenticate[T](magnet : spray.routing.directives.AuthMagnet[T]) : spray.routing.Directive1[T] = ???
}
正是因为运用了Magnet Pattern,我们可以直接在Path中通过authenticate添加认证功能,例如:
def myUserPassAuthenticator(userPass: Option[UserPass]): Future[Option[String]] =
Future {
if (userPass.exists(up => up.user == "John" && up.pass == "p4ssw0rd")) Some("John")
else None
}
val customerRoute =
path("customers") {
authenticate(BasicAuth(myUserPassAuthenticator _, realm = "admin area")) { user =>
get {
handleRequest {
AllCustomers
}
} ~ post {
entity(as[Customer]) {
customer =>
handleRequest {
CreateCustomer(customer.email, customer.name)
}
}
}
}
}
Spray Authentication通过BasicHttpAuthenticator类来支持Basic Access Authentication。上面代码片段中的BasicAuth是一个对象,提供了多个构造函数重载。这段代码中传递了两个参数:第一个参数为UserPassAuthenticator类型;第二个参数用于指定认证的realm。
UserPassAuthenticator是一个type,实质为一个函数:
type UserPassAuthenticator[T] = Option[UserPass] => Future[Option[T]]
上面代码中的myUserPassAuthenticator就是自定义的一个UserPassAuthenticator。显然,BasicAuth接收一个函数作为参数,使得我们可以更容易自定义。若要通过认证,我们可以创建BasicHttpCredentials对象,将其加入到authorization header中。Spray也支持配置的形式管理用户信息,具体内容可参见Spray的官方文档Authentication。
JMeter测试
我用JMeter来测试这个具有Authentication的REST API。由于具有认证功能,因而,在JMeter中需要添加Http Authorization Manager。Http Authorization Manager是Config Element,添加后,需要配置认证信息,包括Base URL、Username、Password、Realm等。如下图所示:
注意,在配置Base URL时,应该设置为完整的URL(当然,也可以使用JMeter的变量)。
如果为了验证执行是否成功,建议添加View Result Tree这个Listener,因为它给出的结果信息中包括了Sampler result、Request与Response Data等信息,这样有利于我们甄别测试的Http Request是否正确,如果错误,是什么原因导致的。