Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jwt api implementation #110

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>co.elastic.logging</groupId>
<artifactId>logback-ecs-encoder</artifactId>
<version>1.3.2</version>
<dependency>
<groupId>co.elastic.logging</groupId>
<artifactId>logback-ecs-encoder</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -125,8 +125,7 @@
<version>1.5.5.Final</version>
</dependency>

<!--
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand Down Expand Up @@ -197,14 +196,12 @@
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

<!--
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--
https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis -->
<!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
Expand All @@ -225,24 +222,21 @@
<version>7.0.0</version>
</dependency>

<!--
https://mvnrepository.com/artifact/ca.uhn.hapi.fhir/hapi-fhir-structures-r4 -->
<!-- https://mvnrepository.com/artifact/ca.uhn.hapi.fhir/hapi-fhir-structures-r4 -->
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-structures-r4</artifactId>
<version>7.0.0</version>
</dependency>

<!--
https://mvnrepository.com/artifact/ca.uhn.hapi.fhir/hapi-fhir-server -->
<!-- https://mvnrepository.com/artifact/ca.uhn.hapi.fhir/hapi-fhir-server -->
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-server</artifactId>
<version>7.0.0</version>
</dependency>

<!--
https://mvnrepository.com/artifact/ca.uhn.hapi.fhir/hapi-fhir-validation -->
<!-- https://mvnrepository.com/artifact/ca.uhn.hapi.fhir/hapi-fhir-validation -->
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-validation</artifactId>
Expand All @@ -255,8 +249,7 @@
</exclusions>
</dependency>

<!--
https://mvnrepository.com/artifact/ca.uhn.hapi.fhir/hapi-fhir-validation-resources-r4 -->
<!-- https://mvnrepository.com/artifact/ca.uhn.hapi.fhir/hapi-fhir-validation-resources-r4 -->
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-validation-resources-r4</artifactId>
Expand All @@ -273,11 +266,31 @@
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
</dependency>
<dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>6.1.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-api -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.12.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-impl -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.12.6</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-jackson -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.12.6</version>
<scope>runtime</scope>
</dependency>
</dependencies>


Expand Down Expand Up @@ -370,8 +383,7 @@
${target-properties} and
${source-properties}
</echo>
<concat destfile="${target-properties}"
append="yes"
<concat destfile="${target-properties}" append="yes"
force="yes">
<fileset file="${source-properties}">
</fileset>
Expand Down
5 changes: 4 additions & 1 deletion src/main/environment/common_ci.properties
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ prescription=TMPrescription SMS

### Redis IP
spring.redis.host=localhost
[email protected]_SECRET_KEY@


#ELK logging file name
[email protected]_API_LOGGING_FILE_NAME@
[email protected]_API_LOGGING_FILE_NAME@

1 change: 1 addition & 0 deletions src/main/environment/common_dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ prescription=TMPrescription SMS

### Redis IP
spring.redis.host=localhost
jwt.secret=



1 change: 1 addition & 0 deletions src/main/environment/common_example.properties
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ prescription=TMPrescription SMS

### Redis IP
spring.redis.host=localhost
jwt.secret=



1 change: 1 addition & 0 deletions src/main/environment/common_prod.properties
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ prescription=TMPrescription SMS

### Redis IP
spring.redis.host=localhost
jwt.secret=



1 change: 1 addition & 0 deletions src/main/environment/common_test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ prescription=TMPrescription SMS

### Redis IP
spring.redis.host=localhost
jwt.secret=



1 change: 1 addition & 0 deletions src/main/environment/common_uat.properties
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ prescription=TMPrescription SMS

### Redis IP
spring.redis.host=localhost
jwt.secret=



24 changes: 22 additions & 2 deletions src/main/java/com/iemr/hwc/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);

// Use StringRedisSerializer for keys (userId)
template.setKeySerializer(new StringRedisSerializer());

// Use Jackson2JsonRedisSerializer for values (Users objects)
Jackson2JsonRedisSerializer<Users> serializer = new Jackson2JsonRedisSerializer<>(Users.class);
template.setValueSerializer(serializer);

return template;
}
Comment on lines +73 to +86
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Generic type usage in RedisTemplate can lead to type mismatches if not carefully handled.

  1. You are returning RedisTemplate<String, Object> but only configuring the value serializer for Users. If you plan to store other object types, consider a more universal approach or define multiple templates with narrower type parameters.
  2. As with RedisConfig, confirm that stored Users objects remain compatible through version changes.

}
38 changes: 38 additions & 0 deletions src/main/java/com/iemr/hwc/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -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<String, Users> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Users> template = new RedisTemplate<>();
template.setConnectionFactory(factory);

// Use StringRedisSerializer for keys (userId)
template.setKeySerializer(new StringRedisSerializer());

// Use Jackson2JsonRedisSerializer for values (Users objects)
Jackson2JsonRedisSerializer<Users> serializer = new Jackson2JsonRedisSerializer<>(Users.class);
template.setValueSerializer(serializer);

return template;
}

}
5 changes: 4 additions & 1 deletion src/main/java/com/iemr/hwc/data/login/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@
*/
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;

@Data
@Entity
@Table(name = "m_user")
public class Users {
@JsonIgnoreProperties(ignoreUnknown = true)
public class Users implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Expose
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/iemr/hwc/repo/login/UserLoginRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public interface UserLoginRepo extends CrudRepository<Users, Long> {
@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);

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/iemr/hwc/utils/CookieUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.iemr.hwc.utils;

import java.util.Arrays;
import java.util.Optional;

import org.springframework.stereotype.Service;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;

@Service
public class CookieUtil {

public Optional<String> getCookieValue(HttpServletRequest request, String cookieName) {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookieName.equals(cookie.getName())) {
return Optional.of(cookie.getValue());
}
}
}
return Optional.empty();
}

public String getJwtTokenFromCookie(HttpServletRequest request) {
return Arrays.stream(request.getCookies()).filter(cookie -> "Jwttoken".equals(cookie.getName()))
.map(Cookie::getValue).findFirst().orElse(null);
}
}
Loading
Loading