- 浏览: 2551956 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Spray(8)REST API Project - Auth
6. How to Deal with Auth
My structure for this will be as follow:
/**
* 1 User(1, 'admin', ... , 'admin')
* 2 User(2, 'manager', ..., 'manager')
* 3 User(3, 'customer', ..., 'customer')
*/
caseclass User(id: Option[Long], userName: String, age: Int, userType: UserType.Value, createDate: DateTime, expirationDate: DateTime, password: String)
/**
* 1 Role(1, "admin", "")
* 2 Role(2, "manager", "")
* 3 Role(3, "customer", "")
*/
caseclass Role(id: Option[Long], roleCode: String, description: String)
/**
* 1. RUserRole(1, 1)
* 2. RUserRole(2, 2)
* 3. RUserRole(3, 3)
*/
caseclass RUserRole(roleId : Long, userId : Long)
Since we use basicHttpAuthority, the most important part is to get the data from the header
def getToken(ctx: RequestContext): Option[UserPass] = {
valauthHeader = ctx.request.headers.findByType[Authorization]
valcredentials = authHeader.map { case Authorization(creds) => creds }
credentials.flatMap {
case BasicHttpCredentials(user, pass) => Some(UserPass(user, pass))
case _ => None
}
}
Mock the user data for now
def auth(userName: String, password: String)(implicit session: Session) : Option[User] = {
(userName, password) match {
case ("admin","admin") => Option(User(Some(1), "admin", 100, UserType.ADMIN, new DateTime(), new DateTime(),"admin"))
case ("customer","customer") => Option(User(Some(2), "customer", 100, UserType.CUSTOMER, new DateTime(), new DateTime(),"customer"))
case ("manager","manager") => Option(User(Some(3), "manager", 100, UserType.SELLER, new DateTime(), new DateTime(),"manager"))
case _ => None
}
}
When we use that, it will be like this
trait URLRouterService extends HttpService with UsersAuthenticationDirectives {
authenticate(adminOnly) { user =>
…snip…
authenticate(customerOnly) { user =>
…snip...
authenticate(withRole("manager")) { user =>
The test specification class for this auth thing will be look like this>
package com.sillycat.easysprayrestserver.actor
import org.specs2.mutable.Specification
import spray.testkit.Specs2RouteTest
import spray.http._
import StatusCodes._
import spray.http.BasicHttpCredentials
import spray.http.HttpHeaders.Authorization
import spray.routing.AuthenticationFailedRejection
import spray.routing.AuthenticationRequiredRejection
import spray.routing.HttpService
import spray.routing.RequestContext
import spray.routing.authentication.Authentication
import spray.routing.authentication.UserPass
import spray.util.executionContextFromActorRefFactory
import spray.util.pimpSeq
class URLRouterActorSpec extends Specification with Specs2RouteTest with URLRouterService {
def actorRefFactory = system
"The URLRouterActor" should {
"Anyone can visit this page." in {
Get("/v1/sillycat/resource/all") ~> route ~> check { entityAs[String] must contain("Morning") }
}
"Admin can visit this page." in {
Get("/v1/sillycat/resource/admin-only") ~> addCredentials(BasicHttpCredentials("admin", "admin")) ~> route ~> check { entityAs[String] must contain("Morning") }
}
"No UserName Password can not visit this page." in {
Get("/v1/sillycat/resource/admin-only") ~> route ~> check {
rejection === AuthenticationRequiredRejection("https", "sillycat")
}
}
"Wrong UserName Password can not visit this page." in {
Get("/v1/sillycat/resource/admin-only") ~> addCredentials(BasicHttpCredentials("admin", "asdfadsf")) ~> route ~> check {
rejection === AuthenticationFailedRejection("sillycat")
}
}
}
}
7. How to Work with Logback
come soon…
8. How to work with DB
come soon...
9. How to Work with Actor
come soon…
10. How to do Validation
come soon...
Tips:
1. Log Error
Error Message:
app[ERROR]: May 1, 2013 1:57:26 PM com.mchange.v2.log.MLog <clinit>
app[ERROR]: INFO: MLog clients using java 1.4+ standard logging.
app[ERROR]: May 1, 2013 1:57:27 PM com.mchange.v2.c3p0.C3P0Registry banner
app[ERROR]: INFO: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
Solution:
2. JRebel
The purpose for this is to enable hot reloading.
Follow the document here https://github.com/spray/sbt-revolver
Visit this website to get a free license https://my.jrebel.com/plans/
We can see the information here https://my.jrebel.com/account/my-dashboard
And get the license from here https://my.jrebel.com/account/how-to-activate
Download the latest package named rebel-5.2.2-nosetup.zip
Unzip this file and place in the directory /Users/carl/tool/jrebel
Link it to the working directory
>sudo ln -s /Users/carl/tool/jrebel /opt/jrebel
Run the active command
>cd /opt/jrebel/bin
>./jrebel-config.sh
After that, make sbt-revoler find that plugin
>vi ~/.profile
export JREBEL_PATH=/opt/jrebel/jrebel.jar
>. ~/.profile
That is it. We do not need to re-start our spray server now.
References:
http://www.gtan.com/akka_doc/scala/routing.html
https://github.com/cakesolutions/spray-auth-example
http://spray.io/documentation/spray-routing/
https://github.com/spray/spray/wiki/Authentication-Authorization
https://github.com/spray/spray/wiki/Configuration
https://github.com/spray/spray/wiki
https://github.com/spray/sbt-revolver
https://github.com/spray/spray/blob/master/spray-routing-tests/src/test/scala/spray/routing/SecurityDirectivesSpec.scala
http://doc.akka.io/docs/akka/2.1.0/scala/logging.html
6. How to Deal with Auth
My structure for this will be as follow:
/**
* 1 User(1, 'admin', ... , 'admin')
* 2 User(2, 'manager', ..., 'manager')
* 3 User(3, 'customer', ..., 'customer')
*/
caseclass User(id: Option[Long], userName: String, age: Int, userType: UserType.Value, createDate: DateTime, expirationDate: DateTime, password: String)
/**
* 1 Role(1, "admin", "")
* 2 Role(2, "manager", "")
* 3 Role(3, "customer", "")
*/
caseclass Role(id: Option[Long], roleCode: String, description: String)
/**
* 1. RUserRole(1, 1)
* 2. RUserRole(2, 2)
* 3. RUserRole(3, 3)
*/
caseclass RUserRole(roleId : Long, userId : Long)
Since we use basicHttpAuthority, the most important part is to get the data from the header
def getToken(ctx: RequestContext): Option[UserPass] = {
valauthHeader = ctx.request.headers.findByType[Authorization]
valcredentials = authHeader.map { case Authorization(creds) => creds }
credentials.flatMap {
case BasicHttpCredentials(user, pass) => Some(UserPass(user, pass))
case _ => None
}
}
Mock the user data for now
def auth(userName: String, password: String)(implicit session: Session) : Option[User] = {
(userName, password) match {
case ("admin","admin") => Option(User(Some(1), "admin", 100, UserType.ADMIN, new DateTime(), new DateTime(),"admin"))
case ("customer","customer") => Option(User(Some(2), "customer", 100, UserType.CUSTOMER, new DateTime(), new DateTime(),"customer"))
case ("manager","manager") => Option(User(Some(3), "manager", 100, UserType.SELLER, new DateTime(), new DateTime(),"manager"))
case _ => None
}
}
When we use that, it will be like this
trait URLRouterService extends HttpService with UsersAuthenticationDirectives {
authenticate(adminOnly) { user =>
…snip…
authenticate(customerOnly) { user =>
…snip...
authenticate(withRole("manager")) { user =>
The test specification class for this auth thing will be look like this>
package com.sillycat.easysprayrestserver.actor
import org.specs2.mutable.Specification
import spray.testkit.Specs2RouteTest
import spray.http._
import StatusCodes._
import spray.http.BasicHttpCredentials
import spray.http.HttpHeaders.Authorization
import spray.routing.AuthenticationFailedRejection
import spray.routing.AuthenticationRequiredRejection
import spray.routing.HttpService
import spray.routing.RequestContext
import spray.routing.authentication.Authentication
import spray.routing.authentication.UserPass
import spray.util.executionContextFromActorRefFactory
import spray.util.pimpSeq
class URLRouterActorSpec extends Specification with Specs2RouteTest with URLRouterService {
def actorRefFactory = system
"The URLRouterActor" should {
"Anyone can visit this page." in {
Get("/v1/sillycat/resource/all") ~> route ~> check { entityAs[String] must contain("Morning") }
}
"Admin can visit this page." in {
Get("/v1/sillycat/resource/admin-only") ~> addCredentials(BasicHttpCredentials("admin", "admin")) ~> route ~> check { entityAs[String] must contain("Morning") }
}
"No UserName Password can not visit this page." in {
Get("/v1/sillycat/resource/admin-only") ~> route ~> check {
rejection === AuthenticationRequiredRejection("https", "sillycat")
}
}
"Wrong UserName Password can not visit this page." in {
Get("/v1/sillycat/resource/admin-only") ~> addCredentials(BasicHttpCredentials("admin", "asdfadsf")) ~> route ~> check {
rejection === AuthenticationFailedRejection("sillycat")
}
}
}
}
7. How to Work with Logback
come soon…
8. How to work with DB
come soon...
9. How to Work with Actor
come soon…
10. How to do Validation
come soon...
Tips:
1. Log Error
Error Message:
app[ERROR]: May 1, 2013 1:57:26 PM com.mchange.v2.log.MLog <clinit>
app[ERROR]: INFO: MLog clients using java 1.4+ standard logging.
app[ERROR]: May 1, 2013 1:57:27 PM com.mchange.v2.c3p0.C3P0Registry banner
app[ERROR]: INFO: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
Solution:
2. JRebel
The purpose for this is to enable hot reloading.
Follow the document here https://github.com/spray/sbt-revolver
Visit this website to get a free license https://my.jrebel.com/plans/
We can see the information here https://my.jrebel.com/account/my-dashboard
And get the license from here https://my.jrebel.com/account/how-to-activate
Download the latest package named rebel-5.2.2-nosetup.zip
Unzip this file and place in the directory /Users/carl/tool/jrebel
Link it to the working directory
>sudo ln -s /Users/carl/tool/jrebel /opt/jrebel
Run the active command
>cd /opt/jrebel/bin
>./jrebel-config.sh
After that, make sbt-revoler find that plugin
>vi ~/.profile
export JREBEL_PATH=/opt/jrebel/jrebel.jar
>. ~/.profile
That is it. We do not need to re-start our spray server now.
References:
http://www.gtan.com/akka_doc/scala/routing.html
https://github.com/cakesolutions/spray-auth-example
http://spray.io/documentation/spray-routing/
https://github.com/spray/spray/wiki/Authentication-Authorization
https://github.com/spray/spray/wiki/Configuration
https://github.com/spray/spray/wiki
https://github.com/spray/sbt-revolver
https://github.com/spray/spray/blob/master/spray-routing-tests/src/test/scala/spray/routing/SecurityDirectivesSpec.scala
http://doc.akka.io/docs/akka/2.1.0/scala/logging.html
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 476NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 337Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 385Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 478NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 423Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 248GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 451GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 318Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 294Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 288NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 261Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 573NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 265Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 368Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 370Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
AndroidAsync.zip,用于Android的异步套接字、HTTP(客户端 服务器)、WebSocket和socket.io库。基于nio,而不是threads.asynchronous socket、http(client server)和android的websocket库。基于nio,而不是线程。
spray-actor-per-request, 使用每个请求模型中的参与者的示例 Spray 应用程序 每个请求的 Spray这个项目提供了一个示例 Spray 应用程序,它使用每个请求模型中的参与者。为什么要为每个HTTP请求启动一个参与者?轻松...
综上所述,"knol-spray-auth" 提供了一个基础模板,展示了如何使用 Scala、Akka 和 Spray 构建具备身份验证功能的 REST API。开发者可以通过研究这个项目,学习如何在实际应用中实现安全的、基于 Akka 的 REST 服务...
"slick-pg_spray-json_2.10-0.5.2.2.zip"中的Slick-PG版本,结合了Scala的spray-json库,使得JSON数据可以无缝地在数据库和代码之间转换。spray-json是Scala的一个轻量级、快速且易于使用的JSON库,它可以方便地解析...
描述 "spray-cache-spymemcached.zip" 涉及的是一个针对 Spray 框架的缓存扩展,名为 SpyMemcached 后端。Spray 是一个基于 Scala 的高性能、轻量级的 HTTP 和 RESTful 服务构建工具,常用于构建异步、非阻塞的服务...
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
$ git clone git://github.com/spray/spray-template.git my-project 将目录更改为您的克隆: $ cd 我的项目 启动 SBT: $ sbt 编译一切并运行所有测试: 测试 启动应用程序: 重新开始 浏览到以查看 angular...
spray-template使得开发RESTful API和服务时,能够快速、灵活地生成动态HTML或其他文本格式的响应。 在spray-can中,spray-template扮演了重要的角色,它允许开发者使用简洁、可读性强的模板语言来构建HTTP响应的...
《Akka HTTP与JSON集成:探索开源项目fabric8-maven-generator-api-3.1.62.zip中的精华》 在当今的软件开发领域,高效、可靠的网络通信框架和灵活的数据序列化工具是不可或缺的组件。Akka HTTP作为一款强大的、反应...
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用