From b18ed35571fe942e43f603f7debed8e307b30610 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Wed, 22 Dec 2021 16:21:44 -0500 Subject: [PATCH] Add logging when users login --- .../config/security/JwtFilters.kt | 5 +++++ .../config/security/SecurityConfig.kt | 15 ++++++++++----- .../rest/AccountControllers.kt | 1 + .../service/jobs/TouchUpKitRemover.kt | 14 +++++++++----- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/config/security/JwtFilters.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/config/security/JwtFilters.kt index d5b5023..b7c27a8 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/config/security/JwtFilters.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/config/security/JwtFilters.kt @@ -41,6 +41,7 @@ class JwtAuthenticationFilter( override fun attemptAuthentication(request: HttpServletRequest, response: HttpServletResponse): Authentication { val loginRequest = jacksonObjectMapper().readValue(request.inputStream, UserLoginRequest::class.java) + logger.debug("Login attempt for user ${loginRequest.id}...") return authManager.authenticate(UsernamePasswordAuthenticationToken(loginRequest.id, loginRequest.password)) } @@ -53,6 +54,10 @@ class JwtAuthenticationFilter( val userDetails = auth.principal as UserDetails val token = jwtService.buildJwt(userDetails) + with(userDetails.user) { + logger.info("User ${this.id} (${this.firstName} ${this.lastName}) has logged in successfully") + } + response.addHeader("Access-Control-Expose-Headers", authorizationCookieName) response.addHeader(authorizationCookieName, "Bearer $token") response.addCookie(authorizationCookieName, "Bearer$token") { diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/config/security/SecurityConfig.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/config/security/SecurityConfig.kt index ec68d49..f2a70e7 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/config/security/SecurityConfig.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/config/security/SecurityConfig.kt @@ -7,6 +7,8 @@ import dev.fyloz.colorrecipesexplorer.model.account.User import dev.fyloz.colorrecipesexplorer.service.users.JwtService import dev.fyloz.colorrecipesexplorer.service.users.UserDetailsService import dev.fyloz.colorrecipesexplorer.service.users.UserService +import mu.KLogger +import mu.KotlinLogging import org.slf4j.Logger import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean @@ -39,9 +41,10 @@ abstract class BaseSecurityConfig( private val userDetailsService: UserDetailsService, private val jwtService: JwtService, private val environment: Environment, - protected val logger: Logger, protected val securityProperties: CreSecurityProperties ) : WebSecurityConfigurerAdapter() { + protected abstract val logger: Logger + protected val passwordEncoder = BCryptPasswordEncoder() var debugMode = false @@ -119,9 +122,10 @@ class SecurityConfig( @Lazy private val userService: UserService, jwtService: JwtService, environment: Environment, - logger: Logger, securityProperties: CreSecurityProperties -) : BaseSecurityConfig(userDetailsService, jwtService, environment, logger, securityProperties) { +) : BaseSecurityConfig(userDetailsService, jwtService, environment, securityProperties) { + override val logger = KotlinLogging.logger {} + @PostConstruct fun initWebSecurity() { if (emergencyMode) { @@ -165,9 +169,10 @@ class EmergencySecurityConfig( userDetailsService: UserDetailsService, jwtService: JwtService, environment: Environment, - logger: Logger, securityProperties: CreSecurityProperties -) : BaseSecurityConfig(userDetailsService, jwtService, environment, logger, securityProperties) { +) : BaseSecurityConfig(userDetailsService, jwtService, environment, securityProperties) { + override val logger = KotlinLogging.logger {} + init { emergencyMode = true } diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/rest/AccountControllers.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/rest/AccountControllers.kt index 42ab55e..75921be 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/rest/AccountControllers.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/rest/AccountControllers.kt @@ -5,6 +5,7 @@ import dev.fyloz.colorrecipesexplorer.config.annotations.PreAuthorizeViewUsers import dev.fyloz.colorrecipesexplorer.model.account.* import dev.fyloz.colorrecipesexplorer.service.users.GroupService import dev.fyloz.colorrecipesexplorer.service.users.UserService +import mu.KotlinLogging import org.springframework.context.annotation.Profile import org.springframework.http.MediaType import org.springframework.security.access.prepost.PreAuthorize diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/jobs/TouchUpKitRemover.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/jobs/TouchUpKitRemover.kt index e8cffaf..5cde5bf 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/jobs/TouchUpKitRemover.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/service/jobs/TouchUpKitRemover.kt @@ -1,7 +1,7 @@ package dev.fyloz.colorrecipesexplorer.service.jobs import dev.fyloz.colorrecipesexplorer.service.TouchUpKitService -import org.slf4j.Logger +import mu.KotlinLogging import org.springframework.context.annotation.Profile import org.springframework.scheduling.annotation.Scheduled import org.springframework.stereotype.Component @@ -9,18 +9,22 @@ import org.springframework.stereotype.Component @Component @Profile("!emergency") class TouchUpKitRemover( - private val touchUpKitService: TouchUpKitService, - private val logger: Logger + private val touchUpKitService: TouchUpKitService ) { + private val logger = KotlinLogging.logger {} + @Scheduled(cron = "0 0 0 * * *") fun execute() { + logger.debug("Executing expired touch up kits removal job... ") removeExpiredKits() } private fun removeExpiredKits() { - logger.info("Removing expired touch up kits...") with(touchUpKitService.getAll().filter(touchUpKitService::isExpired)) { - this.forEach(touchUpKitService::delete) + this.forEach { + logger.debug("Removed expired touch up kit ${it.id} (${it.project} ${it.buggy})") + touchUpKitService.delete(it) + } logger.info("Removed ${this.size} expired touch up kits") } }