Skip to content

Commit

Permalink
refactor : 도메인과 소통이 없는 TokenService의 이름을 TokenUtil로 변경 (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
binary-ho committed Mar 12, 2024
1 parent ef23d2a commit 12bc99b
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import gdsc.binaryho.imhere.core.member.infrastructure.MemberRepository;
import gdsc.binaryho.imhere.security.filter.JwtAuthorizationFilter;
import gdsc.binaryho.imhere.security.jwt.TokenPropertyHolder;
import gdsc.binaryho.imhere.security.jwt.TokenService;
import gdsc.binaryho.imhere.security.jwt.TokenUtil;
import gdsc.binaryho.imhere.security.oauth.CustomOAuth2SuccessHandler;
import gdsc.binaryho.imhere.security.oauth.CustomOAuth2UserService;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -41,7 +41,7 @@ public class SecurityConfig {
private final CustomOAuth2SuccessHandler customOAuth2SuccessHandler;
private final CustomOAuth2UserService customOAuth2UserService;

private final TokenService tokenService;
private final TokenUtil tokenUtil;
private final TokenPropertyHolder tokenPropertyHolder;

@Value("${actuator.username}")
Expand Down Expand Up @@ -118,7 +118,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

http.addFilterBefore(new JwtAuthorizationFilter(
authenticationManager(authenticationConfiguration),
tokenService, memberRepository, tokenPropertyHolder),
tokenUtil, memberRepository, tokenPropertyHolder),
BasicAuthenticationFilter.class);

return http.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import gdsc.binaryho.imhere.security.jwt.Token;
import gdsc.binaryho.imhere.security.jwt.TokenPropertyHolder;
import gdsc.binaryho.imhere.security.jwt.TokenService;
import gdsc.binaryho.imhere.security.jwt.TokenUtil;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
Expand All @@ -28,7 +28,7 @@ public class JwtAuthenticationFilter extends UsernamePasswordAuthenticationFilte
private static final String HEADER_STRING = HttpHeaders.AUTHORIZATION;

private final AuthenticationManager authenticationManager;
private final TokenService tokenService;
private final TokenUtil tokenUtil;
private final TokenPropertyHolder tokenPropertyHolder;

@Override
Expand Down Expand Up @@ -67,7 +67,7 @@ public void successfulAuthentication(HttpServletRequest request,
.orElseThrow()
.toString();

Token jwtToken = tokenService.createToken(authResult.getPrincipal().toString(), grantedAuthority);
Token jwtToken = tokenUtil.createToken(authResult.getPrincipal().toString(), grantedAuthority);

String accessTokenPrefix = tokenPropertyHolder.getAccessTokenPrefix();
response.addHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.AUTHORIZATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import gdsc.binaryho.imhere.core.member.Member;
import gdsc.binaryho.imhere.core.member.infrastructure.MemberRepository;
import gdsc.binaryho.imhere.security.jwt.TokenPropertyHolder;
import gdsc.binaryho.imhere.security.jwt.TokenService;
import gdsc.binaryho.imhere.security.jwt.TokenUtil;
import gdsc.binaryho.imhere.security.principal.PrincipalDetails;
import java.io.IOException;
import java.util.Objects;
Expand All @@ -23,16 +23,16 @@ public class JwtAuthorizationFilter extends BasicAuthenticationFilter {

private static final String TOKEN_HEADER_STRING = HttpHeaders.AUTHORIZATION;

private final TokenService tokenService;
private final TokenUtil tokenUtil;
private final MemberRepository memberRepository;
private final TokenPropertyHolder tokenPropertyHolder;

public JwtAuthorizationFilter(
AuthenticationManager authenticationManager,
TokenService tokenService, MemberRepository memberRepository,
TokenUtil tokenUtil, MemberRepository memberRepository,
TokenPropertyHolder tokenPropertyHolder) {
super(authenticationManager);
this.tokenService = tokenService;
this.tokenUtil = tokenUtil;
this.memberRepository = memberRepository;
this.tokenPropertyHolder = tokenPropertyHolder;
}
Expand All @@ -49,7 +49,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse

String accessTokenPrefix = tokenPropertyHolder.getAccessTokenPrefix();
String tokenValue = jwtToken.replace(accessTokenPrefix, "");
if (tokenService.validateTokenExpirationTimeNotExpired(tokenValue)) {
if (tokenUtil.validateTokenExpirationTimeNotExpired(tokenValue)) {
setAuthentication(tokenValue);
}
chain.doFilter(request, response);
Expand All @@ -62,7 +62,7 @@ private boolean isTokenNullOrInvalidate(String token) {
}

private void setAuthentication(String jwtToken) {
Long id = tokenService.getId(jwtToken);
Long id = tokenUtil.getId(jwtToken);
Member member = memberRepository.findById(id)
.orElseThrow(() -> MemberNotFoundException.EXCEPTION);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import java.util.Date;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Component;

@Log4j2
@Service
@Component
@RequiredArgsConstructor
public class TokenService {
public class TokenUtil {

private final SeoulDateTimeHolder seoulDateTimeHolder;
private final TokenPropertyHolder tokenPropertyHolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import gdsc.binaryho.imhere.security.SignUpProcessRedirectionPath;
import gdsc.binaryho.imhere.security.jwt.Token;
import gdsc.binaryho.imhere.security.jwt.TokenPropertyHolder;
import gdsc.binaryho.imhere.security.jwt.TokenService;
import gdsc.binaryho.imhere.security.jwt.TokenUtil;
import gdsc.binaryho.imhere.util.ClientUrlUtil;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -20,7 +20,7 @@ public class CustomOAuth2SuccessHandler extends SimpleUrlAuthenticationSuccessHa

private static final String HEADER_STRING = HttpHeaders.AUTHORIZATION;

private final TokenService tokenService;
private final TokenUtil tokenUtil;
private final ClientUrlUtil clientUrlUtil;
private final TokenPropertyHolder tokenPropertyHolder;

Expand All @@ -43,7 +43,7 @@ private void setAccessToken(HttpServletResponse response, CustomOAuth2User oAuth
response.addHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.AUTHORIZATION);

String accessTokenPrefix = tokenPropertyHolder.getAccessTokenPrefix();
Token jwtToken = tokenService.createToken(oAuthUser.getMemberId(), oAuthUser.getRole());
Token jwtToken = tokenUtil.createToken(oAuthUser.getMemberId(), oAuthUser.getRole());
response.addHeader(HEADER_STRING, accessTokenPrefix + jwtToken.getAccessToken());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import gdsc.binaryho.imhere.core.member.infrastructure.MemberRepository;
import gdsc.binaryho.imhere.security.jwt.Token;
import gdsc.binaryho.imhere.security.jwt.TokenPropertyHolder;
import gdsc.binaryho.imhere.security.jwt.TokenService;
import gdsc.binaryho.imhere.security.jwt.TokenUtil;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -35,7 +35,7 @@ public class SecurityConfigTest {
private MockMvc mockMvc;

@Autowired
private TokenService tokenService;
private TokenUtil tokenUtil;

@MockBean
private MemberRepository memberRepository;
Expand All @@ -56,7 +56,7 @@ public class SecurityConfigTest {
public void 토큰을_통해_인가_할_수_있다() throws Exception {
given(memberRepository.findById(any()))
.willReturn(Optional.of(MOCK_STUDENT));
Token token = tokenService.createToken(1L, Role.STUDENT);
Token token = tokenUtil.createToken(1L, Role.LECTURER);

String accessTokenPrefix = tokenPropertyHolder.getAccessTokenPrefix();
mockMvc.perform(get("/api/lecture")
Expand All @@ -70,7 +70,7 @@ public class SecurityConfigTest {
public void 권한이_없는_토큰_요청은_403_응답을_반환한다() throws Exception {
given(memberRepository.findById(any()))
.willReturn(Optional.of(MOCK_STUDENT));
Token token = tokenService.createToken(1L, Role.STUDENT);
Token token = tokenUtil.createToken(1L, Role.STUDENT);

String accessTokenPrefix = tokenPropertyHolder.getAccessTokenPrefix();
mockMvc.perform(post("/api/admin/role/1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
import java.util.UUID;
import org.junit.jupiter.api.Test;

public class TokenServiceTest {
public class TokenUtilTest {

private static final Role ROLE = MOCK_STUDENT.getRole();
private static final String SECRET = "TEST_SECRET";
private static final long ACCESS_TOKEN_EXPIRATION_TIME = 1000L * 60L * 20L;
private static final long TIME_NOW = FixedSeoulTimeHolder.FIXED_MILLISECONDS;

TokenPropertyHolder tokenPropertyHolder = new FakeTokenPropertyHolder(SECRET, Duration.ofDays(999L));
TokenService tokenService = new TokenService(new FixedSeoulTimeHolder(), tokenPropertyHolder);
TokenUtil tokenUtil = new TokenUtil(new FixedSeoulTimeHolder(), tokenPropertyHolder);

@Test
void 이메일과_권한을_넣어_토큰을_만들_수_있다() throws JsonProcessingException {
Token token = tokenService.createToken(UNIV_ID, ROLE.getKey());
Token token = tokenUtil.createToken(UNIV_ID, ROLE.getKey());
String accessToken = token.getAccessToken();

String[] splitToken = accessToken.split("\\.");
Expand All @@ -51,7 +51,7 @@ public class TokenServiceTest {
@Test
void 맴버_아이디와_권한을_넣어_토큰을_만들_수_있다() throws JsonProcessingException {
Long id = MOCK_STUDENT.getId();
Token token = tokenService.createToken(id, ROLE);
Token token = tokenUtil.createToken(id, ROLE);
String accessToken = token.getAccessToken();

String[] splitToken = accessToken.split("\\.");
Expand Down Expand Up @@ -84,7 +84,7 @@ public class TokenServiceTest {
// when
// then
assertThat(
tokenService.validateTokenExpirationTimeNotExpired(token.getAccessToken()))
tokenUtil.validateTokenExpirationTimeNotExpired(token.getAccessToken()))
.isNotNull();
}

Expand All @@ -105,7 +105,7 @@ public class TokenServiceTest {
// when
// then
assertThat(
tokenService.validateTokenExpirationTimeNotExpired(token.getAccessToken()))
tokenUtil.validateTokenExpirationTimeNotExpired(token.getAccessToken()))
.isTrue();
}

Expand All @@ -126,7 +126,7 @@ public class TokenServiceTest {
// when
// then
assertThat(
tokenService.validateTokenExpirationTimeNotExpired(token.getAccessToken()))
tokenUtil.validateTokenExpirationTimeNotExpired(token.getAccessToken()))
.isFalse();
}

Expand All @@ -138,7 +138,7 @@ public class TokenServiceTest {
// when
// then
assertThat(
tokenService.validateTokenExpirationTimeNotExpired(token.getAccessToken()))
tokenUtil.validateTokenExpirationTimeNotExpired(token.getAccessToken()))
.isFalse();
}

Expand All @@ -150,7 +150,7 @@ public class TokenServiceTest {
// when
// then
assertThat(
tokenService.validateTokenExpirationTimeNotExpired(token.getAccessToken()))
tokenUtil.validateTokenExpirationTimeNotExpired(token.getAccessToken()))
.isFalse();
}

Expand All @@ -162,7 +162,7 @@ public class TokenServiceTest {
// when
// then
assertThat(
tokenService.validateTokenExpirationTimeNotExpired(token.getAccessToken()))
tokenUtil.validateTokenExpirationTimeNotExpired(token.getAccessToken()))
.isFalse();
}
}
2 changes: 1 addition & 1 deletion src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ token:
prefix: ""
header-string: ""
access-token-prefix: ""
access-token-expire-minute: 1
access-token-expire-minute: 9999

admin:
univ-id: ENC(MnnJJNaW4by9rR1oo90AfA==)
Expand Down

0 comments on commit 12bc99b

Please sign in to comment.