From 62d7ef1bb3a3d0888c05adb586e5e806d8d4eaa0 Mon Sep 17 00:00:00 2001 From: d142796 Date: Mon, 3 Jun 2024 16:42:33 +0200 Subject: [PATCH] =?UTF-8?q?Debugging:=20logger=20for=20=C3=A5=20se=20om=20?= =?UTF-8?q?url=20blir=20satt=20riktig=20under=20kall=20til=20kontoregister?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SecurityClientConfiguration.kt | 54 ++++++++++++++++--- .../okonomi/KontoregisterServiceImpl.kt | 22 ++++---- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/no/nav/arbeidsgiver/tiltakrefusjon/SecurityClientConfiguration.kt b/src/main/kotlin/no/nav/arbeidsgiver/tiltakrefusjon/SecurityClientConfiguration.kt index 4dc00c28..0252cd82 100644 --- a/src/main/kotlin/no/nav/arbeidsgiver/tiltakrefusjon/SecurityClientConfiguration.kt +++ b/src/main/kotlin/no/nav/arbeidsgiver/tiltakrefusjon/SecurityClientConfiguration.kt @@ -1,11 +1,14 @@ package no.nav.arbeidsgiver.tiltakrefusjon +import no.nav.arbeidsgiver.tiltakrefusjon.okonomi.KontoregisterProperties import no.nav.security.token.support.client.core.ClientProperties import no.nav.security.token.support.client.core.oauth2.OAuth2AccessTokenService import no.nav.security.token.support.client.spring.ClientConfigurationProperties import no.nav.security.token.support.client.spring.oauth2.ClientConfigurationPropertiesMatcher import no.nav.security.token.support.client.spring.oauth2.EnableOAuth2Client import no.nav.security.token.support.client.spring.oauth2.OAuth2ClientRequestInterceptor +import org.slf4j.Logger +import org.slf4j.LoggerFactory import org.springframework.boot.web.client.RestTemplateBuilder import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration @@ -21,11 +24,14 @@ import java.util.* @Configuration @Profile("dev-gcp", "prod-gcp") class SecurityClientConfiguration( - val restTemplateBuilder: RestTemplateBuilder, - val clientConfigurationProperties: ClientConfigurationProperties, - val oAuth2AccessTokenService: OAuth2AccessTokenService + val properties: KontoregisterProperties, + val restTemplateBuilder: RestTemplateBuilder, + val clientConfigurationProperties: ClientConfigurationProperties, + val oAuth2AccessTokenService: OAuth2AccessTokenService ) { + val log: Logger = LoggerFactory.getLogger(javaClass) + @Bean fun pƄVegneAvSaksbehandlerGraphRestTemplate() = restTemplateForRegistration("aad-graph") @@ -45,9 +51,31 @@ class SecurityClientConfiguration( private fun restTemplateForRegistration(registration: String): RestTemplate { val clientProperties = clientConfigurationProperties.registration[registration] ?: throw RuntimeException("could not find oauth2 client config for $registration") - return restTemplateBuilder - .additionalInterceptors(bearerTokenInterceptor(clientProperties, oAuth2AccessTokenService)) - .build() + when(registration ) { + "sokos-kontoregister" -> { + val restTemplate: RestTemplate = restTemplateBuilder + .additionalInterceptors( + bearerTokenInterceptorKontoregister( + clientProperties, + oAuth2AccessTokenService + ) + ) + .build() + return restTemplate + } + else -> { + val restTemplate: RestTemplate = restTemplateBuilder + .additionalInterceptors( + bearerTokenInterceptor( + clientProperties, + oAuth2AccessTokenService + ) + ) + .build() + return restTemplate + } + + } } @@ -61,4 +89,18 @@ class SecurityClientConfiguration( execution.execute(request, body) } } + + private fun bearerTokenInterceptorKontoregister( + clientProperties: ClientProperties, + oAuth2AccessTokenService: OAuth2AccessTokenService + ): ClientHttpRequestInterceptor { + return ClientHttpRequestInterceptor { request: HttpRequest, body: ByteArray, execution: ClientHttpRequestExecution -> + val response = oAuth2AccessTokenService.getAccessToken(clientProperties) + request.headers.setBearerAuth(response.accessToken!!) + request.headers.set("Nav-Consumer-Id",properties.consumerId) + request.headers.set("Nav-Call-Id",UUID.randomUUID().toString()) + log.info("#### HEADERS: ${request.headers}"); + execution.execute(request, body) + } + } } \ No newline at end of file diff --git a/src/main/kotlin/no/nav/arbeidsgiver/tiltakrefusjon/okonomi/KontoregisterServiceImpl.kt b/src/main/kotlin/no/nav/arbeidsgiver/tiltakrefusjon/okonomi/KontoregisterServiceImpl.kt index 24e79d0f..12831119 100644 --- a/src/main/kotlin/no/nav/arbeidsgiver/tiltakrefusjon/okonomi/KontoregisterServiceImpl.kt +++ b/src/main/kotlin/no/nav/arbeidsgiver/tiltakrefusjon/okonomi/KontoregisterServiceImpl.kt @@ -3,12 +3,18 @@ package no.nav.arbeidsgiver.tiltakrefusjon.okonomi import no.nav.arbeidsgiver.tiltakrefusjon.okonomi.request.KontoregisterRequest import no.nav.arbeidsgiver.tiltakrefusjon.okonomi.response.KontoregisterResponse import no.nav.arbeidsgiver.tiltakrefusjon.utils.ConditionalOnPropertyNotEmpty +import no.nav.security.token.support.client.core.ClientProperties +import no.nav.security.token.support.client.core.oauth2.OAuth2AccessTokenService import org.slf4j.Logger import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Qualifier +import org.springframework.boot.web.client.RestTemplateBuilder import org.springframework.http.HttpEntity import org.springframework.http.HttpHeaders import org.springframework.http.HttpMethod +import org.springframework.http.HttpRequest +import org.springframework.http.client.ClientHttpRequestExecution +import org.springframework.http.client.ClientHttpRequestInterceptor import org.springframework.stereotype.Service import org.springframework.web.client.RestClientException import org.springframework.web.client.RestTemplate @@ -22,19 +28,18 @@ import java.util.* @ConditionalOnPropertyNotEmpty("tiltak-refusjon.kontoregister.uri") class KontoregisterServiceImpl( val properties: KontoregisterProperties, - @Qualifier("sokosRestTemplate") val restTemplate: RestTemplate + @Qualifier("sokosRestTemplate") val restTemplate: RestTemplate, + val restTemplateBuilder: RestTemplateBuilder, ) : KontoregisterService { val log: Logger = LoggerFactory.getLogger(javaClass) override fun hentBankkontonummer(bedriftNr: String): String? { - val requestEntity = lagRequest() val url = "${properties.uri}/${bedriftNr}" try { - log.warn("##### kall url: ${url} "); + log.warn("##### kall url: ${url} ${restTemplate}"); val responseMedKontonummerTilBedrift = - restTemplate.exchange(url, HttpMethod.GET, requestEntity) - log.warn("##### kall headers: ${responseMedKontonummerTilBedrift.headers}"); + restTemplate.exchange(url, HttpMethod.GET) return responseMedKontonummerTilBedrift?.body?.kontonr } catch (e: RestClientException) { log.warn("Kontoregister call feiler", e) @@ -42,11 +47,4 @@ class KontoregisterServiceImpl( return null } - private fun lagRequest(): HttpEntity { - val headers = HttpHeaders() - headers["Nav-Consumer-Id"] = properties.consumerId - headers["Nav-Call-Id"] = UUID.randomUUID().toString() - val body = null - return HttpEntity(body, headers) - } } \ No newline at end of file