cloud.hedou.abp.remote.HeadersInterceptor.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abp-spring-boot-starter Show documentation
Show all versions of abp-spring-boot-starter Show documentation
When the functions of ABP cannot meet service requirements, the Spring Boot framework can be used to expand its own services to make use of abundant Java frameworks on the market.
package cloud.hedou.abp.remote
import okhttp3.Interceptor
import okhttp3.Response
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.ServletRequestAttributes
class HeadersInterceptor : Interceptor {
private val headerNames = arrayOf("Authorization", "__tenant")
override fun intercept(chain: Interceptor.Chain): Response {
val requestBuilder = chain.request().newBuilder().addHeader("Accept", "application/json")
val requestAttributes = RequestContextHolder.getRequestAttributes()
if (requestAttributes is ServletRequestAttributes) {
val request = requestAttributes.request
for (name in request.headerNames) {
if (isValidatedHeader(name)) {
val value = request.getHeader(name)
requestBuilder.addHeader(name, value)
}
}
}
return chain.proceed(requestBuilder.build())
}
private fun isValidatedHeader(name: String): Boolean {
return headerNames.any {
name.equals(it, true)
}
}
}