-
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.
Browse files
Browse the repository at this point in the history
[FEAT] 카카오 로그인 구현 및 사용자 인증 방식 개선
- Loading branch information
Showing
18 changed files
with
287 additions
and
71 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
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
54 changes: 54 additions & 0 deletions
54
src/main/java/org/websoso/WSSServer/config/jwt/JWTUtil.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,54 @@ | ||
package org.websoso.WSSServer.config.jwt; | ||
|
||
import static org.websoso.WSSServer.config.jwt.JwtProvider.CLAIM_USER_ID; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.ExpiredJwtException; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.MalformedJwtException; | ||
import io.jsonwebtoken.UnsupportedJwtException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class JWTUtil { | ||
|
||
private final JwtProvider jwtProvider; | ||
|
||
public Long getUserIdFromJwt(String token) { | ||
Claims claims = getClaim(token); | ||
return Long.valueOf(claims.get(CLAIM_USER_ID).toString()); | ||
} | ||
|
||
public JwtValidationType validateJWT(String token) { | ||
try { | ||
final Claims claims = getClaim(token); | ||
String tokenType = claims.getSubject(); | ||
if (tokenType.equals("access")) { | ||
return JwtValidationType.VALID_ACCESS; | ||
} | ||
return JwtValidationType.VALID_REFRESH; | ||
} catch (MalformedJwtException ex) { | ||
return JwtValidationType.INVALID_TOKEN; | ||
} catch (ExpiredJwtException ex) { | ||
String tokenType = ex.getClaims().getSubject(); | ||
if (tokenType.equals("access")) { | ||
return JwtValidationType.EXPIRED_ACCESS; | ||
} | ||
return JwtValidationType.EXPIRED_REFRESH; | ||
} catch (UnsupportedJwtException ex) { | ||
return JwtValidationType.UNSUPPORTED_TOKEN; | ||
} catch (IllegalArgumentException ex) { | ||
return JwtValidationType.EMPTY_TOKEN; | ||
} | ||
} | ||
|
||
private Claims getClaim(final String token) { | ||
return Jwts.parserBuilder() | ||
.setSigningKey(jwtProvider.getSigningKey()) | ||
.build() | ||
.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
13 changes: 7 additions & 6 deletions
13
src/main/java/org/websoso/WSSServer/config/jwt/JwtValidationType.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,10 +1,11 @@ | ||
package org.websoso.WSSServer.config.jwt; | ||
|
||
public enum JwtValidationType { | ||
VALID_TOKEN, // 유효한 JWT | ||
INVALID_SIGNATURE, // 유효하지 않은 서명 | ||
INVALID_TOKEN, // 유효하지 않은 토큰 | ||
EXPIRED_TOKEN, // 만료된 토큰 | ||
UNSUPPORTED_TOKEN, // 지원하지 않는 형식의 토큰 | ||
EMPTY_TOKEN // 빈 JWT | ||
VALID_ACCESS, | ||
VALID_REFRESH, | ||
INVALID_TOKEN, | ||
EXPIRED_ACCESS, | ||
EXPIRED_REFRESH, | ||
UNSUPPORTED_TOKEN, | ||
EMPTY_TOKEN | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/org/websoso/WSSServer/controller/AuthController.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,27 @@ | ||
package org.websoso.WSSServer.controller; | ||
|
||
import static org.springframework.http.HttpStatus.OK; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.websoso.WSSServer.dto.auth.ReissueRequest; | ||
import org.websoso.WSSServer.dto.auth.ReissueResponse; | ||
import org.websoso.WSSServer.service.AuthService; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class AuthController { | ||
|
||
private final AuthService authService; | ||
|
||
@PostMapping("/reissue") | ||
public ResponseEntity<ReissueResponse> reissue(@RequestBody ReissueRequest reissueRequest) { | ||
String refreshToken = reissueRequest.refreshToken(); | ||
return ResponseEntity | ||
.status(OK) | ||
.body(authService.reissue(refreshToken)); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/org/websoso/WSSServer/domain/RefreshToken.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 org.websoso.WSSServer.domain; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.redis.core.RedisHash; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@RedisHash(value = "refreshToken", timeToLive = 60 * 60 * 24 * 7 * 2) | ||
public class RefreshToken { | ||
|
||
@Id | ||
private String refreshToken; | ||
|
||
private Long userId; | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/org/websoso/WSSServer/dto/auth/ReissueRequest.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,6 @@ | ||
package org.websoso.WSSServer.dto.auth; | ||
|
||
public record ReissueRequest( | ||
String refreshToken | ||
) { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/websoso/WSSServer/dto/auth/ReissueResponse.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,11 @@ | ||
package org.websoso.WSSServer.dto.auth; | ||
|
||
public record ReissueResponse( | ||
String Authorization, | ||
String refreshToken | ||
) { | ||
|
||
public static ReissueResponse of(String accessToken, String refreshToken) { | ||
return new ReissueResponse(accessToken, refreshToken); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/websoso/WSSServer/exception/error/CustomAuthError.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 org.websoso.WSSServer.exception.error; | ||
|
||
import static org.springframework.http.HttpStatus.UNAUTHORIZED; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
import org.websoso.WSSServer.exception.common.ICustomError; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum CustomAuthError implements ICustomError { | ||
|
||
INVALID_TOKEN("AUTH-001", "유효하지 않은 토큰입니다.", UNAUTHORIZED); | ||
|
||
private final String code; | ||
private final String description; | ||
private final HttpStatus statusCode; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/org/websoso/WSSServer/exception/exception/CustomAuthException.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,13 @@ | ||
package org.websoso.WSSServer.exception.exception; | ||
|
||
import lombok.Getter; | ||
import org.websoso.WSSServer.exception.common.AbstractCustomException; | ||
import org.websoso.WSSServer.exception.error.CustomAuthError; | ||
|
||
@Getter | ||
public class CustomAuthException extends AbstractCustomException { | ||
|
||
public CustomAuthException(CustomAuthError customAuthError, String message) { | ||
super(customAuthError, message); | ||
} | ||
} |
Oops, something went wrong.