Skip to content

Commit

Permalink
Merge pull request #138 from studio-recoding/dev
Browse files Browse the repository at this point in the history
[🚀feat] 21차 배포
  • Loading branch information
JeonHaeseung authored Jul 10, 2024
2 parents 80c1b33 + f5ed237 commit 80b3ad1
Show file tree
Hide file tree
Showing 31 changed files with 316 additions and 158 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/dev-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,23 @@ jobs:
host: ${{ secrets.HOST_DEV }}
username: ${{ secrets.USER_DEV }}
key: ${{ secrets.KEY_DEV }}
script: mkdir -p ./dev
script: mkdir -p ./ness

## 소스 코드 복사 붙여넣기
- name: copy source via ssh key
uses: burnett01/[email protected]
with:
switches: -avzr --delete
path: ./config
remote_path: ./dev
remote_path: ./ness
remote_host: ${{ secrets.HOST_DEV }}
remote_user: ${{ secrets.USER_DEV }}
remote_key: ${{ secrets.KEY_DEV }}

## EC2에 배포(CD)
## 아래 두 개는 이미 되어 있는 상태
## docker network create ness_network
## docker network connect ness_network redis-server
- name: executing remote ssh commands using password
uses: appleboy/ssh-action@master
with:
Expand All @@ -100,8 +103,9 @@ jobs:
script: |
sh ./dev/config/scripts/deploy.sh
sudo docker stop $(sudo docker ps -a -q)
sudo docker rm $(sudo docker ps -a -q)
sudo docker stop $(sudo docker ps -a --filter="name=backend-server" -q)
sudo docker rm $(sudo docker ps -a --filter="name=backend-server" -q)
sudo docker rmi $(sudo docker images -q)
docker pull jeonhaeseung/ness-server-dev:dev
docker run -d --name backend-server -p 80:8080 --restart unless-stopped jeonhaeseung/ness-server-dev:dev
docker run -d --name backend-server -p 80:8080 --restart unless-stopped jeonhaeseung/ness-server-dev:dev
docker network connect ness_network backend-server
14 changes: 9 additions & 5 deletions .github/workflows/prod-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,23 @@ jobs:
host: ${{ secrets.HOST_PROD }}
username: ${{ secrets.USER_PROD }}
key: ${{ secrets.KEY_PROD }}
script: mkdir -p ./prod
script: mkdir -p ./ness

## 소스 코드 복사 붙여넣기
- name: copy source via ssh key
uses: burnett01/[email protected]
with:
switches: -avzr --delete
path: ./config
remote_path: ./prod
remote_path: ./ness
remote_host: ${{ secrets.HOST_PROD }}
remote_user: ${{ secrets.USER_PROD }}
remote_key: ${{ secrets.KEY_PROD }}

## EC2에 배포(CD)
## 아래 두 개는 이미 되어 있는 상태
## docker network create ness_network
## docker network connect ness_network redis-server
- name: executing remote ssh commands using password
uses: appleboy/ssh-action@master
with:
Expand All @@ -105,8 +108,9 @@ jobs:
script: |
sh ./prod/config/scripts/deploy.sh
sudo docker stop $(sudo docker ps -a -q)
sudo docker rm $(sudo docker ps -a -q)
sudo docker stop $(sudo docker ps -a --filter="name=backend-server" -q)
sudo docker rm $(sudo docker ps -a --filter="name=backend-server" -q)
sudo docker rmi $(sudo docker images -q)
docker pull jeonhaeseung/ness-server-prod:main
docker run -d --name backend-server -p 80:8080 --restart unless-stopped jeonhaeseung/ness-server-prod:main
docker run -d --name backend-server -p 80:8080 --restart unless-stopped jeonhaeseung/ness-server-prod:main
docker network connect ness_network backend-server
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,4 @@ out/

### Secrets ###
application-dev.yml
application-prod.yml

## etc ###
docker-compose.yml
application-prod.yml
5 changes: 1 addition & 4 deletions config/scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ fi

if ! type docker-compose > /dev/null
then
echo "docker does not exist"
echo "Start installing docker"
sudo dnf install -y libxcrypt-compat
echo "docker-compose does not exist"
echo "Start installing docker-compose"
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
fi
34 changes: 34 additions & 0 deletions src/main/java/Ness/Backend/domain/auth/AuthController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package Ness.Backend.domain.auth;

import Ness.Backend.domain.auth.dto.request.PostRefreshTokenDto;
import Ness.Backend.domain.auth.dto.response.GetJwtTokenDto;
import Ness.Backend.domain.member.entity.Member;
import Ness.Backend.global.auth.AuthUser;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/auth")
public class AuthController {
private final AuthService authService;

@PostMapping("/logout")
@Operation(summary = "로그아웃 요청", description = "로그아웃 요청 API 입니다.")
public ResponseEntity<?> logout(@AuthUser Member member, @RequestBody PostRefreshTokenDto postRefreshTokenDto) {
authService.logout(member, postRefreshTokenDto);
return new ResponseEntity<>(HttpStatusCode.valueOf(200));
}

@PostMapping("/reIssuance")
@Operation(summary = "JWT access 토큰 재발급 요청", description = "JWT access 토큰 재발급 요청 API 입니다.")
public GetJwtTokenDto reIssuance(@AuthUser Member member, @RequestBody PostRefreshTokenDto postRefreshTokenDto) {
return authService.reIssuance(member, postRefreshTokenDto);
}
}
59 changes: 59 additions & 0 deletions src/main/java/Ness/Backend/domain/auth/AuthService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package Ness.Backend.domain.auth;

