-
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
Refactor/#161/인증 인가 처리 리펙토링
- Loading branch information
Showing
19 changed files
with
605 additions
and
84 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
Binary file not shown.
Binary file not shown.
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
45 changes: 45 additions & 0 deletions
45
src/main/java/space/space_spring/domain/authorization/jwt/model/JwtLoginTokenResolver.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 space.space_spring.domain.authorization.jwt.model; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.stereotype.Component; | ||
import space.space_spring.exception.jwt.bad_request.JwtNoTokenException; | ||
import space.space_spring.exception.jwt.bad_request.JwtUnsupportedTokenException; | ||
|
||
import static space.space_spring.response.status.BaseExceptionResponseStatus.TOKEN_NOT_FOUND; | ||
import static space.space_spring.response.status.BaseExceptionResponseStatus.UNSUPPORTED_TOKEN_TYPE; | ||
|
||
@Component | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class JwtLoginTokenResolver { | ||
|
||
private static final String JWT_TOKEN_PREFIX = "Bearer "; | ||
|
||
public TokenPairDTO resolveTokenPair(HttpServletRequest request) { | ||
// TODO 1. access token 파싱 | ||
String accessToken = request.getHeader(HttpHeaders.AUTHORIZATION); | ||
validateToken(accessToken); | ||
|
||
// TODO 2. refresh token 파싱 | ||
String refreshToken = request.getHeader("Authorization-refresh"); | ||
validateToken(refreshToken); | ||
|
||
// TODO 3. return | ||
return TokenPairDTO.builder() | ||
.accessToken(accessToken.substring(JWT_TOKEN_PREFIX.length())) | ||
.refreshToken(refreshToken.substring(JWT_TOKEN_PREFIX.length())) | ||
.build(); | ||
} | ||
|
||
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); | ||
} | ||
} | ||
|
||
} |
17 changes: 13 additions & 4 deletions
17
src/main/java/space/space_spring/domain/authorization/jwt/repository/JwtRepository.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,15 +1,24 @@ | ||
package space.space_spring.domain.authorization.jwt.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
import space.space_spring.entity.TokenStorage; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import space.space_spring.entity.RefreshTokenStorage; | ||
import space.space_spring.entity.User; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface JwtRepository extends JpaRepository<TokenStorage, Long> { | ||
public interface JwtRepository extends JpaRepository<RefreshTokenStorage, Long> { | ||
|
||
Optional<RefreshTokenStorage> findByUser(User user); | ||
|
||
@Modifying | ||
@Transactional | ||
@Query("DELETE FROM RefreshTokenStorage t WHERE t.user = :user") | ||
void deleteByUser(@Param("user") User user); | ||
|
||
Optional<TokenStorage> findByUser(User user); | ||
void deleteByUser(User user); | ||
} |
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
Oops, something went wrong.