-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
9 changed files
with
115 additions
and
67 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
17 changes: 17 additions & 0 deletions
17
business-service/src/main/java/com/ns/business/JwtExceptionHandler.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,17 @@ | ||
package com.ns.business; | ||
|
||
import io.jsonwebtoken.ExpiredJwtException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
public class JwtExceptionHandler { | ||
|
||
@ExceptionHandler(ExpiredJwtException.class) | ||
public ResponseEntity<String> handleExpiredJwtException(ExpiredJwtException ex) { | ||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED) | ||
.body("JWT token has expired"); | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
business-service/src/main/java/com/ns/business/adpater/out/JwtTokenProvider.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,45 @@ | ||
package com.ns.business.adpater.out; | ||
|
||
import io.jsonwebtoken.*; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
|
||
@Component | ||
public class JwtTokenProvider { | ||
|
||
private String jwtSecret; // secret key | ||
|
||
public JwtTokenProvider(){ | ||
this.jwtSecret="NYd4nEtyLtcU7cpS/1HTFVmQJd7MmrP+HafWoXZjWNOL7qKccOOUfQNEx5yvG6dfdpuBeyMs9eEbRmdBrPQCNg=="; | ||
} | ||
|
||
public String getJwtToken(){ | ||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); | ||
return request.getHeader("Authorization"); | ||
} | ||
|
||
public Long getMembershipIdbyToken() { | ||
String accessToken = getJwtToken(); | ||
if(accessToken == null || accessToken.length() == 0){ | ||
throw new RuntimeException("JwtToken is Invalid."); | ||
} | ||
|
||
try { | ||
String token = accessToken.replace("Bearer ", ""); | ||
Claims claims = Jwts.parserBuilder() | ||
.setSigningKey(jwtSecret) | ||
.build() | ||
.parseClaimsJws(token) | ||
.getBody(); | ||
|
||
String membershipIdString = claims.get("sub", String.class); | ||
return Long.parseLong(membershipIdString); | ||
} catch (ExpiredJwtException ex) { | ||
throw ex; | ||
} | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
dedicated-service/src/main/java/com/ns/dedicated/JwtExceptionHandler.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,17 @@ | ||
package com.ns.dedicated; | ||
|
||
import io.jsonwebtoken.ExpiredJwtException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
public class JwtExceptionHandler { | ||
|
||
@ExceptionHandler(ExpiredJwtException.class) | ||
public ResponseEntity<String> handleExpiredJwtException(ExpiredJwtException ex) { | ||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED) | ||
.body("JWT token has expired"); | ||
} | ||
} |
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
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