前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spray中的Authentication和JMeter测试

Spray中的Authentication和JMeter测试

作者头像
张逸
发布2018-03-07 16:03:09
1.1K0
发布2018-03-07 16:03:09
举报
文章被收录于专栏:斑斓斑斓斑斓

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是否正确,如果错误,是什么原因导致的。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2015-02-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 逸言 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档