-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from sh1220/dev
Refactor: merge & conflict resolve
- Loading branch information
Showing
14 changed files
with
266 additions
and
26 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
71 changes: 71 additions & 0 deletions
71
src/main/java/store/itpick/backend/common/interceptor/JwtAuthRefreshInterceptor.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,71 @@ | ||
package store.itpick.backend.common.interceptor; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import store.itpick.backend.common.exception.jwt.bad_request.JwtNoTokenException; | ||
import store.itpick.backend.common.exception.jwt.bad_request.JwtUnsupportedTokenException; | ||
import store.itpick.backend.common.exception.jwt.unauthorized.JwtExpiredTokenException; | ||
import store.itpick.backend.common.exception.jwt.unauthorized.JwtInvalidTokenException; | ||
import store.itpick.backend.jwt.JwtProvider; | ||
import store.itpick.backend.service.UserService; | ||
|
||
import static store.itpick.backend.common.response.status.BaseExceptionResponseStatus.*; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtAuthRefreshInterceptor implements HandlerInterceptor { | ||
|
||
private static final String JWT_TOKEN_PREFIX = "Bearer "; | ||
private final JwtProvider jwtProvider; | ||
private final UserService userService; | ||
|
||
// 컨트롤러 호출전에 JWT 검증 | ||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
|
||
String refreshToken = resolveRefreshToken(request); | ||
validateRefreshToken(refreshToken); | ||
|
||
String email = jwtProvider.getPrincipal(refreshToken); | ||
validatePayload(email); | ||
|
||
long userId = userService.getUserIdByEmail(email); | ||
request.setAttribute("userId", userId); | ||
return true; | ||
|
||
} | ||
|
||
private String resolveRefreshToken(HttpServletRequest request) { | ||
String token = request.getHeader(HttpHeaders.AUTHORIZATION); | ||
validateToken(token); | ||
return token.substring(JWT_TOKEN_PREFIX.length()); | ||
} | ||
|
||
private void validateToken(String token) { | ||
if (token == null) { | ||
throw new JwtNoTokenException(TOKEN_NOT_FOUND); | ||
} | ||
if (!token.startsWith(JWT_TOKEN_PREFIX)) { | ||
throw new JwtUnsupportedTokenException(UNSUPPORTED_TOKEN_TYPE); | ||
} | ||
} | ||
|
||
private void validateRefreshToken(String accessToken) { | ||
if (jwtProvider.isExpiredToken(accessToken)) { | ||
throw new JwtExpiredTokenException(EXPIRED_TOKEN); | ||
} | ||
} | ||
|
||
private void validatePayload(String email) { | ||
if (email == null) { | ||
throw new JwtInvalidTokenException(INVALID_TOKEN); | ||
} | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package store.itpick.backend.dto.auth; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class JwtDTO { | ||
private String accessToken; | ||
private String refreshToken; | ||
|
||
public JwtDTO(String accessToken, String refreshToken) { | ||
this.accessToken = accessToken; | ||
this.refreshToken = refreshToken; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -8,6 +8,6 @@ | |
public class LoginResponse { | ||
|
||
private long userId; | ||
private String jwt; | ||
private JwtDTO jwt; | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/store/itpick/backend/dto/auth/LogoutRequest.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 store.itpick.backend.dto.auth; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
public class LogoutRequest { | ||
|
||
@NotBlank(message = "refreshToken: {NotBlank}") | ||
private String refreshToken; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/store/itpick/backend/dto/auth/RefreshRequest.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 store.itpick.backend.dto.auth; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
public class RefreshRequest { | ||
|
||
@NotBlank(message = "refreshToken: {NotBlank}") | ||
private String refreshToken; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/store/itpick/backend/dto/auth/RefreshResponse.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 store.itpick.backend.dto.auth; | ||
|
||
import lombok.AllArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
public class RefreshResponse { | ||
private String accessToken; | ||
} |
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
Oops, something went wrong.