Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: main.yml #37

Merged
merged 20 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ name: CI/CD using github actions & docker
on:
push:
branches: [ "main" ]
pull_request:
branches: "main"

permissions:
contents: read

jobs:
CI-CD:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v2
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
Expand All @@ -28,14 +34,17 @@ jobs:
run: chmod +x ./gradlew

- name: Build with Gradle
run: ./gradlew bootJar
run: ./gradlew build -x test

- name: List build/libs contents
run: ls -la build/libs

- name: Docker build
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -t alimo . # Dockerfile 경로 수정
docker tag alimo ${{ secrets.DOCKER_USERNAME }}/alimo:latest
docker push ${{ secrets.DOCKER_USERNAME }}/alimo:latest
docker build -t ${{ secrets.DOCKER_USERNAME }}/sopo .
docker push ${{ secrets.DOCKER_USERNAME }}/sopo:latest


- name: Deploy with SSH
if: github.ref == 'refs/heads/main'
Expand All @@ -48,23 +57,14 @@ jobs:
port: ${{ secrets.PORT }} # ssh port (22)
envs: GITHUB_SHA
script: |
docker stop sopo_v2 || true
docker rm sopo_v2 || true
docker rmi ${{ secrets.DOCKER_USERNAME }}/sopo_v2 || true
docker pull ${{ secrets.DOCKER_USERNAME }}/sopo_v2
docker run -d -p 8080:8080 --name sopo_v2 ${{ secrets.DOCKER_USERNAME }}/sopo_v2

sudo chkconfig --add redis-server
sudo chkconfig --level 345 redis-server on
sudo service reids-server start

mkdir -p /home/ubuntu/app
cd /home/ubuntu/app

echo "${{ secrets.DOCKER_COMPOSE }}" > docker-compose.yml

# Docker Compose 명령어 실행
docker-compose pull
docker-compose up -d

docker system prune -f
script: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker stop sopo
docker rm sopo
sudo docker pull redis:latest
sudo docker run -d --name redis -p 6379:6379 --network redis-network redis:latest
sudo docker rm -f sopo_redis sopo_app
docker rmi ${{ secrets.DOCKER_USERNAME }}/sopo:latest
docker pull ${{ secrets.DOCKER_USERNAME }}/sopo:latest
sudo docker run -d --name alimo_redis_new redis:latest
docker run -d -p 80:8080 --name sopo ${{ secrets.DOCKER_USERNAME }}/sopo:latest
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM openjdk:17-alpine
COPY build/libs/sopo-2.0.0.jar /sopo.jar
ENV TZ=Asia/Seoul
ENTRYPOINT ["java", "-jar", "/sopo.jar", "-Duser.timezone=Asia/Seoul"]
FROM openjdk:17
ARG JAR_FILE=build/libs/SOPO_server_v2-0.0.2-SNAPSHOT.jar
COPY ${JAR_FILE} sopo.jar
ENTRYPOINT ["java","-jar","/sopo.jar"]
8 changes: 3 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ plugins {
}

group = 'kr.hs.dgsw'
version = '0.0.1-SNAPSHOT'
version = '0.0.2-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

jar {
archiveFileName.set('sopo-2.0.0.jar')
}

