-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
IN40068837
authored and
IN40068837
committed
Dec 14, 2024
1 parent
9fa9ba3
commit d4b26da
Showing
12 changed files
with
355 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ [email protected]_API_BASE_URL@ | |
spring.redis.host=localhost | ||
|
||
#ELK logging file name | ||
logging.file.name=@env.HELPLINE104_API_LOGGING_FILE_NAME@ | ||
logging.file.name=@env.HELPLINE104_API_LOGGING_FILE_NAME@ | ||
jwt.secret=@env.JWT_SECRET_KEY@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ common-url=<Enter your socket address here>/commonapi-v1.0 | |
|
||
### Redis IP | ||
spring.redis.host=localhost | ||
|
||
jwt.secret= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,4 @@ common-url=http://localhost:8080/commonapi-v1.0 | |
|
||
### Redis IP | ||
spring.redis.host=localhost | ||
|
||
jwt.secret= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,4 @@ common-url=<Enter your socket address here>/commonapi-v1.0 | |
|
||
### Redis IP | ||
spring.redis.host=localhost | ||
|
||
jwt.secret= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.iemr.helpline104.utils; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
@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); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/iemr/helpline104/utils/FilterConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.iemr.helpline104.utils; | ||
|
||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
|
||
@Configuration | ||
public class FilterConfig { | ||
|
||
@Bean | ||
public FilterRegistrationBean<JwtUserIdValidationFilter> jwtUserIdValidationFilter(JwtAuthenticationUtil jwtAuthenticationUtil) { | ||
FilterRegistrationBean<JwtUserIdValidationFilter> registrationBean = new FilterRegistrationBean<>(); | ||
registrationBean.setFilter(new JwtUserIdValidationFilter(jwtAuthenticationUtil)); | ||
registrationBean.addUrlPatterns("/*"); // Apply filter to all API endpoints | ||
return registrationBean; | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/com/iemr/helpline104/utils/JwtAuthenticationUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.iemr.helpline104.utils; | ||
|
||
import java.util.Optional; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.iemr.helpline104.data.users.M_User; | ||
import com.iemr.helpline104.repository.users.IEMRUserRepositoryCustom; | ||
import com.iemr.helpline104.utils.exception.IEMRException; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
@Component | ||
public class JwtAuthenticationUtil { | ||
|
||
@Autowired | ||
private CookieUtil cookieUtil; | ||
@Autowired | ||
private JwtUtil jwtUtil; | ||
@Autowired | ||
private IEMRUserRepositoryCustom iEMRUserRepositoryCustom; | ||
private final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); | ||
|
||
public JwtAuthenticationUtil(CookieUtil cookieUtil, JwtUtil jwtUtil) { | ||
this.cookieUtil = cookieUtil; | ||
this.jwtUtil = jwtUtil; | ||
} | ||
|
||
public ResponseEntity<String> validateJwtToken(HttpServletRequest request) { | ||
Optional<String> jwtTokenOpt = cookieUtil.getCookieValue(request, "Jwttoken"); | ||
|
||
if (jwtTokenOpt.isEmpty()) { | ||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED) | ||
.body("Error 401: Unauthorized - JWT Token is not set!"); | ||
} | ||
|
||
String jwtToken = jwtTokenOpt.get(); | ||
|
||
// Validate the token | ||
Claims claims = jwtUtil.validateToken(jwtToken); | ||
if (claims == null) { | ||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Error 401: Unauthorized - Invalid JWT Token!"); | ||
} | ||
|
||
// Extract username from token | ||
String usernameFromToken = claims.getSubject(); | ||
if (usernameFromToken == null || usernameFromToken.isEmpty()) { | ||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED) | ||
.body("Error 401: Unauthorized - Username is missing!"); | ||
} | ||
|
||
// Return the username if valid | ||
return ResponseEntity.ok(usernameFromToken); | ||
} | ||
|
||
public boolean validateUserIdAndJwtToken(String jwtToken) throws IEMRException { | ||
try { | ||
// Validate JWT token and extract claims | ||
Claims claims = jwtUtil.validateToken(jwtToken); | ||
|
||
if (claims == null) { | ||
throw new IEMRException("Invalid JWT token."); | ||
} | ||
|
||
String userId = claims.get("userId", String.class); | ||
|
||
// Fetch user based on userId from the database or cache | ||
M_User user = iEMRUserRepositoryCustom.findByUserID(Long.parseLong(userId)); | ||
if (user == null) { | ||
throw new IEMRException("Invalid User ID."); | ||
} | ||
|
||
return true; // Valid userId and JWT token | ||
} catch (Exception e) { | ||
logger.error("Validation failed: " + e.getMessage(), e); | ||
throw new IEMRException("Validation error: " + e.getMessage(), e); | ||
} | ||
} | ||
} |
Oops, something went wrong.