用来 authenticate 的 Directives
来源:互联网 发布:mysql count查询太慢 编辑:程序博客网 时间:2024/06/13 07:54
authenticate 这个Directives 使用了http的
WWW-Authenticate
来实现认证的功能,这个没怎用,百度了下
http://blog.163.com/hongshaoguoguo@126/blog/static/18046981201322384241640/
创建authenticate ,authenticate 跟route构建是不同个线程的
def authenticator(userPass: Option[UserPass]): Future[Option[String]] = Future { //检查用户 if (userPass.exists(up => up.user == "admin" && up.pass == "admin")) Some(userPass.get.user) else None }
val route = sealRoute { path("secured") { authenticate(BasicAuth(authenticator _, realm = "secure site")) { userName => complete(s"The user is '$userName'") } } }
定义跟Directives 的使用一样
注意的是这里
/** * "Seals" a route by wrapping it with exception handling and rejection conversion. */ def sealRoute(route: Route)(implicit eh: ExceptionHandler, rh: RejectionHandler): Route = (handleExceptions(eh) & handleRejections(sealRejectionHandler(rh)))(route)整个代码
import akka.actor.{Props, ActorSystem}import akka.io.IOimport akka.util.Timeoutimport spray.can.Httpimport spray.routing._import spray.http._import spray.routing.authentication.{UserPass, BasicAuth}import scala.concurrent.duration._import scala.concurrent.Futureimport scala.concurrent.ExecutionContext.Implicits.globalobject ActorTest5 extends App{ implicit val system = ActorSystem("mySystem") val se = system.actorOf(Props[MyService2]) implicit val timeout = Timeout(5.seconds) IO(Http) ! Http.Bind(se,interface = "localhost",port=8080)}class MyService2 extends HttpServiceActor { def authenticator(userPass: Option[UserPass]): Future[Option[String]] = Future { //检查用户 if (userPass.exists(up => up.user == "admin" && up.pass == "admin")) Some(userPass.get.user) else None } val route = sealRoute { path("secured") { authenticate(BasicAuth(authenticator _, realm = "secure site")) { userName => complete(s"The user is '$userName'") } } } def receive = runRoute(route)}
抓包分析
由于wireshark无法抓取本地连接,我们修改url,把他发给百度,让后截取包分析
重要是这句
Authorization: Basic YWRtaW46YWRtaW4=
0 0
- 用来 authenticate 的 Directives
- Authenticate
- 对指令(directives)的认识
- vetur插件提示 'v-for' directives require 'v-bind:key' directives.错误的解决办法
- Correlation Directives -> Directives
- ARM汇编器armasm支持的 Directives完整列表
- Django的login和authenticate模块的使用
- 用来学习的网站
- FormsAuthentication.Authenticate()方法总是返回false的原因
- FormsAuthentication.Authenticate()方法延长保存时间的方法
- django中 authenticate返回None的情况解决办法
- Preprocessor Directives
- References & Directives
- Pragma Directives
- JSP Directives
- #Pragma Directives
- arm directives
- CFI directives
- 使用Gradle构建Android应用的渠道包
- android.text.Html源码解析-再也不用担心图文混排什么的了
- SVN 冲突
- Android的签名与校验机制
- 汉字转拼音
- 用来 authenticate 的 Directives
- UVA - 1395 Slim Span(最小生成树最大边权和最小边权之差最小)
- 命名空间
- position定位和添加阴影
- SharedPreference
- 简单利用SharedPreferences实现直接登录
- android 调用相机拍照 伪闪退
- 热备,冷备,云备的区别
- RelativeLayout相对布局常用属性