This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Oppretter egen resttemplate for maskinporten som legger til bearer au…
…tomatisk
- Loading branch information
Showing
3 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/no/nav/bidrag/commons/web/config/RestOperationsMaskinporten.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package no.nav.bidrag.commons.web.config | ||
|
||
import no.nav.bidrag.commons.web.interceptor.MaskinportenBearerTokenClientInterceptor | ||
import org.springframework.boot.web.client.RestTemplateBuilder | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Import | ||
import org.springframework.context.annotation.Scope | ||
|
||
@Configuration | ||
@Import(RestTemplateBuilderBean::class, | ||
MaskinportenBearerTokenClientInterceptor::class) | ||
class RestOperationsMaskinporten { | ||
|
||
@Bean("maskinporten") | ||
@Scope("prototype") | ||
fun restOperationsMaskinporten( | ||
restTemplateBuilder: RestTemplateBuilder, | ||
maskinportenBearerTokenClientInterceptor: MaskinportenBearerTokenClientInterceptor | ||
) = restTemplateBuilder.additionalInterceptors(maskinportenBearerTokenClientInterceptor).build() | ||
} |
26 changes: 26 additions & 0 deletions
26
.../kotlin/no/nav/bidrag/commons/web/interceptor/MaskinportenBearerTokenClientInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package no.nav.bidrag.commons.web.interceptor | ||
|
||
import no.nav.bidrag.commons.security.maskinporten.MaskinportenClient | ||
import org.springframework.context.annotation.Import | ||
import org.springframework.http.HttpRequest | ||
import org.springframework.http.MediaType | ||
import org.springframework.http.client.ClientHttpRequestExecution | ||
import org.springframework.http.client.ClientHttpRequestInterceptor | ||
import org.springframework.http.client.ClientHttpResponse | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
@Import(MaskinportenClient::class) | ||
class MaskinportenBearerTokenClientInterceptor(private val maskinportenClient: MaskinportenClient): ClientHttpRequestInterceptor { | ||
|
||
override fun intercept( | ||
request: HttpRequest, | ||
body: ByteArray, | ||
execution: ClientHttpRequestExecution | ||
): ClientHttpResponse { | ||
request.headers.setBearerAuth(maskinportenClient.hentMaskinportenToken().parsedString) | ||
request.headers.accept = listOf(MediaType.APPLICATION_JSON) | ||
request.headers.contentType = MediaType.APPLICATION_JSON | ||
return execution.execute(request, body) | ||
} | ||
} |