All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cloud.hedou.abp.remote.HeadersInterceptor.kt Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.0.1
Show newest version
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)
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy