Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #22 from FinFellows/develop
Browse files Browse the repository at this point in the history
feat: CI/CD 배포
  • Loading branch information
sejineer authored Jan 1, 2024
2 parents ba7ca7c + 6c9dd09 commit c5ecbf9
Show file tree
Hide file tree
Showing 49 changed files with 2,071 additions and 7 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Java CI with Gradle

# 동작 조건 설정 : main 브랜치에 push 혹은 pull request가 발생할 경우 동작한다.
on:
push:
branches: [ "main" ]

permissions:
contents: read

jobs:
# Spring Boot 애플리케이션을 빌드하여 도커허브에 푸시하는 과정
build-docker-image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# 1. Java 17 세팅
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

## create application-database.yaml
- name: create application.properties file
run: |
mkdir ./src/main/resources
touch ./src/main/resources/application.yml
echo "${{ secrets.APPLICATION_YML }}" >> src/main/resources/application.yml
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
shell: bash

# 2. Spring Boot 애플리케이션 빌드
- name: Build with Gradle
uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
with:
arguments: clean bootJar

# 3. Docker 이미지 빌드
- name: docker image build
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/finfellow .

# 4. DockerHub 로그인
- name: docker login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

# 5. Docker Hub 이미지 푸시
- name: docker Hub push
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/finfellow

run-docker-image-on-ec2:
needs: build-docker-image
runs-on: self-hosted

steps:
# 1. 최신 이미지를 풀받습니다
- name: docker pull
run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/finfellow

# 2. 기존의 컨테이너를 중지시킵니다
- name: docker stop container
run: sudo docker stop $(sudo docker ps -q) 2>/dev/null || true

# 3. 최신 이미지를 컨테이너화하여 실행시킵니다
- name: docker run new container
run: sudo docker run --name finfellow --rm -d -p 80:8080 ${{ secrets.DOCKERHUB_USERNAME }}/finfellow

# 4. 미사용 이미지를 정리합니다
- name: delete old docker image
run: sudo docker system prune -f
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ out/
### VS Code ###
.vscode/
/src/main/resources/
application.yml
13 changes: 13 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,28 @@ repositories {
}

