Skip to content

Commit

Permalink
refactor: 기존의 TokenService를 대체
Browse files Browse the repository at this point in the history
  • Loading branch information
SJ70 committed Jul 25, 2024
1 parent 4d80ff6 commit 5d11524
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.j9.bestmoments.domain.Member;
import com.j9.bestmoments.service.MemberService;
import com.j9.bestmoments.service.TokenService;
import com.j9.bestmoments.service.Token_Service;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/j9/bestmoments/domain/AccessToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.data.redis.core.RedisHash;

@RedisHash(value = "AccessToken", timeToLive = TokenExpiration.ACCESS_TOKEN)
public class AccessToken extends Token_ {
public class AccessToken extends Token {

public AccessToken(Member member, String token) {
super(member, token);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/j9/bestmoments/domain/RefreshToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.data.redis.core.RedisHash;

@RedisHash(value = "RefreshToken", timeToLive = TokenExpiration.REFRESH_TOKEN)
public class RefreshToken extends Token_ {
public class RefreshToken extends Token {

public RefreshToken(Member member, String token) {
super(member, token);
Expand Down
37 changes: 10 additions & 27 deletions src/main/java/com/j9/bestmoments/domain/Token.java
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();
}

}
23 changes: 0 additions & 23 deletions src/main/java/com/j9/bestmoments/domain/Token_.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
if (token != null && jwtTokenProvider.validateToken(token)) {
Authentication authentication = jwtTokenProvider.getAuthentication(token);
SecurityContextHolder.getContext().setAuthentication(authentication);
tokenService.checkExpired(token);
tokenService.findByToken(token);
}

chain.doFilter(request, response);
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/j9/bestmoments/repository/TokenRepository.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.j9.bestmoments.repository;

import com.j9.bestmoments.domain.Token_;
import com.j9.bestmoments.domain.Token;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface Token_Repository extends CrudRepository<Token_, String> {
public interface Token_Repository extends CrudRepository<Token, String> {

boolean existsByToken(String token);

Expand Down
72 changes: 29 additions & 43 deletions src/main/java/com/j9/bestmoments/service/TokenService.java
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 src/main/java/com/j9/bestmoments/service/Token_Service.java

This file was deleted.

0 comments on commit 5d11524

Please sign in to comment.