Skip to content

Commit

Permalink
Merge pull request #68 from kssumin/feat/#185/redis
Browse files Browse the repository at this point in the history
[BE/FEAT] 토큰 저장소를 redis로 변경한다.
  • Loading branch information
kssumin authored Mar 9, 2024
2 parents e7d9ac4 + f217d63 commit b82b403
Show file tree
Hide file tree
Showing 36 changed files with 281 additions and 326 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:

- name: DB를 실행
run: |
docker-compose -f resources/test-develop-environment/docker-compose.yml up -d
docker-compose -f resources/local-develop-environment/docker-compose.yml up -d
sleep 20
- name: Gradle 캐싱
Expand Down
4 changes: 4 additions & 0 deletions BE/eeos/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ dependencies {
implementation 'org.flywaydb:flyway-core'
implementation 'org.flywaydb:flyway-mysql'

// redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'


// openfeign
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ services:
environment:
- ADMINER_DEFAULT_SERVER=overflow-mysql8
- ADMINER_DESIGN=nette
- ADMINER_PLUGINS=tables-filter tinymce
- ADMINER_PLUGINS=tables-filter tinymce

overflow-redis: # Redis
container_name: eeos-redis
image: redis:latest
ports:
- "16379:6379"

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.blackcompany.eeos.auth.application.domain.converter;

import com.blackcompany.eeos.auth.application.domain.OauthMemberModel;
import com.blackcompany.eeos.auth.persistence.OAuthMemberEntity;
import com.blackcompany.eeos.common.support.converter.AbstractEntityConverter;
import org.springframework.stereotype.Component;

@Component
public class OauthMemberEntityConverter
implements AbstractEntityConverter<OAuthMemberEntity, OauthMemberModel> {
@Override
public OauthMemberModel from(final OAuthMemberEntity entity) {
return OauthMemberModel.builder().oauthId(entity.getOauthId()).build();
}

@Override
public OAuthMemberEntity toEntity(final OauthMemberModel model) {
return OAuthMemberEntity.builder().oauthId(model.getOauthId()).build();
}

public OAuthMemberEntity toEntity(final String oauthId, Long memberId) {
return OAuthMemberEntity.builder().oauthId(oauthId).memberId(memberId).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@

import com.blackcompany.eeos.auth.application.domain.OauthMemberModel;
import com.blackcompany.eeos.auth.application.domain.TokenModel;
import com.blackcompany.eeos.auth.application.support.AuthenticationTokenGenerator;
import com.blackcompany.eeos.auth.application.usecase.LoginUsecase;
import com.blackcompany.eeos.auth.persistence.OauthInfoEntity;
import com.blackcompany.eeos.auth.persistence.OAuthMemberEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional
public class AuthFacadeService implements LoginUsecase {
private final OauthClientService oauthClientService;
private final AuthService authService;
private final CreateTokenService createTokenService;
private final AuthenticationTokenGenerator authenticationTokenGenerator;

@Transactional(propagation = Propagation.REQUIRED)
@Override
public TokenModel login(String oauthServerType, String authCode, String uri) {
OauthMemberModel model = oauthClientService.getOauthMember(oauthServerType, authCode, uri);
OauthInfoEntity entity = authService.login(model);
return createTokenService.execute(entity.getMemberId());
OAuthMemberEntity entity = authService.authenticate(model);
return authenticationTokenGenerator.execute(entity.getMemberId());
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
package com.blackcompany.eeos.auth.application.service;

import com.blackcompany.eeos.auth.application.domain.OauthMemberModel;
import com.blackcompany.eeos.auth.application.domain.converter.OauthInfoEntityConverter;
import com.blackcompany.eeos.auth.persistence.OauthInfoEntity;
import com.blackcompany.eeos.auth.persistence.OauthInfoRepository;
import com.blackcompany.eeos.auth.application.domain.converter.OauthMemberEntityConverter;
import com.blackcompany.eeos.auth.persistence.OAuthMemberEntity;
import com.blackcompany.eeos.auth.persistence.OAuthMemberRepository;
import com.blackcompany.eeos.member.application.model.converter.MemberEntityConverter;
import com.blackcompany.eeos.member.persistence.MemberEntity;
import com.blackcompany.eeos.member.persistence.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class AuthService {

private final OauthInfoRepository oauthInfoRepository;
private final MemberRepository memberRepository;
private final OAuthMemberRepository oAuthMemberRepository;
private final MemberEntityConverter memberEntityConverter;
private final OauthInfoEntityConverter oauthInfoEntityConverter;
private final OauthMemberEntityConverter oauthMemberEntityConverter;

@Transactional(propagation = Propagation.REQUIRED)
public OauthInfoEntity login(final OauthMemberModel model) {
return oauthInfoRepository
@Transactional
public OAuthMemberEntity authenticate(final OauthMemberModel model) {
return oAuthMemberRepository
.findByOauthId(model.getOauthId())
.orElseGet(() -> signUpMember(model));
}

private OauthInfoEntity signUpMember(final OauthMemberModel model) {
private OAuthMemberEntity signUpMember(final OauthMemberModel model) {
MemberEntity entity =
memberEntityConverter.toEntity(model.getName(), model.getOauthServerType());

MemberEntity savedMember = memberRepository.save(entity);
return createOauthInfoEntity(model.getOauthId(), savedMember.getId());
return saveOauthInfoEntity(model.getOauthId(), savedMember.getId());
}

private OauthInfoEntity createOauthInfoEntity(final String oauthId, final Long memberId) {
OauthInfoEntity entity = oauthInfoEntityConverter.toEntity(oauthId, memberId);
return oauthInfoRepository.save(entity);
private OAuthMemberEntity saveOauthInfoEntity(final String oauthId, final Long memberId) {
OAuthMemberEntity entity = oauthMemberEntityConverter.toEntity(oauthId, memberId);
return oAuthMemberRepository.save(entity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
import com.blackcompany.eeos.auth.application.domain.client.OauthMemberClientComposite;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class OauthClientService {
private final OauthMemberClientComposite oauthMemberClientComposite;

@Transactional(propagation = Propagation.REQUIRED)
public OauthMemberModel getOauthMember(String oauthServerType, String authCode, String uri) {
OauthMemberModel model = oauthMemberClientComposite.fetch(oauthServerType, authCode, uri);
model.validateNameFormat();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,45 @@
import com.blackcompany.eeos.auth.application.domain.TokenModel;
import com.blackcompany.eeos.auth.application.domain.token.TokenResolver;
import com.blackcompany.eeos.auth.application.exception.InvalidTokenException;
import com.blackcompany.eeos.auth.application.support.AuthenticationTokenGenerator;
import com.blackcompany.eeos.auth.application.usecase.ReissueUsecase;
import com.blackcompany.eeos.auth.persistence.AuthInfoEntity;
import com.blackcompany.eeos.auth.persistence.AuthInfoRepository;
import java.util.Optional;
import com.blackcompany.eeos.auth.persistence.MemberAuthenticationRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Slf4j
public class ReissueService implements ReissueUsecase {
private final CreateTokenService createTokenService;
private final AuthInfoRepository authInfoRepository;
private final AuthenticationTokenGenerator authenticationTokenGenerator;
private final MemberAuthenticationRepository memberAuthenticationRepository;
private final TokenResolver tokenResolver;

@Transactional
@Override
public TokenModel execute(final String token) {
Long memberId = tokenResolver.getUserInfoByCookie(token);
validateToken(memberId, token);

return createTokenService.execute(memberId);
validateToken(token);
saveUsedToken(token, memberId);

return authenticationTokenGenerator.execute(memberId);
}

private void validateToken(final Long memberId, final String token) {
Optional<AuthInfoEntity> validToken =
authInfoRepository.findByMemberIdAndToken(memberId, token);
if (validToken.isPresent()) {
validToken.ifPresent(authInfoRepository::delete);
return;
private void validateToken(final String token) {
boolean isExistToken = memberAuthenticationRepository.isExistToken(token);

if (isExistToken) {
throw new InvalidTokenException();
}
deleteInvalidToken(token);
}

private void deleteInvalidToken(final String token) {
authInfoRepository.findByToken(token).ifPresent(authInfoRepository::delete);
throw new InvalidTokenException();
private void saveUsedToken(final String token, final Long memberId) {
memberAuthenticationRepository.save(token, memberId, getExpiredToken(token));
}

private Long getExpiredToken(final String token) {
return tokenResolver.getExpiredDateByHeader(token);
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,27 @@
package com.blackcompany.eeos.auth.application.service;
package com.blackcompany.eeos.auth.application.support;

import com.blackcompany.eeos.auth.application.domain.TokenModel;
import com.blackcompany.eeos.auth.application.domain.converter.TokenModelConverter;
import com.blackcompany.eeos.auth.application.domain.token.TokenProvider;
import com.blackcompany.eeos.auth.application.domain.token.TokenResolver;
import com.blackcompany.eeos.auth.persistence.AuthInfoEntity;
import com.blackcompany.eeos.auth.persistence.AuthInfoEntityConverter;
import com.blackcompany.eeos.auth.persistence.AuthInfoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Component;

@Service
@Component
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class CreateTokenService {
public class AuthenticationTokenGenerator {
private final TokenProvider tokenProvider;
private final TokenModelConverter tokenModelConverter;
private final TokenResolver tokenResolver;
private final AuthInfoRepository authInfoRepository;
private final AuthInfoEntityConverter authInfoEntityConverter;

@Transactional
public TokenModel execute(final Long memberId) {
String accessToken = tokenProvider.createAccessToken(memberId);
String refreshToken = tokenProvider.createRefreshToken(memberId);

saveToken(memberId, refreshToken);

return tokenModelConverter.from(
accessToken,
tokenResolver.getExpiredDateByHeader(accessToken),
refreshToken,
tokenResolver.getExpiredDateByCookie(refreshToken));
}

private void saveToken(final Long memberId, final String token) {
AuthInfoEntity authInfoEntity = authInfoEntityConverter.from(memberId, token);
authInfoRepository.save(authInfoEntity);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.blackcompany.eeos.auth.persistence;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.TimeToLive;

@RedisHash("MemberAuthentication")
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class MemberAuthenticationEntity {
@Id private String token;
private Long memberId;
@TimeToLive private Long expiration;
}
Loading

0 comments on commit b82b403

Please sign in to comment.