dependencies {
// implementation 'net.bytebuddy:byte-buddy:1.11.22'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'

compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'

//Swagger, RestDocs
implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.1.0'
testImplementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-api', version: '2.1.0'

//jwt
implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5'

annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.finfellows.domain.auth.application;

import com.finfellows.domain.auth.dto.TokenMapping;
import com.finfellows.global.config.security.OAuth2Config;
import com.finfellows.global.config.security.token.UserPrincipal;
import io.jsonwebtoken.*;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.security.Key;
import java.util.Date;

@Slf4j
@Service
public class CustomTokenProviderService {

@Autowired
private OAuth2Config oAuth2Config;

@Autowired
private final CustomUserDetailsService customUserDetailsService;

public CustomTokenProviderService(CustomUserDetailsService customUserDetailsService) {
this.customUserDetailsService = customUserDetailsService;
}


public TokenMapping refreshToken(Authentication authentication, String refreshToken) {
UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
Date now = new Date();

Date accessTokenExpiresIn = new Date(now.getTime() + oAuth2Config.getAuth().getAccessTokenExpirationMsec());

String secretKey = oAuth2Config.getAuth().getTokenSecret();
byte[] keyBytes = Decoders.BASE64.decode(secretKey);
Key key = Keys.hmacShaKeyFor(keyBytes);

String accessToken = Jwts.builder()
.setSubject(Long.toString(userPrincipal.getId()))
.setIssuedAt(new Date())
.setExpiration(accessTokenExpiresIn)
.signWith(key, SignatureAlgorithm.HS512)
.compact();

return TokenMapping.builder()
.email(userPrincipal.getEmail())
.accessToken(accessToken)
.refreshToken(refreshToken)
.build();
}

public Long getExpiration(String token) {
// accessToken 남은 유효시간
Date expiration = Jwts.parserBuilder().setSigningKey(oAuth2Config.getAuth().getTokenSecret()).build().parseClaimsJws(token).getBody().getExpiration();
// 현재 시간
Long now = new Date().getTime();
//시간 계산
return (expiration.getTime() - now);
}


//토큰 만들기
public TokenMapping createToken(Authentication authentication) {
UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();


Date now = new Date();

Date accessTokenExpiresIn = new Date(now.getTime() + oAuth2Config.getAuth().getAccessTokenExpirationMsec());
Date refreshTokenExpiresIn = new Date(now.getTime() + oAuth2Config.getAuth().getRefreshTokenExpirationMsec());

String secretKey = oAuth2Config.getAuth().getTokenSecret();

byte[] keyBytes = Decoders.BASE64.decode(secretKey);
Key key = Keys.hmacShaKeyFor(keyBytes);

String accessToken = Jwts.builder()
.setSubject(Long.toString(userPrincipal.getId()))
.setIssuedAt(new Date())
.setExpiration(accessTokenExpiresIn)
.signWith(key, SignatureAlgorithm.HS512)
.compact();

String refreshToken = Jwts.builder()
.setExpiration(refreshTokenExpiresIn)
.signWith(key, SignatureAlgorithm.HS512)
.compact();


return TokenMapping.builder()
.email(userPrincipal.getEmail())
.accessToken(accessToken)
.refreshToken(refreshToken)
.build();

}


public boolean validateToken(String token) {
try {
//log.info("bearerToken = {} \n oAuth2Config.getAuth()={}", token, oAuth2Config.getAuth().getTokenSecret());
Jwts.parserBuilder().setSigningKey(oAuth2Config.getAuth().getTokenSecret()).build().parseClaimsJws(token);
return true;
} catch (io.jsonwebtoken.security.SecurityException ex) {
log.error("잘못된 JWT 서명입니다.");
} catch (MalformedJwtException ex) {
log.error("잘못된 JWT 서명입니다.");
} catch (ExpiredJwtException ex) {
log.error("만료된 JWT 토큰입니다.");
} catch (UnsupportedJwtException ex) {
log.error("지원되지 않는 JWT 토큰입니다.");
} catch (IllegalArgumentException ex) {
log.error("JWT 토큰이 잘못되었습니다.");
}
return false;
}



public UsernamePasswordAuthenticationToken getAuthenticationById(String token) {
Long userId = getUserIdFromToken(token);
UserDetails userDetails = customUserDetailsService.loadUserById(userId);
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
return authentication;
}

public UsernamePasswordAuthenticationToken getAuthenticationByEmail(String email) {
UserDetails userDetails = customUserDetailsService.loadUserByUsername(email);
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
return authentication;
}

public Long getUserIdFromToken(String token) {
Claims claims = Jwts.parserBuilder()
.setSigningKey(oAuth2Config.getAuth().getTokenSecret())
.build()
.parseClaimsJws(token)
.getBody();

return Long.parseLong(claims.getSubject());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.finfellows.domain.auth.application;


import com.finfellows.domain.user.domain.User;
import com.finfellows.domain.user.domain.repository.UserRepository;
import com.finfellows.global.config.security.token.UserPrincipal;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
@Slf4j
public class CustomUserDetailsService implements UserDetailsService {
private final UserRepository userRepository;

@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
Optional<User> user = userRepository.findByEmail(email);
if (user.isPresent()) {
return UserPrincipal.createUser(user.get());
}

throw new UsernameNotFoundException("유효하지 않는 유저입니다.");
}

public UserDetails loadUserById(Long id) {
log.debug("Attempting to load user by ID: {}", id);
return userRepository.findById(id)
.map(UserPrincipal::createUser)
.orElseThrow(() -> {
log.error("User with ID: {} could not be found.", id);
return new UsernameNotFoundException("유효하지 않는 유저입니다.");
});
}






}
Loading

0 comments on commit c5ecbf9

Please sign in to comment.