-
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
10 changed files
with
44 additions
and
178 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,23 @@ | ||
package com.j9.bestmoments.domain; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.annotation.Id; | ||
import java.util.UUID; | ||
import lombok.Getter; | ||
import org.springframework.data.redis.core.RedisHash; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor | ||
@RedisHash("Token") | ||
public class Token { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private Long id; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "member_id", referencedColumnName = "id") | ||
private Member member; | ||
|
||
private String refreshToken; | ||
private String accessToken; | ||
|
||
@Builder | ||
public Token(Member member, String refreshToken, String accessToken) { | ||
this.member = member; | ||
this.refreshToken = refreshToken; | ||
this.accessToken = accessToken; | ||
} | ||
private String token; | ||
private UUID memberId; | ||
|
||
public void setAccessToken(String accessToken) { | ||
this.accessToken = accessToken; | ||
public Token(Member member, String token) { | ||
this.token = token; | ||
this.memberId = member.getId(); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
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
12 changes: 0 additions & 12 deletions
12
src/main/java/com/j9/bestmoments/repository/TokenRepository.java
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
src/main/java/com/j9/bestmoments/repository/Token_Repository.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
72 changes: 29 additions & 43 deletions
72
src/main/java/com/j9/bestmoments/service/TokenService.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,80 +1,66 @@ | ||
package com.j9.bestmoments.service; | ||
|
||
import com.j9.bestmoments.domain.AccessToken; | ||
import com.j9.bestmoments.domain.Member; | ||
import com.j9.bestmoments.domain.RefreshToken; | ||
import com.j9.bestmoments.domain.Token; | ||
import com.j9.bestmoments.dto.response.LoginDto; | ||
import com.j9.bestmoments.jwt.JwtTokenProvider; | ||
import com.j9.bestmoments.repository.TokenRepository; | ||
import com.j9.bestmoments.repository.Token_Repository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class TokenService { | ||
|
||
private final TokenRepository tokenRepository; | ||
private final Token_Repository tokenRepository; | ||
private final JwtTokenProvider jwtTokenProvider; | ||
private final MemberService memberService; | ||
|
||
@Transactional | ||
public LoginDto create(Member member) { | ||
String accessToken = jwtTokenProvider.generateAccessToken(member); | ||
String refreshToken = jwtTokenProvider.generateRefreshToken(member); | ||
Token token = Token.builder() | ||
.member(member) | ||
.accessToken(accessToken) | ||
.refreshToken(refreshToken) | ||
.build(); | ||
tokenRepository.save(token); | ||
log.error(token.getAccessToken()); | ||
log.error(token.getRefreshToken()); | ||
log.error(tokenRepository.findAll().get(0).getAccessToken()); | ||
String accessToken = createAccessToken(member); | ||
String refreshToken = createRefreshToken(member); | ||
log.info("토큰 발급됨\n - accessToken: {}\n - refreshToken: {}", accessToken, refreshToken); | ||
return new LoginDto("Bearer", accessToken, refreshToken, member.getDeletedAt()); | ||
} | ||
|
||
public Token findByAnyToken(String token) { | ||
return tokenRepository.findByAccessToken(resolveToken(token)) | ||
.or(() -> tokenRepository.findByRefreshToken(resolveToken(token))) | ||
.orElseThrow(() -> new AccessDeniedException("존재하지 않거나 만료된 토큰입니다.")); | ||
} | ||
|
||
public Token findByAccessToken(String accessToken) { | ||
return tokenRepository.findByAccessToken(resolveToken(accessToken)) | ||
.orElseThrow(() -> new AccessDeniedException("존재하지 않거나 만료된 액세스 토큰입니다.")); | ||
} | ||
|
||
public Token findByRefreshToken(String refreshToken) { | ||
return tokenRepository.findByRefreshToken(resolveToken(refreshToken)) | ||
.orElseThrow(() -> new AccessDeniedException("존재하지 않거나 만료된 리프래시 토큰입니다.")); | ||
private String createAccessToken(Member member) { | ||
String accessTokenValue = jwtTokenProvider.generateAccessToken(member); | ||
AccessToken accessToken = new AccessToken(member, accessTokenValue); | ||
tokenRepository.save(accessToken); | ||
return accessTokenValue; | ||
} | ||
|
||
public void checkExpired(String token) { | ||
findByAnyToken(token); | ||
private String createRefreshToken(Member member) { | ||
String refreshTokenValue = jwtTokenProvider.generateRefreshToken(member); | ||
RefreshToken refreshToken = new RefreshToken(member, refreshTokenValue); | ||
tokenRepository.save(refreshToken); | ||
return refreshTokenValue; | ||
} | ||
|
||
@Transactional | ||
public void expire(String token) { | ||
Token foundToken = findByAnyToken(token); | ||
Token foundToken = this.findByToken(token); | ||
tokenRepository.delete(foundToken); | ||
} | ||
|
||
@Transactional | ||
public String refresh(String refreshToken) { | ||
Token foundToken = findByRefreshToken(refreshToken); | ||
String accessToken = jwtTokenProvider.generateAccessToken(foundToken.getMember()); | ||
foundToken.setAccessToken(accessToken); | ||
tokenRepository.save(foundToken); | ||
return accessToken; | ||
public Token findByToken(String token) { | ||
return tokenRepository.findById(token) | ||
.orElseThrow(() -> new AccessDeniedException("만료되거나 발급되지 않은 토큰입니다.")); | ||
} | ||
|
||
private String resolveToken(String token) { | ||
if (token.startsWith("Bearer")) { | ||
return token.substring(7); | ||
} | ||
return token; | ||
@Transactional | ||
public String refresh(String refreshToken) { | ||
Token foundToken = this.findByToken(refreshToken); | ||
Member member = memberService.findById(foundToken.getMemberId()); | ||
String newAccessToken = createAccessToken(member); | ||
return newAccessToken; | ||
} | ||
|
||
} |
67 changes: 0 additions & 67 deletions
67
src/main/java/com/j9/bestmoments/service/Token_Service.java
This file was deleted.
Oops, something went wrong.