import Ness.Backend.domain.auth.dto.request.PostRefreshTokenDto;
import Ness.Backend.domain.auth.dto.response.GetJwtTokenDto;
import Ness.Backend.domain.auth.inmemory.RefreshTokenRepository;
import Ness.Backend.domain.auth.inmemory.RefreshTokenService;
import Ness.Backend.domain.auth.jwt.JwtTokenProvider;
import Ness.Backend.domain.member.entity.Member;
import Ness.Backend.global.error.ErrorCode;
import Ness.Backend.global.error.exception.UnauthorizedException;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@RequiredArgsConstructor
public class AuthService {
private final JwtTokenProvider jwtTokenProvider;
private final RefreshTokenRepository refreshTokenRepository;
private final RefreshTokenService refreshTokenService;

@Transactional
public void logout(Member member, PostRefreshTokenDto postRefreshTokenDto) {
/* refreshToken 만료 여부 확인 */
if(refreshTokenRepository.findRefreshTokenByJwtRefreshToken(postRefreshTokenDto.getJwtRefreshToken()).isEmpty()){
throw new UnauthorizedException(ErrorCode.INVALID_REFRESH_TOKEN);
}

refreshTokenService.removeRefreshToken(postRefreshTokenDto.getJwtRefreshToken());
SecurityContextHolder.clearContext();
}

@Transactional
public GetJwtTokenDto reIssuance(Member member, PostRefreshTokenDto postRefreshTokenDto) {
/* refreshToken 유효성 확인 */
String refreshToken = postRefreshTokenDto.getJwtRefreshToken().substring(7);

if (!jwtTokenProvider.validRefreshToken(refreshToken)) {
throw new UnauthorizedException(ErrorCode.INVALID_TOKEN);
}

/* refreshToken 만료 여부 확인 */
if(refreshTokenRepository.findRefreshTokenByJwtRefreshToken(postRefreshTokenDto.getJwtRefreshToken()).isEmpty()){
throw new UnauthorizedException(ErrorCode.INVALID_REFRESH_TOKEN);
}

final GetJwtTokenDto generateToken = GetJwtTokenDto.builder()
.jwtAccessToken("Bearer " + jwtTokenProvider.generateAccessToken(member.getEmail(), new Date()))
.jwtRefreshToken(postRefreshTokenDto.getJwtRefreshToken())
.build();

return generateToken;
}


}
20 changes: 0 additions & 20 deletions src/main/java/Ness/Backend/domain/auth/dto/LoginRequestDto.java

This file was deleted.

20 changes: 0 additions & 20 deletions src/main/java/Ness/Backend/domain/auth/dto/RegisterRequestDto.java

This file was deleted.

31 changes: 0 additions & 31 deletions src/main/java/Ness/Backend/domain/auth/dto/ResourceDto.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package Ness.Backend.domain.auth.dto.request;

import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
public class PostRefreshTokenDto {
@NotNull
private String jwtRefreshToken;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Ness.Backend.domain.auth.dto.response;


import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@ToString
@NoArgsConstructor
public class GetJwtTokenDto {
private String jwtAccessToken;
private String jwtRefreshToken;

@Builder
public GetJwtTokenDto(String jwtAccessToken, String jwtRefreshToken) {
this.jwtAccessToken = jwtAccessToken;
this.jwtRefreshToken = jwtRefreshToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
import Ness.Backend.domain.auth.inmemory.entity.RefreshToken;
import org.springframework.data.repository.CrudRepository;

import java.util.Optional;

public interface RefreshTokenRepository extends CrudRepository<RefreshToken, Long> {
Optional<RefreshToken> findRefreshTokenByJwtRefreshToken(String refreshToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ public void saveRefreshToken(String refreshToken, String authKey) {
.build();
refreshTokenRepository.save(token);
}

@Transactional
public void removeRefreshToken(String refreshToken) {
refreshTokenRepository.findRefreshTokenByJwtRefreshToken(refreshToken)
.ifPresent(token -> refreshTokenRepository.delete(token));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,25 @@

@Getter
@NoArgsConstructor
@RedisHash(value = "refreshToken")
//@RedisHash(value = "refreshToken", timeToLive = 60*60*24*14)
@RedisHash(value = "refreshToken", timeToLive = 60*5)
public class RefreshToken { /* Redis에 저장해서 RefreshToken이 유효한지 검증 */
@Id
@Indexed
private String jwtRefreshToken;

// 맴버 이메일로 설정
private String authKey;

//리프레시 토큰의 생명 주기(14일)
@TimeToLive
private Long ttl;

@Builder
public RefreshToken(String jwtRefreshToken, String authKey) {
this.jwtRefreshToken = jwtRefreshToken;
this.authKey = authKey;
this.ttl = 1000L * 60 * 60 * 24 * 14;
//this.ttl = 1000L * 60 * 60 * 24 * 14;
this.ttl = 1000L * 60 * 5;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse

} catch (TokenExpiredException e){
log.error(e + " EXPIRED_TOKEN");
//request.setAttribute("exception", ErrorCode.EXPIRED_TOKEN.getCode());
setResponse(response, ErrorCode.EXPIRED_TOKEN);
} catch (SignatureVerificationException e){
log.error(e + " INVALID_TOKEN_SIGNATURE");
Expand Down
Loading

0 comments on commit 80b3ad1

Please sign in to comment.