From eb2f9851ed5ab3400ea85e1796dcfa8cd968ee10 Mon Sep 17 00:00:00 2001 From: IN40068837 Date: Thu, 19 Dec 2024 12:53:01 +0530 Subject: [PATCH] use fetch from redis concept --- src/main/java/com/iemr/hwc/Application.java | 24 +++++++++++- .../java/com/iemr/hwc/config/RedisConfig.java | 38 +++++++++++++++++++ .../java/com/iemr/hwc/data/login/Users.java | 5 ++- .../iemr/hwc/repo/login/UserLoginRepo.java | 2 +- .../iemr/hwc/utils/JwtAuthenticationUtil.java | 20 ++++++++-- 5 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/iemr/hwc/config/RedisConfig.java diff --git a/src/main/java/com/iemr/hwc/Application.java b/src/main/java/com/iemr/hwc/Application.java index 8c415c55..cd9bf306 100644 --- a/src/main/java/com/iemr/hwc/Application.java +++ b/src/main/java/com/iemr/hwc/Application.java @@ -32,8 +32,13 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.web.client.RestTemplate; +import com.iemr.hwc.data.login.Users; import com.iemr.hwc.fhir.config.fhirRestfulServer.FhirRestfulServer; import com.iemr.hwc.utils.IEMRApplBeans; @@ -57,11 +62,26 @@ public RestTemplate getRestTemplate() { return new RestTemplate(); } - //Registering new fhir servlet for CHO mobile app + // Registering new fhir servlet for CHO mobile app @Bean public ServletRegistrationBean ServletRegistrationBean() { - ServletRegistrationBean registration= new ServletRegistrationBean(new FhirRestfulServer(context),"/fhir/*"); + ServletRegistrationBean registration = new ServletRegistrationBean(new FhirRestfulServer(context), "/fhir/*"); registration.setName("FhirServlet"); return registration; } + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(factory); + + // Use StringRedisSerializer for keys (userId) + template.setKeySerializer(new StringRedisSerializer()); + + // Use Jackson2JsonRedisSerializer for values (Users objects) + Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(Users.class); + template.setValueSerializer(serializer); + + return template; + } } diff --git a/src/main/java/com/iemr/hwc/config/RedisConfig.java b/src/main/java/com/iemr/hwc/config/RedisConfig.java new file mode 100644 index 00000000..e8fea873 --- /dev/null +++ b/src/main/java/com/iemr/hwc/config/RedisConfig.java @@ -0,0 +1,38 @@ +package com.iemr.hwc.config; + +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; +import org.springframework.session.data.redis.config.ConfigureRedisAction; + +import com.iemr.hwc.data.login.Users; + +@Configuration +@EnableCaching +public class RedisConfig { + + @Bean + public ConfigureRedisAction configureRedisAction() { + return ConfigureRedisAction.NO_OP; + } + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(factory); + + // Use StringRedisSerializer for keys (userId) + template.setKeySerializer(new StringRedisSerializer()); + + // Use Jackson2JsonRedisSerializer for values (Users objects) + Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(Users.class); + template.setValueSerializer(serializer); + + return template; + } + +} diff --git a/src/main/java/com/iemr/hwc/data/login/Users.java b/src/main/java/com/iemr/hwc/data/login/Users.java index 13669863..54232f47 100644 --- a/src/main/java/com/iemr/hwc/data/login/Users.java +++ b/src/main/java/com/iemr/hwc/data/login/Users.java @@ -21,10 +21,12 @@ */ package com.iemr.hwc.data.login; +import java.io.Serializable; import java.sql.Timestamp; import jakarta.persistence.*; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.google.gson.annotations.Expose; import com.iemr.hwc.data.location.DistrictBranchMapping; import lombok.Data; @@ -32,7 +34,8 @@ @Data @Entity @Table(name = "m_user") -public class Users { +@JsonIgnoreProperties(ignoreUnknown = true) +public class Users implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Expose diff --git a/src/main/java/com/iemr/hwc/repo/login/UserLoginRepo.java b/src/main/java/com/iemr/hwc/repo/login/UserLoginRepo.java index 5dc04854..541acc9b 100644 --- a/src/main/java/com/iemr/hwc/repo/login/UserLoginRepo.java +++ b/src/main/java/com/iemr/hwc/repo/login/UserLoginRepo.java @@ -33,7 +33,7 @@ public interface UserLoginRepo extends CrudRepository { @Query(" SELECT u FROM Users u WHERE u.userName = :UserName AND u.deleted = false ") public Users getUserByUsername(@Param("UserName") String username); - + @Query(" SELECT u FROM Users u WHERE u.userID = :userID AND u.deleted = false ") public Users getUserByUserID(@Param("userID") Long userID); diff --git a/src/main/java/com/iemr/hwc/utils/JwtAuthenticationUtil.java b/src/main/java/com/iemr/hwc/utils/JwtAuthenticationUtil.java index 7655242f..f4579bfd 100644 --- a/src/main/java/com/iemr/hwc/utils/JwtAuthenticationUtil.java +++ b/src/main/java/com/iemr/hwc/utils/JwtAuthenticationUtil.java @@ -5,6 +5,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; @@ -24,7 +25,7 @@ public class JwtAuthenticationUtil { @Autowired private JwtUtil jwtUtil; @Autowired - private UserLoginRepo userLoginRepo; + private RedisTemplate redisTemplate; private final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); public JwtAuthenticationUtil(CookieUtil cookieUtil, JwtUtil jwtUtil) { @@ -70,8 +71,8 @@ public boolean validateUserIdAndJwtToken(String jwtToken) throws IEMRException { String userId = claims.get("userId", String.class); - // Fetch user based on userId from the database or cache - Users user = userLoginRepo.getUserByUserID(Long.parseLong(userId)); + // Check if user data is present in Redis + Users user = getUserFromCache(userId); if (user == null) { throw new IEMRException("Invalid User ID."); } @@ -82,4 +83,17 @@ public boolean validateUserIdAndJwtToken(String jwtToken) throws IEMRException { throw new IEMRException("Validation error: " + e.getMessage(), e); } } + + private Users getUserFromCache(String userId) { + String redisKey = "user_" + userId; // The Redis key format + Users user = (Users) redisTemplate.opsForValue().get(redisKey); + + if (user == null) { + logger.warn("User not found in Redis."); + } else { + logger.info("User fetched successfully from Redis."); + } + + return user; // Returns null if not found + } }