简体   繁体   English

如何在blaze服务器(http4s)中安装servlet过滤器

[英]How to mount a servlet filter in blaze server (http4s)

I am a newbie in scala world and using http4s for developing the REST layer of my application. 我是scala世界中的新手,并且使用http4s开发应用程序的REST层。 I am using blaze server for deploying/publishing the services. 我正在使用blaze服务器来部署/发布服务。 I need to mount a servlet filter or interceptor in the flow before it reaches my HttpService methods. 在到达我的HttpService方法之前,我需要在流中安装servlet过滤器或拦截器。 How can I do that? 我怎样才能做到这一点?

http4s library has so called middleware functionality. http4s库具有所谓的中间件功能。 Middleware functionality is a wrapper around your service. 中间件功能是服务的包装。 For example, you have simple endpoint: 例如,您有简单的端点:

 val helloWorldService = HttpService {
  case GET -> Root / "hello" / name =>
    Ok(s"Hello, $name.")
}

To apply middleware you may do the following: 要应用中间件,您可以执行以下操作:

     val service: HttpService = middleware(authedService)

      val authedService: AuthedService[User] =
        AuthedService {
          case GET -> Root / "welcome" as user => Ok(s"Welcome, ${user.name}")
        }
      val middleware = AuthMiddleware(authUser)

and your just need to implement your authUser : 而您只需要实现您的authUser

val authUser: Service[Request, User] = ???

You may want to check out the org.http4s.server.middleware package for already existent middleware. 您可能需要检出org.http4s.server.middleware软件包中是否已存在的中间件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM