diff --git a/http-requests/thirdParty.http b/http-requests/thirdParty.http new file mode 100644 index 0000000..b2817a2 --- /dev/null +++ b/http-requests/thirdParty.http @@ -0,0 +1 @@ +GET http://localhost:12000/wahlrecht/thirdParty \ No newline at end of file diff --git a/module-server/src/main/kotlin/de/twomartens/wahlrecht/configuration/WebSecurityConfiguration.kt b/module-server/src/main/kotlin/de/twomartens/wahlrecht/configuration/WebSecurityConfiguration.kt index 330f897..8c4aaff 100644 --- a/module-server/src/main/kotlin/de/twomartens/wahlrecht/configuration/WebSecurityConfiguration.kt +++ b/module-server/src/main/kotlin/de/twomartens/wahlrecht/configuration/WebSecurityConfiguration.kt @@ -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()) - .permitAll() - .and() - .authorizeHttpRequests() - .requestMatchers(HttpMethod.OPTIONS) - .permitAll() - .and() - .authorizeHttpRequests() - .anyRequest() - .authenticated() - .and() - .oauth2ResourceServer { obj: OAuth2ResourceServerConfigurer -> obj.jwt() } + .csrf { it.disable() } + .authorizeHttpRequests { it.requestMatchers(*PERMITTED_PATHS.toTypedArray()).permitAll() } + .authorizeHttpRequests { it.requestMatchers(HttpMethod.OPTIONS).permitAll() } + .authorizeHttpRequests { it.anyRequest().authenticated() } + .oauth2ResourceServer { obj: OAuth2ResourceServerConfigurer -> 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() diff --git a/module-server/src/main/kotlin/de/twomartens/wahlrecht/controller/ThirdPartyController.kt b/module-server/src/main/kotlin/de/twomartens/wahlrecht/controller/ThirdPartyController.kt new file mode 100644 index 0000000..be0630a --- /dev/null +++ b/module-server/src/main/kotlin/de/twomartens/wahlrecht/controller/ThirdPartyController.kt @@ -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 { + return template.getForEntity("https://www.google.de", String::class.java) + } +} \ No newline at end of file diff --git a/module-server/src/main/kotlin/de/twomartens/wahlrecht/interceptor/HeaderInterceptor.kt b/module-server/src/main/kotlin/de/twomartens/wahlrecht/interceptor/HeaderInterceptor.kt index 10553d4..7e2e425 100644 --- a/module-server/src/main/kotlin/de/twomartens/wahlrecht/interceptor/HeaderInterceptor.kt +++ b/module-server/src/main/kotlin/de/twomartens/wahlrecht/interceptor/HeaderInterceptor.kt @@ -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 diff --git a/module-server/src/main/kotlin/de/twomartens/wahlrecht/interceptor/HeaderInterceptorRest.kt b/module-server/src/main/kotlin/de/twomartens/wahlrecht/interceptor/HeaderInterceptorRest.kt index b7b29cd..ea66d87 100644 --- a/module-server/src/main/kotlin/de/twomartens/wahlrecht/interceptor/HeaderInterceptorRest.kt +++ b/module-server/src/main/kotlin/de/twomartens/wahlrecht/interceptor/HeaderInterceptorRest.kt @@ -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() +// } +// } } \ No newline at end of file diff --git a/module-server/src/main/kotlin/de/twomartens/wahlrecht/interceptor/LoggingInterceptorRest.kt b/module-server/src/main/kotlin/de/twomartens/wahlrecht/interceptor/LoggingInterceptorRest.kt index af7b75e..c95173c 100644 --- a/module-server/src/main/kotlin/de/twomartens/wahlrecht/interceptor/LoggingInterceptorRest.kt +++ b/module-server/src/main/kotlin/de/twomartens/wahlrecht/interceptor/LoggingInterceptorRest.kt @@ -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) }