This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from FinFellows/develop
feat: CI/CD 배포
- Loading branch information
Showing
49 changed files
with
2,071 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,4 @@ out/ | |
### VS Code ### | ||
.vscode/ | ||
/src/main/resources/ | ||
application.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
src/main/java/com/finfellows/domain/auth/application/CustomTokenProviderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/finfellows/domain/auth/application/CustomUserDetailsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("유효하지 않는 유저입니다."); | ||
}); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
} |
Oops, something went wrong.