Fix trace id

This commit is contained in:
2023-08-01 15:44:36 +02:00
parent fd8be51d4c
commit 792d8a1aa4
6 changed files with 65 additions and 51 deletions
+1
View File
@@ -0,0 +1 @@
GET http://localhost:12000/wahlrecht/thirdParty
@@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpMethod import org.springframework.http.HttpMethod
import org.springframework.security.config.Customizer
import org.springframework.security.config.annotation.web.builders.HttpSecurity import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer
@@ -33,20 +34,11 @@ open class WebSecurityConfiguration {
@Throws(Exception::class) @Throws(Exception::class)
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain { open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http http
.csrf().disable() .csrf { it.disable() }
.authorizeHttpRequests() .authorizeHttpRequests { it.requestMatchers(*PERMITTED_PATHS.toTypedArray<String>()).permitAll() }
.requestMatchers(*PERMITTED_PATHS.toTypedArray<String>()) .authorizeHttpRequests { it.requestMatchers(HttpMethod.OPTIONS).permitAll() }
.permitAll() .authorizeHttpRequests { it.anyRequest().authenticated() }
.and() .oauth2ResourceServer { obj: OAuth2ResourceServerConfigurer<HttpSecurity?> -> obj.jwt(Customizer.withDefaults()) }
.authorizeHttpRequests()
.requestMatchers(HttpMethod.OPTIONS)
.permitAll()
.and()
.authorizeHttpRequests()
.anyRequest()
.authenticated()
.and()
.oauth2ResourceServer { obj: OAuth2ResourceServerConfigurer<HttpSecurity?> -> obj.jwt() }
.addFilterAfter(createPolicyEnforcerFilter(), BearerTokenAuthenticationFilter::class.java) .addFilterAfter(createPolicyEnforcerFilter(), BearerTokenAuthenticationFilter::class.java)
return http.build() return http.build()
} }
@@ -84,7 +76,9 @@ open class WebSecurityConfiguration {
"/actuator/**", "/actuator/**",
"/wahlrecht/v1/doc/**", "/wahlrecht/v1/doc/**",
"/wahlrecht/v1/api-docs/**", "/wahlrecht/v1/api-docs/**",
"/error" "/error",
"/wahlrecht/version",
"/wahlrecht/thirdParty",
) )
private val PATHS = buildPathConfigs() private val PATHS = buildPathConfigs()
@@ -0,0 +1,17 @@
package de.twomartens.wahlrecht.controller
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.client.RestTemplate
@Controller
@RequestMapping(value = ["/wahlrecht"])
class ThirdPartyController(@Qualifier("restTemplate") private val template: RestTemplate) {
@GetMapping(path = ["/thirdParty"])
fun send(): ResponseEntity<String> {
return template.getForEntity("https://www.google.de", String::class.java)
}
}
@@ -18,10 +18,9 @@ abstract class HeaderInterceptor {
const val REQ_TYPE_SERVER_TEST = "SERVER_TEST" const val REQ_TYPE_SERVER_TEST = "SERVER_TEST"
const val REQ_TYPE_WARMUP = "WARMUP" const val REQ_TYPE_WARMUP = "WARMUP"
private fun createNewTraceId(): String { fun createNewTraceId(): String {
return UUID.randomUUID().toString() return UUID.randomUUID().toString()
} }
}
fun getTraceId(): String { fun getTraceId(): String {
val traceId = MDC.get(LOGGER_TRACE_ID) val traceId = MDC.get(LOGGER_TRACE_ID)
@@ -47,6 +46,8 @@ abstract class HeaderInterceptor {
} else setTraceId(traceId) } else setTraceId(traceId)
} }
}
fun markAsHealthCheck(): InterceptorCloseables { fun markAsHealthCheck(): InterceptorCloseables {
return InterceptorCloseables(mark(REQ_TYPE_HEALTHCHECK), setTraceId(createNewTraceId())) return InterceptorCloseables(mark(REQ_TYPE_HEALTHCHECK), setTraceId(createNewTraceId()))
} }
@@ -63,7 +64,6 @@ abstract class HeaderInterceptor {
return InterceptorCloseables(mark(REQ_TYPE_WARMUP), setTraceId(createNewTraceId())) return InterceptorCloseables(mark(REQ_TYPE_WARMUP), setTraceId(createNewTraceId()))
} }
class InterceptorCloseables(vararg closeables: Closeable) : Closeable { class InterceptorCloseables(vararg closeables: Closeable) : Closeable {
private val closeables: Array<out Closeable> private val closeables: Array<out Closeable>
@@ -7,7 +7,6 @@ import org.springframework.http.client.ClientHttpRequestExecution
import org.springframework.http.client.ClientHttpRequestInterceptor import org.springframework.http.client.ClientHttpRequestInterceptor
import org.springframework.http.client.ClientHttpResponse import org.springframework.http.client.ClientHttpResponse
import org.springframework.web.servlet.HandlerInterceptor import org.springframework.web.servlet.HandlerInterceptor
import org.springframework.web.servlet.ModelAndView
import java.io.IOException import java.io.IOException
import java.util.* import java.util.*
@@ -18,7 +17,7 @@ class HeaderInterceptorRest : HeaderInterceptor(), HandlerInterceptor, ClientHtt
fun extractTraceId(request: HttpServletRequest): String { fun extractTraceId(request: HttpServletRequest): String {
var traceId = request.getHeader(HEADER_FIELD_TRACE_ID) var traceId = request.getHeader(HEADER_FIELD_TRACE_ID)
if (traceId.isNullOrBlank()) traceId = request.getHeader(HEADER_FIELD_B3_TRACE_ID) if (traceId.isNullOrBlank()) traceId = request.getHeader(HEADER_FIELD_B3_TRACE_ID)
if (traceId.isNullOrBlank()) return UUID.randomUUID().toString() if (traceId.isNullOrBlank()) return createNewTraceId()
return traceId return traceId
} }
@@ -75,15 +74,15 @@ class HeaderInterceptorRest : HeaderInterceptor(), HandlerInterceptor, ClientHtt
} }
// HandlerInterceptor // HandlerInterceptor
override fun postHandle( // override fun postHandle(
request: HttpServletRequest, // request: HttpServletRequest,
response: HttpServletResponse, // response: HttpServletResponse,
handler: Any, // handler: Any,
modelAndView: ModelAndView? // modelAndView: ModelAndView?
) { // ) {
val obj = request.getAttribute(CLASS_NAME) // val obj = request.getAttribute(CLASS_NAME)
if (obj != null && obj is InterceptorCloseables) { // if (obj != null && obj is InterceptorCloseables) {
obj.close() // obj.close()
} // }
} // }
} }
@@ -112,12 +112,15 @@ class LoggingInterceptorRest(
direction = DIRECTION_IN, direction = DIRECTION_IN,
requestTime = requestTime, requestTime = requestTime,
responseTime = ZonedDateTime.now(clock), responseTime = ZonedDateTime.now(clock),
traceId = HeaderInterceptorRest.extractTraceId(httpRequest), traceId = HeaderInterceptor.getTraceId(),
requestType = HeaderInterceptorRest.extractRequestType(httpRequest), requestType = HeaderInterceptor.getRequestType(),
businessType = businessType, businessType = businessType,
throwable = throwable throwable = throwable
) )
) )
val interceptorCloseables =
request.getAttribute(HeaderInterceptorRest.CLASS_NAME) as HeaderInterceptor.InterceptorCloseables
interceptorCloseables.close()
} catch (e: java.lang.RuntimeException) { } catch (e: java.lang.RuntimeException) {
log.error(e.toString(), e) log.error(e.toString(), e)
} }