用来 authenticate 的 Directives
2015-09-10 17:16
459 查看
authenticate 这个Directives 使用了http的
WWW-Authenticate
来实现认证的功能,这个没怎用,百度了下
http://blog.163.com/hongshaoguoguo@126/blog/static/18046981201322384241640/
创建authenticate ,authenticate 跟route构建是不同个线程的
定义跟Directives 的使用一样
注意的是这里
![](http://img.blog.csdn.net/20150910164544084?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
抓包分析
由于wireshark无法抓取本地连接,我们修改url,把他发给百度,让后截取包分析
![](http://img.blog.csdn.net/20150910171529192?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
重要是这句
Authorization: Basic YWRtaW46YWRtaW4=
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.IO import akka.util.Timeout import spray.can.Http import spray.routing._ import spray.http._ import spray.routing.authentication.{UserPass, BasicAuth} import scala.concurrent.duration._ import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global object 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=
相关文章推荐
- 汇总查询到的数据
- android 监听网络变化 多处做出监听处理
- Android 国际化 语言适配
- Http请求框架 okHttp 简单使用介绍
- ReactJS State
- Bootstrap模态框(modal)垂直居中
- POJ 2048 Longge's problem (欧拉函数 积性函数)
- JSP&Servlet面试题
- synchronized
- spring AOP面向切面编程学习笔记
- 服务器服务开发重中之重
- 在代码片段中添加时间间隔
- 最大熵模型中的数学推导
- 判断一棵二叉树是否为平衡二叉树(待续!!!!)
- Java总结篇系列:Java多线程(二)
- 数据库ER图 PowerDesigner
- Spring+Quartz实现定时从FTP服务器读取文件并存入Oracel数据库
- Linux网络编程--select()和pselect()函数
- MVC
- 编码转换