configurations {
compileOnly {
extendsFrom annotationProcessor
Expand Down Expand Up @@ -63,3 +59,5 @@ dependencies {
tasks.named('test') {
useJUnitPlatform()
}

jar { enabled = false }
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import kr.hs.dgsw.SOPO_server_v2.domain.auth.dto.req.SignInReq;
import kr.hs.dgsw.SOPO_server_v2.domain.auth.dto.req.SignUpReq;
import kr.hs.dgsw.SOPO_server_v2.domain.auth.dto.res.ReProvideTokenRes;
import kr.hs.dgsw.SOPO_server_v2.domain.auth.dto.res.TokenRes;
import kr.hs.dgsw.SOPO_server_v2.domain.auth.service.AuthEmailService;
import kr.hs.dgsw.SOPO_server_v2.domain.auth.service.AuthService;
import kr.hs.dgsw.SOPO_server_v2.domain.auth.service.AuthTokenService;
Expand All @@ -31,7 +32,7 @@ public Response signUp(
}

@PostMapping("/sign_in")
public ResponseData<JsonWebTokenResponse> signIn(
public ResponseData<TokenRes> signIn(
@RequestBody @Valid SignInReq signInReq
){
return authService.signIn(signInReq);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,24 @@

import kr.hs.dgsw.SOPO_server_v2.domain.auth.dto.req.SignInReq;
import kr.hs.dgsw.SOPO_server_v2.domain.auth.dto.req.SignUpReq;
import kr.hs.dgsw.SOPO_server_v2.domain.auth.dto.res.TokenRes;
import kr.hs.dgsw.SOPO_server_v2.domain.member.entity.MemberEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.member.enums.MemberCategory;
import kr.hs.dgsw.SOPO_server_v2.domain.member.enums.MemberState;
import kr.hs.dgsw.SOPO_server_v2.domain.member.repository.MemberRepository;
import kr.hs.dgsw.SOPO_server_v2.global.common.dto.res.JsonWebTokenResponse;
import kr.hs.dgsw.SOPO_server_v2.global.error.custom.auth.WrongPasswordException;
import kr.hs.dgsw.SOPO_server_v2.global.error.custom.email.CodeIsWrongException;
import kr.hs.dgsw.SOPO_server_v2.global.error.custom.email.EmailAlreadyExistsException;
import kr.hs.dgsw.SOPO_server_v2.global.error.custom.member.MemberNotFoundException;
import kr.hs.dgsw.SOPO_server_v2.global.infra.jwt.JwtProvider;
import kr.hs.dgsw.SOPO_server_v2.global.infra.security.CustomMemberDetails;
import kr.hs.dgsw.SOPO_server_v2.global.response.Response;
import kr.hs.dgsw.SOPO_server_v2.global.response.ResponseData;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;

import static kr.hs.dgsw.SOPO_server_v2.global.response.Response.of;

@Component
@RequiredArgsConstructor
public class AuthService {
Expand Down Expand Up @@ -65,7 +53,7 @@ public Response signUp(SignUpReq signUpReq) {
}

@Transactional(rollbackFor = Exception.class)
public ResponseData<JsonWebTokenResponse> signIn(SignInReq signInReq){
public ResponseData<TokenRes> signIn(SignInReq signInReq){
MemberEntity memberEntity = memberRepository.findByMemberId(signInReq.memberId());

if(memberEntity == null){
Expand All @@ -77,10 +65,7 @@ public ResponseData<JsonWebTokenResponse> signIn(SignInReq signInReq){
throw WrongPasswordException.EXCEPTION;
}

return ResponseData.of(HttpStatus.OK, "로그인 성공", JsonWebTokenResponse.builder()
.accessToken(jwtProvider.generateAccessToken(memberEntity.getMemberId(), memberEntity.getMemberState()))
.refreshToken(jwtProvider.generateRefreshToken(memberEntity.getMemberId(), memberEntity.getMemberState()))
.build());
return ResponseData.of(HttpStatus.OK, "로그인 성공", jwtProvider.generateToken(signInReq.memberId(), memberEntity.getMemberState()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,15 @@
import kr.hs.dgsw.SOPO_server_v2.global.error.exception.StatusEnum;
import kr.hs.dgsw.SOPO_server_v2.global.infra.jwt.JwtExceptionFilter;
import kr.hs.dgsw.SOPO_server_v2.global.infra.jwt.JwtFilter;
import kr.hs.dgsw.SOPO_server_v2.global.infra.security.CustomMemberDetails;
import kr.hs.dgsw.SOPO_server_v2.global.infra.security.MemberDetailsService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
Expand Down Expand Up @@ -55,8 +46,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.requestMatchers("/email/**").permitAll()
.requestMatchers("/re_provide/**").permitAll()
.requestMatchers("/file/**").hasAuthority("ROLE_ACTIVE")
.requestMatchers("/board/**").hasAuthority("ROLE_ACTIVE")
.requestMatchers("/contest/**").hasAuthority("ROLE_ACTIVE")
.requestMatchers("/contest/**").authenticated()
.requestMatchers("/like/**").hasAuthority("ROLE_ACTIVE")
.anyRequest().authenticated()
.and()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package kr.hs.dgsw.SOPO_server_v2.global.infra.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.UnsupportedJwtException;
import kr.hs.dgsw.SOPO_server_v2.domain.member.entity.MemberEntity;
import kr.hs.dgsw.SOPO_server_v2.domain.member.repository.MemberRepository;
import kr.hs.dgsw.SOPO_server_v2.global.error.custom.member.MemberNotFoundException;
Expand All @@ -24,17 +26,24 @@ public class JwtHelper {
@Transactional
public Authentication getAuthentication(String accessToken) {
Claims claims = getClaims(accessToken);
MemberEntity member = memberRepository.findById(Long.valueOf(claims.getSubject()))
.orElseThrow(()-> MemberNotFoundException.EXCEPTION);
MemberEntity member = memberRepository.findByMemberId(claims.getSubject());

CustomMemberDetails details = new CustomMemberDetails(member);

return new UsernamePasswordAuthenticationToken(details, null, details.getAuthorities());
}

public Claims getClaims(String token) {
try{
return Jwts.parserBuilder()
.setSigningKey(jwtProperties.getSecretKey()).build().parseClaimsJws(token).getBody();
} catch (ExpiredJwtException e) {
throw new IllegalArgumentException("만료된 토큰");
} catch (UnsupportedJwtException e) {
throw new IllegalArgumentException("지원되지 않는 토큰");
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("잘못된 토큰");
}
}

public String extractToken(final String token) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
@Component
public class GetCurrentMember {
public MemberEntity current() {
return ((CustomMemberDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).member();
return getMemberDetails().member();
}

private CustomMemberDetails getMemberDetails() {
return ((CustomMemberDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
}
}
14 changes: 14 additions & 0 deletions src/test/java/kr/hs/dgsw/SOPO_server_v2/SecretKeyGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package kr.hs.dgsw.SOPO_server_v2;

import java.security.SecureRandom;
import java.util.Base64;

public class SecretKeyGenerator {
public static void main(String[] args) {
SecureRandom secureRandom = new SecureRandom();
byte[] key = new byte[32]; // 256-bit key
secureRandom.nextBytes(key);
String secretKey = Base64.getEncoder().encodeToString(key);
System.out.println("Generated Secret Key: " + secretKey);
}
}
Loading