From 04c18a4599a2619e3ad53b961c9a81aa1b28bbaa Mon Sep 17 00:00:00 2001 From: "K.Filippopolitis" <56073635+KFilippopolitis@users.noreply.github.com> Date: Thu, 4 Jul 2024 16:12:12 +0300 Subject: [PATCH] Redirect logs outside of console (#78) * Redirect logs outside of console * Log user log in and authorities. --- config/application.tmpl | 2 ++ .../AuthenticationEventListener.java | 26 +++++++++++++++++++ src/main/resources/application.yml | 8 ++---- 3 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 src/main/java/hbp/mip/configurations/AuthenticationEventListener.java diff --git a/config/application.tmpl b/config/application.tmpl index 601b82a6..087ffb0f 100644 --- a/config/application.tmpl +++ b/config/application.tmpl @@ -15,6 +15,8 @@ logging: root: {{ default .Env.LOG_LEVEL_FRAMEWORK "ERROR" }} eu: hbp: {{ default .Env.LOG_LEVEL "INFO" }} + file: + name: /var/log/portalbackend/portalbackend.log ### DATABASE CONFIGURATION ### diff --git a/src/main/java/hbp/mip/configurations/AuthenticationEventListener.java b/src/main/java/hbp/mip/configurations/AuthenticationEventListener.java new file mode 100644 index 00000000..7de16e61 --- /dev/null +++ b/src/main/java/hbp/mip/configurations/AuthenticationEventListener.java @@ -0,0 +1,26 @@ +package hbp.mip.configurations; + +import hbp.mip.utils.Logger; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.event.EventListener; +import org.springframework.security.authentication.event.AuthenticationSuccessEvent; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +public class AuthenticationEventListener { + + @EventListener + public void handleAuthenticationSuccess(AuthenticationSuccessEvent event) { + Authentication authentication = event.getAuthentication(); + String username = authentication.getName(); + Logger logger = new Logger(username, "LOGGING IN"); + logger.info("User " + username + " has logged in successfully"); + + // If you need to log additional details: + authentication.getAuthorities().forEach(authority -> + logger.info("User " + username + " has authority " + authority.getAuthority()) + ); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b0ab7b82..16fb8178 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,6 +1,5 @@ # Configuration for development purposes - ### EMBEDDED SERVER CONFIGURATION ### server: servlet: @@ -8,14 +7,14 @@ server: port: 8080 forward-headers-strategy: native - ### LOG LEVELS ### logging: level: root: "INFO" eu: hbp: "DEBUG" - + file: + name: /var/log/portalbackend/portalbackend.log ### DATABASE CONFIGURATION ### spring: @@ -51,7 +50,6 @@ spring: user-name-attribute: preferred_username issuer-uri: http://172.17.0.1/auth/realms/MIP - ### AUTHENTICATION ### authentication: enabled: 0 @@ -59,7 +57,6 @@ authentication: all_experiments_allowed_claim: research_experiment_all dataset_claim_prefix: research_dataset_ - ### EXTERNAL SERVICES ### services: algorithmsUpdateInterval: 100 @@ -68,7 +65,6 @@ services: attributesUrl: "http://127.0.0.1:5000/data_models_attributes" cdesMetadataUrl: "http://127.0.0.1:5000/cdes_metadata" - ### EXTERNAL FILES ### # Files are loaded from the resources files: