-
Notifications
You must be signed in to change notification settings - Fork 28
401 unauthorized issue resolved #70
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
Conversation
WalkthroughThis pull request introduces JWT token handling across multiple service classes. Each modified class now injects a Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Service
participant CookieUtil
participant ExternalAPI
Client->>Service: Sends HTTP request
Service->>Service: Retrieve HttpServletRequest via RequestContextHolder
Service->>CookieUtil: Extract JWT token from cookie
CookieUtil-->>Service: Returns JWT token
Service->>ExternalAPI: HTTP request with header "Cookie: Jwttoken=<token>"
ExternalAPI-->>Service: Returns response
Service-->>Client: Responds back
Poem
Tip β‘π§ͺ Multi-step agentic review comment chat (experimental)
π Recent review detailsConfiguration used: CodeRabbit UI π Files selected for processing (1)
β Files skipped from review due to trivial changes (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. πͺ§ TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
π Outside diff range comments (1)
src/main/java/com/iemr/mmu/service/registrar/RegistrarServiceImpl.java (1)
735-797
: π οΈ Refactor suggestionSignificant code duplication across multiple methods.
The same JWT token extraction logic is repeated in four different methods (registerBeneficiary, updateBeneficiary, beneficiaryQuickSearch, and beneficiaryAdvanceSearch).
Extract this common code into a private helper method:
+ private HttpEntity<Object> createAuthenticatedRequest(String requestBody, String authorization) { + ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + String jwtTokenFromCookie = null; + if (requestAttributes != null) { + HttpServletRequest requestHeader = requestAttributes.getRequest(); + jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader); + } + + MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); + headers.add("Content-Type", "application/json"); + headers.add("AUTHORIZATION", authorization); + if (jwtTokenFromCookie != null) { + headers.add("Cookie", "Jwttoken=" + jwtTokenFromCookie); + } + + return new HttpEntity<>(requestBody, headers); + }Then update all the methods to use this helper:
// Example for registerBeneficiary - HttpServletRequest requestHeader = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) - .getRequest(); - String jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader); - MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); - headers.add("Content-Type", "application/json"); - headers.add("AUTHORIZATION", Authorization); - headers.add("Cookie", "Jwttoken=" + jwtTokenFromCookie); - HttpEntity<Object> request = new HttpEntity<Object>(comingRequest, headers); + HttpEntity<Object> request = createAuthenticatedRequest(comingRequest, Authorization);
π§Ή Nitpick comments (2)
src/main/java/com/iemr/mmu/service/dataSyncActivity/DownloadDataFromServerTransactionalImpl.java (1)
277-280
: Code duplication - JWT token extraction logic is repeated.The same JWT token extraction logic is used in both downloadDataFromCentral and updateProcessedFlagToCentral methods.
Extract this logic into a private helper method to improve maintainability:
+ private String getJwtTokenFromCurrentRequest() { + ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + if (requestAttributes != null) { + HttpServletRequest requestHeader = requestAttributes.getRequest(); + return cookieUtil.getJwtTokenFromCookie(requestHeader); + } + return null; + }Then use this method in both places where the token is needed.
src/main/java/com/iemr/mmu/service/common/transaction/CommonServiceImpl.java (1)
715-719
: Code duplication - JWT token extraction logic is repeated.The same JWT token extraction logic is used in multiple methods (getTmCaseSheet and getCaseSheetFromCentralServer).
Extract this logic into a private helper method to improve maintainability:
+ private String getJwtTokenFromCurrentRequest() { + ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + if (requestAttributes != null) { + HttpServletRequest requestHeader = requestAttributes.getRequest(); + return cookieUtil.getJwtTokenFromCookie(requestHeader); + } + return null; + }Then use this method in all places where the token is needed.
π Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
π Files selected for processing (8)
src/main/java/com/iemr/mmu/service/cancerScreening/CSCarestreamServiceImpl.java
(2 hunks)src/main/java/com/iemr/mmu/service/common/transaction/CommonDoctorServiceImpl.java
(4 hunks)src/main/java/com/iemr/mmu/service/common/transaction/CommonServiceImpl.java
(5 hunks)src/main/java/com/iemr/mmu/service/dataSyncActivity/DownloadDataFromServerImpl.java
(4 hunks)src/main/java/com/iemr/mmu/service/dataSyncActivity/DownloadDataFromServerTransactionalImpl.java
(5 hunks)src/main/java/com/iemr/mmu/service/dataSyncActivity/UploadDataToServerImpl.java
(5 hunks)src/main/java/com/iemr/mmu/service/registrar/RegistrarServiceImpl.java
(7 hunks)src/main/java/com/iemr/mmu/service/tele_consultation/TeleConsultationServiceImpl.java
(4 hunks)
β° Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: Analyze (java)
π Additional comments (21)
src/main/java/com/iemr/mmu/service/dataSyncActivity/UploadDataToServerImpl.java (3)
43-45
: Necessary imports added for JWT token extraction.These imports enable the use of RequestContextHolder and ServletRequestAttributes for accessing the current HTTP request, along with CookieUtil for extracting JWT tokens from cookies.
Also applies to: 54-56
87-88
: CookieUtil dependency properly injected.The
CookieUtil
is correctly autowired to allow extraction of JWT tokens from cookies.
352-355
: Properly implemented JWT token extraction and header inclusion.The code correctly:
- Retrieves the current HTTP request using RequestContextHolder
- Extracts the JWT token from cookies using the cookieUtil
- Adds the token to the request headers, resolving the 401 unauthorized issue
This implementation ensures proper authentication when making API calls to the data sync upload URL.
Also applies to: 380-380
src/main/java/com/iemr/mmu/service/tele_consultation/TeleConsultationServiceImpl.java (3)
45-47
: Necessary imports added for JWT token handling.These imports enable access to the current HTTP request context and cookie utilities needed for JWT token extraction.
Also applies to: 61-65
79-80
: CookieUtil dependency properly injected.The
CookieUtil
is correctly autowired for JWT token extraction from cookies.
195-198
: JWT token extraction and header inclusion implemented correctly.This implementation:
- Obtains the current HTTP request from the RequestContextHolder
- Extracts the JWT token from cookies using cookieUtil
- Adds the token to the request headers when calling the teleconsultation specialist slot cancellation API
This ensures proper authentication for the external API call, resolving potential 401 unauthorized errors.
Also applies to: 201-201
src/main/java/com/iemr/mmu/service/cancerScreening/CSCarestreamServiceImpl.java (3)
42-44
: Required imports added for JWT token handling.These imports provide the necessary classes for accessing the current HTTP request and extracting JWT tokens from cookies.
Also applies to: 46-48
56-57
: CookieUtil dependency properly injected.The CookieUtil is correctly autowired for JWT token extraction.
64-67
: JWT token extraction and header inclusion properly implemented.The implementation correctly:
- Retrieves the current HTTP request context
- Extracts the JWT token from cookies
- Adds the token to the request headers for the mammography request API call
This ensures proper authentication when interacting with the Carestream service.
Also applies to: 72-72
src/main/java/com/iemr/mmu/service/dataSyncActivity/DownloadDataFromServerImpl.java (4)
46-47
: Required imports added for JWT token handling.These imports enable access to the current HTTP request and cookie utilities for JWT token extraction.
Also applies to: 55-55, 58-58
73-74
: CookieUtil dependency properly injected.The CookieUtil is correctly autowired for JWT token extraction from cookies.
188-191
: JWT token extraction and header inclusion implemented in downloadDataFromServer method.The implementation correctly retrieves the current HTTP request, extracts the JWT token from cookies, and adds it to the request headers when downloading master data from the server.
Also applies to: 196-196
345-348
: JWT token handling implemented in callCentralAPIToGenerateBenIDAndimportToLocal method.The code correctly implements JWT token extraction and includes it in the request headers when calling the central API for beneficiary ID generation, ensuring proper authentication.
Also applies to: 353-353
src/main/java/com/iemr/mmu/service/dataSyncActivity/DownloadDataFromServerTransactionalImpl.java (2)
81-82
: New dependency injection for JWT token handling.The CookieUtil injection enables the extraction of JWT tokens from cookies to solve the 401 unauthorized issue.
253-253
: Added JWT token to request headers.The JWT token is correctly added to the Cookie header for authentication with the server.
src/main/java/com/iemr/mmu/service/common/transaction/CommonDoctorServiceImpl.java (2)
114-115
: New dependency injection for JWT token handling.The CookieUtil injection enables the extraction of JWT tokens from cookies to solve the 401 unauthorized issue.
915-915
: Added JWT token to request headers.The JWT token is correctly added to the Cookie header for authentication with the server.
src/main/java/com/iemr/mmu/service/common/transaction/CommonServiceImpl.java (2)
132-133
: New dependency injection for JWT token handling.The CookieUtil injection enables the extraction of JWT tokens from cookies to solve the 401 unauthorized issue.
649-649
: Added JWT token to request headers.The JWT token is correctly added to the Cookie header for authentication with the server.
src/main/java/com/iemr/mmu/service/registrar/RegistrarServiceImpl.java (2)
112-113
: New dependency injection for JWT token handling.The CookieUtil injection enables the extraction of JWT tokens from cookies to solve the 401 unauthorized issue.
707-707
: Added JWT token to request headers.The JWT token is correctly added to the Cookie header for authentication with the server.
HttpServletRequest requestHeader = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) | ||
.getRequest(); | ||
String jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader); | ||
SyncUploadDataDigester syncUploadDataDigester = new SyncUploadDataDigester(schemaName, tableName, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling for RequestContextHolder.
The current implementation assumes RequestContextHolder.getRequestAttributes() will never return null, which could happen in non-HTTP contexts (background jobs, scheduled tasks, etc.).
Add null check to prevent NullPointerException:
- HttpServletRequest requestHeader = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
- .getRequest();
- String jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader);
+ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
+ String jwtTokenFromCookie = null;
+ if (requestAttributes != null) {
+ HttpServletRequest requestHeader = requestAttributes.getRequest();
+ jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader);
+ }
π Committable suggestion
βΌοΈ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
HttpServletRequest requestHeader = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) | |
.getRequest(); | |
String jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader); | |
SyncUploadDataDigester syncUploadDataDigester = new SyncUploadDataDigester(schemaName, tableName, | |
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | |
String jwtTokenFromCookie = null; | |
if (requestAttributes != null) { | |
HttpServletRequest requestHeader = requestAttributes.getRequest(); | |
jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader); | |
} | |
SyncUploadDataDigester syncUploadDataDigester = new SyncUploadDataDigester(schemaName, tableName, |
HttpServletRequest requestHeader = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) | ||
.getRequest(); | ||
String jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader); | ||
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling for RequestContextHolder.
The current implementation assumes RequestContextHolder.getRequestAttributes() will never return null, which could happen in non-HTTP contexts.
Add null check to prevent NullPointerException:
- HttpServletRequest requestHeader = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
- .getRequest();
- String jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader);
+ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
+ String jwtTokenFromCookie = null;
+ if (requestAttributes != null) {
+ HttpServletRequest requestHeader = requestAttributes.getRequest();
+ jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader);
+ }
π Committable suggestion
βΌοΈ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
HttpServletRequest requestHeader = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) | |
.getRequest(); | |
String jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader); | |
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); | |
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | |
String jwtTokenFromCookie = null; | |
if (requestAttributes != null) { | |
HttpServletRequest requestHeader = requestAttributes.getRequest(); | |
jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader); | |
} | |
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); |
HttpServletRequest requestHeader = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) | ||
.getRequest(); | ||
String jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader); | ||
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling for RequestContextHolder.
The current implementation assumes RequestContextHolder.getRequestAttributes() will never return null, which could happen in non-HTTP contexts.
Add null check to prevent NullPointerException:
- HttpServletRequest requestHeader = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
- .getRequest();
- String jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader);
+ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
+ String jwtTokenFromCookie = null;
+ if (requestAttributes != null) {
+ HttpServletRequest requestHeader = requestAttributes.getRequest();
+ jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader);
+ }
π Committable suggestion
βΌοΈ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
HttpServletRequest requestHeader = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) | |
.getRequest(); | |
String jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader); | |
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); | |
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | |
String jwtTokenFromCookie = null; | |
if (requestAttributes != null) { | |
HttpServletRequest requestHeader = requestAttributes.getRequest(); | |
jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader); | |
} | |
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); |
HttpServletRequest requestHeader = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) | ||
.getRequest(); | ||
String jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader); | ||
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling for RequestContextHolder.
The current implementation assumes RequestContextHolder.getRequestAttributes() will never return null, which could happen in non-HTTP contexts.
Add null check to prevent NullPointerException:
- HttpServletRequest requestHeader = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
- .getRequest();
- String jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader);
+ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
+ String jwtTokenFromCookie = null;
+ if (requestAttributes != null) {
+ HttpServletRequest requestHeader = requestAttributes.getRequest();
+ jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader);
+ }
π Committable suggestion
βΌοΈ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
HttpServletRequest requestHeader = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) | |
.getRequest(); | |
String jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader); | |
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); | |
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | |
String jwtTokenFromCookie = null; | |
if (requestAttributes != null) { | |
HttpServletRequest requestHeader = requestAttributes.getRequest(); | |
jwtTokenFromCookie = cookieUtil.getJwtTokenFromCookie(requestHeader); | |
} | |
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks fine
401 unauthorized issue resolved (PSMRI#70)
π Description
JIRA ID:
Please provide a summary of the change and the motivation behind it. Include relevant context and details.
β Type of Change
βΉοΈ Additional Information
Please describe how the changes were tested, and include any relevant screenshots, logs, or other information that provides additional context.
Summary by CodeRabbit
actions/upload-artifact
GitHub Action from version 3 to version 4.