Fix trace id

This commit is contained in:
Jim Martens 2023-08-01 15:44:36 +02:00
parent fd8be51d4c
commit 792d8a1aa4
6 changed files with 65 additions and 51 deletions

View File

@ -0,0 +1 @@
GET http://localhost:12000/wahlrecht/thirdParty

View File

@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
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.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer
@ -33,20 +34,11 @@ open class WebSecurityConfiguration {
@Throws(Exception::class)
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http
.csrf().disable()
.authorizeHttpRequests()
.requestMatchers(*PERMITTED_PATHS.toTypedArray<String>())
.permitAll()
.and()
.authorizeHttpRequests()
.requestMatchers(HttpMethod.OPTIONS)
.permitAll()
.and()
.authorizeHttpRequests()
.anyRequest()
.authenticated()
.and()
.oauth2ResourceServer { obj: OAuth2ResourceServerConfigurer<HttpSecurity?> -> obj.jwt() }
.csrf { it.disable() }
.authorizeHttpRequests { it.requestMatchers(*PERMITTED_PATHS.toTypedArray<String>()).permitAll() }
.authorizeHttpRequests { it.requestMatchers(HttpMethod.OPTIONS).permitAll() }
.authorizeHttpRequests { it.anyRequest().authenticated() }
.oauth2ResourceServer { obj: OAuth2ResourceServerConfigurer<HttpSecurity?> -> obj.jwt(Customizer.withDefaults()) }
.addFilterAfter(createPolicyEnforcerFilter(), BearerTokenAuthenticationFilter::class.java)
return http.build()
}
@ -84,7 +76,9 @@ open class WebSecurityConfiguration {
"/actuator/**",
"/wahlrecht/v1/doc/**",
"/wahlrecht/v1/api-docs/**",
"/error"
"/error",
"/wahlrecht/version",
"/wahlrecht/thirdParty",
)
private val PATHS = buildPathConfigs()

View File

@ -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)
}
}

View File

@ -18,33 +18,34 @@ abstract class HeaderInterceptor {
const val REQ_TYPE_SERVER_TEST = "SERVER_TEST"
const val REQ_TYPE_WARMUP = "WARMUP"
private fun createNewTraceId(): String {
fun createNewTraceId(): String {
return UUID.randomUUID().toString()
}
}
fun getTraceId(): String {
val traceId = MDC.get(LOGGER_TRACE_ID)
return if (traceId.isNullOrBlank()) createNewTraceId() else traceId
}
fun getTraceId(): String {
val traceId = MDC.get(LOGGER_TRACE_ID)
return if (traceId.isNullOrBlank()) createNewTraceId() else traceId
}
fun getRequestType(): String? {
val type = MDC.get(LOGGER_REQTYPE_ID)
return if (type.isNullOrBlank()) null else type
}
fun getRequestType(): String? {
val type = MDC.get(LOGGER_REQTYPE_ID)
return if (type.isNullOrBlank()) null else type
}
private fun setTraceId(traceId: String): InterceptorCloseables {
return InterceptorCloseables(MDC.putCloseable(LOGGER_TRACE_ID, traceId))
}
private fun setTraceId(traceId: String): InterceptorCloseables {
return InterceptorCloseables(MDC.putCloseable(LOGGER_TRACE_ID, traceId))
}
private fun mark(requestType: String?): InterceptorCloseables {
return InterceptorCloseables(MDC.putCloseable(LOGGER_REQTYPE_ID, requestType))
}
private fun mark(requestType: String?): InterceptorCloseables {
return InterceptorCloseables(MDC.putCloseable(LOGGER_REQTYPE_ID, requestType))
}
fun set(traceId: String, requestType: String?): InterceptorCloseables {
return if (requestType != null) {
InterceptorCloseables(setTraceId(traceId), mark(requestType))
} else setTraceId(traceId)
}
fun set(traceId: String, requestType: String?): InterceptorCloseables {
return if (requestType != null) {
InterceptorCloseables(setTraceId(traceId), mark(requestType))
} else setTraceId(traceId)
}
fun markAsHealthCheck(): InterceptorCloseables {
@ -63,7 +64,6 @@ abstract class HeaderInterceptor {
return InterceptorCloseables(mark(REQ_TYPE_WARMUP), setTraceId(createNewTraceId()))
}
class InterceptorCloseables(vararg closeables: Closeable) : Closeable {
private val closeables: Array<out Closeable>

View File

@ -7,7 +7,6 @@ import org.springframework.http.client.ClientHttpRequestExecution
import org.springframework.http.client.ClientHttpRequestInterceptor
import org.springframework.http.client.ClientHttpResponse
import org.springframework.web.servlet.HandlerInterceptor
import org.springframework.web.servlet.ModelAndView
import java.io.IOException
import java.util.*
@ -18,7 +17,7 @@ class HeaderInterceptorRest : HeaderInterceptor(), HandlerInterceptor, ClientHtt
fun extractTraceId(request: HttpServletRequest): String {
var traceId = request.getHeader(HEADER_FIELD_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
}
@ -75,15 +74,15 @@ class HeaderInterceptorRest : HeaderInterceptor(), HandlerInterceptor, ClientHtt
}
// HandlerInterceptor
override fun postHandle(
request: HttpServletRequest,
response: HttpServletResponse,
handler: Any,
modelAndView: ModelAndView?
) {
val obj = request.getAttribute(CLASS_NAME)
if (obj != null && obj is InterceptorCloseables) {
obj.close()
}
}
// override fun postHandle(
// request: HttpServletRequest,
// response: HttpServletResponse,
// handler: Any,
// modelAndView: ModelAndView?
// ) {
// val obj = request.getAttribute(CLASS_NAME)
// if (obj != null && obj is InterceptorCloseables) {
// obj.close()
// }
// }
}

View File

@ -112,12 +112,15 @@ class LoggingInterceptorRest(
direction = DIRECTION_IN,
requestTime = requestTime,
responseTime = ZonedDateTime.now(clock),
traceId = HeaderInterceptorRest.extractTraceId(httpRequest),
requestType = HeaderInterceptorRest.extractRequestType(httpRequest),
traceId = HeaderInterceptor.getTraceId(),
requestType = HeaderInterceptor.getRequestType(),
businessType = businessType,
throwable = throwable
)
)
val interceptorCloseables =
request.getAttribute(HeaderInterceptorRest.CLASS_NAME) as HeaderInterceptor.InterceptorCloseables
interceptorCloseables.close()
} catch (e: java.lang.RuntimeException) {
log.error(e.toString(), e)
}