forked from codesquad-members-2023/FoodyMoody-team-06
-
Notifications
You must be signed in to change notification settings - Fork 3
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
43 changed files
with
1,386 additions
and
330 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
48 changes: 48 additions & 0 deletions
48
be/src/main/java/com/foodymoody/be/auth/util/ClaimUtil.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,48 @@ | ||
package com.foodymoody.be.auth.util; | ||
|
||
import com.foodymoody.be.common.exception.ClaimNotFoundException; | ||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.IncorrectClaimException; | ||
import io.jsonwebtoken.JwtParser; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.RequiredTypeException; | ||
import io.jsonwebtoken.security.Keys; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import javax.crypto.SecretKey; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ClaimUtil { | ||
|
||
private String secret; | ||
private SecretKey secretKey; | ||
|
||
public ClaimUtil(@Value("${jwt.token.secret}") String secret){ | ||
this.secret = secret; | ||
this.secretKey = Keys.hmacShaKeyFor(secret.getBytes()); | ||
} | ||
|
||
public Map<String, Object> createClaim(String key, String value) { | ||
return Map.of(key, value); | ||
} | ||
|
||
public <T> T getClaim(Claims claims, String key, Class<T> type) { | ||
try { | ||
T claim = claims.get(key, type); | ||
if (Objects.isNull(claim)) { | ||
throw new ClaimNotFoundException(); | ||
} | ||
return claim; | ||
} catch (RequiredTypeException | ClassCastException e) { | ||
throw new IncorrectClaimException(null, claims, key); | ||
} | ||
} | ||
|
||
public Claims extractClaims(String token) { | ||
JwtParser parser = Jwts.parserBuilder().setSigningKey(secretKey).build(); | ||
return parser.parseClaimsJws(token).getBody(); | ||
} | ||
|
||
} |
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
8 changes: 8 additions & 0 deletions
8
be/src/main/java/com/foodymoody/be/common/exception/DuplicateMoodException.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,8 @@ | ||
package com.foodymoody.be.common.exception; | ||
|
||
public class DuplicateMoodException extends BusinessException { | ||
|
||
public DuplicateMoodException() { | ||
super(ErrorMessage.DUPLICATE_MOOD); | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
be/src/main/java/com/foodymoody/be/common/exception/IncorrectMemberPasswordException.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
2 changes: 1 addition & 1 deletion
2
be/src/main/java/com/foodymoody/be/common/exception/MemberNotFoundException.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
15 changes: 15 additions & 0 deletions
15
be/src/main/java/com/foodymoody/be/common/exception/ResourceNotFoundException.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,15 @@ | ||
package com.foodymoody.be.common.exception; | ||
|
||
public abstract class ResourceNotFoundException extends RuntimeException{ | ||
|
||
private final ErrorMessage errorMessage; | ||
|
||
protected ResourceNotFoundException(ErrorMessage errorMessage) { | ||
super(errorMessage.getMessage()); | ||
this.errorMessage = errorMessage; | ||
} | ||
|
||
public String getCode() { | ||
return errorMessage.getCode(); | ||
} | ||
} |
14 changes: 10 additions & 4 deletions
14
be/src/main/java/com/foodymoody/be/common/exception/UnauthorizedException.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 |
---|---|---|
@@ -1,9 +1,15 @@ | ||
package com.foodymoody.be.common.exception; | ||
|
||
public class UnauthorizedException extends BusinessException{ | ||
public class UnauthorizedException extends RuntimeException{ | ||
|
||
// TODO 적절한 표준 예외로 리팩토링 | ||
public UnauthorizedException() { | ||
super(ErrorMessage.UNAUTHORIZED); | ||
private final ErrorMessage errorMessage; | ||
|
||
protected UnauthorizedException(ErrorMessage errorMessage) { | ||
super(errorMessage.getMessage()); | ||
this.errorMessage = errorMessage; | ||
} | ||
|
||
public String getCode() { | ||
return errorMessage.getCode(); | ||
} | ||
} |
Oops, something went wrong.