Skip to content

Commit

Permalink
Merge pull request #418 from boostcampwm2023/server/refactor/415
Browse files Browse the repository at this point in the history
Auth 모듈 스프링 서버로 이전(~ing)
  • Loading branch information
sk000801 authored May 7, 2024
2 parents d7180df + 22093e3 commit 4cd43cc
Show file tree
Hide file tree
Showing 28 changed files with 885 additions and 67 deletions.
136 changes: 72 additions & 64 deletions .github/workflows/server-ci-spring.yml
Original file line number Diff line number Diff line change
@@ -1,72 +1,80 @@
name: Backend Spring CI
# name: Backend Spring CI

on:
pull_request:
branches: [develop]
paths:
- "server/spring/**"
# on:
# pull_request:
# branches: [develop]
# paths:
# - "server/spring/**"

defaults:
run:
working-directory: ./server/spring
# defaults:
# run:
# working-directory: ./server/spring

jobs:
BACKEND-SPRING-CI:
runs-on: ubuntu-20.04
# jobs:
# BACKEND-SPRING-CI:
# runs-on: ubuntu-20.04

steps:
- name: Checkout
uses: actions/checkout@v4
# steps:
# - name: Checkout
# uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: 17
distribution: "adopt"
# - name: Set up JDK 17
# uses: actions/setup-java@v4
# with:
# java-version: 17
# distribution: "adopt"

- name: Create application.yml
env:
DB_HOST_IP: ${{ secrets.SERVER_ENV_DB_HOST_IP }}
DB_PORT: ${{ secrets.SERVER_ENV_DB_PORT }}
DB_USER_NAME: ${{ secrets.SERVER_ENV_DB_USER_NAME }}
DB_PASSWORD: ${{ secrets.SERVER_ENV_DB_PASSWORD }}
DB_DATABASE_NAME: ${{ secrets.SERVER_ENV_DB_DATABASE_NAME }}
ACCESS_ID: ${{ secrets.SERVER_ENV_ACCESS_ID }}
SECRET_ACCESS_KEY: ${{ secrets.SERVER_ENV_SECRET_ACCESS_KEY }}
JWT_SECRET_KEY: ${{ secrets.SERVER_ENV_JWT_SECRET_KEY }}
GREEN_EYE_SECRET_KEY: ${{secrets.GREEN_EYE_SECRET_KEY}}
GREEN_EYE_REQUEST_URL: ${{secrets.GREEN_EYE_REQUEST_URL}}
CLOUD_FUNCTIONS_EXECUTE_URL: ${{secrets.CLOUD_FUNCTIONS_EXECUTE_URL}}
CLOUD_FUNCTIONS_REQUEST_URL: ${{secrets.CLOUD_FUNCTIONS_REQUEST_URL}}
API_GW_ACCESS_KEY: ${{secrets.API_GW_ACCESS_KEY}}
REFRESH_SECRET_KEY: ${{secrets.REFRESH_SECRET_KEY}}
REDIS_HOST_IP: ${{secrets.REDIS_HOST_IP}}
REDIS_PASSWORD: ${{secrets.REDIS_PASSWORD}}
REDIS_PORT: ${{secrets.REDIS_PORT}}
REDIS_TTL: ${{secrets.REDIS_TTL}}
run: |
touch application.yml
echo "DB_HOST_IP=$DB_HOST_IP" >> application.yml
echo "DB_PORT=$DB_PORT" >> application.yml
echo "DB_USER_NAME=$DB_USER_NAME" >> application.yml
echo "DB_PASSWORD=$DB_PASSWORD" >> application.yml
echo "DB_DATABASE_NAME=$DB_DATABASE_NAME" >> application.yml
echo "ACCESS_ID=$ACCESS_ID" >> application.yml
echo "SECRET_ACCESS_KEY=$SECRET_ACCESS_KEY" >> application.yml
echo "JWT_SECRET_KEY=$JWT_SECRET_KEY" >> application.yml
echo "GREEN_EYE_SECRET_KEY=$GREEN_EYE_SECRET_KEY" >> application.yml
echo "GREEN_EYE_REQUEST_URL=$GREEN_EYE_REQUEST_URL" >> application.yml
echo "CLOUD_FUNCTIONS_EXECUTE_URL=$CLOUD_FUNCTIONS_EXECUTE_URL" >> application.yml
echo "CLOUD_FUNCTIONS_REQUEST_URL=$CLOUD_FUNCTIONS_REQUEST_URL" >> application.yml
echo "API_GW_ACCESS_KEY=$API_GW_ACCESS_KEY" >> application.yml
echo "REFRESH_SECRET_KEY=$REFRESH_SECRET_KEY" >> application.yml
echo "REDIS_HOST_IP=$REDIS_HOST_IP" >> application.yml
echo "REDIS_PASSWORD=$REDIS_PASSWORD" >> application.yml
echo "REDIS_PORT=$REDIS_PORT" >> application.yml
echo "REDIS_TTL=$REDIS_TTL" >> application.yml
# - name: Create application.properties
# env:
# DB_HOST_IP: ${{ secrets.SERVER_ENV_DB_HOST_IP }}
# DB_PORT: ${{ secrets.SERVER_ENV_DB_PORT }}
# DB_USER_NAME: ${{ secrets.SERVER_ENV_DB_USER_NAME }}
# DB_PASSWORD: ${{ secrets.SERVER_ENV_DB_PASSWORD }}
# DB_DATABASE_NAME: ${{ secrets.SERVER_ENV_DB_DATABASE_NAME }}
# ACCESS_ID: ${{ secrets.SERVER_ENV_ACCESS_ID }}
# SECRET_ACCESS_KEY: ${{ secrets.SERVER_ENV_SECRET_ACCESS_KEY }}
# JWT_SECRET_KEY: ${{ secrets.SERVER_ENV_JWT_SECRET_KEY }}
# GREEN_EYE_SECRET_KEY: ${{secrets.GREEN_EYE_SECRET_KEY}}
# GREEN_EYE_REQUEST_URL: ${{secrets.GREEN_EYE_REQUEST_URL}}
# CLOUD_FUNCTIONS_EXECUTE_URL: ${{secrets.CLOUD_FUNCTIONS_EXECUTE_URL}}
# CLOUD_FUNCTIONS_REQUEST_URL: ${{secrets.CLOUD_FUNCTIONS_REQUEST_URL}}
# API_GW_ACCESS_KEY: ${{secrets.API_GW_ACCESS_KEY}}
# REFRESH_SECRET_KEY: ${{secrets.REFRESH_SECRET_KEY}}
# REDIS_HOST_IP: ${{secrets.REDIS_HOST_IP}}
# REDIS_PASSWORD: ${{secrets.REDIS_PASSWORD}}
# REDIS_PORT: ${{secrets.REDIS_PORT}}
# REDIS_TTL: ${{secrets.REDIS_TTL}}
# run: |
# touch application.properties
# echo "DB_HOST_IP=$DB_HOST_IP" >> application.properties
# echo "DB_PORT=$DB_PORT" >> application.properties
# echo "DB_USER_NAME=$DB_USER_NAME" >> application.properties
# echo "DB_PASSWORD=$DB_PASSWORD" >> application.properties
# echo "DB_DATABASE_NAME=$DB_DATABASE_NAME" >> application.properties
# echo "ACCESS_ID=$ACCESS_ID" >> application.properties
# echo "SECRET_ACCESS_KEY=$SECRET_ACCESS_KEY" >> application.properties
# echo "JWT_SECRET_KEY=$JWT_SECRET_KEY" >> application.properties
# echo "GREEN_EYE_SECRET_KEY=$GREEN_EYE_SECRET_KEY" >> application.properties
# echo "GREEN_EYE_REQUEST_URL=$GREEN_EYE_REQUEST_URL" >> application.properties
# echo "CLOUD_FUNCTIONS_EXECUTE_URL=$CLOUD_FUNCTIONS_EXECUTE_URL" >> application.properties
# echo "CLOUD_FUNCTIONS_REQUEST_URL=$CLOUD_FUNCTIONS_REQUEST_URL" >> application.properties
# echo "API_GW_ACCESS_KEY=$API_GW_ACCESS_KEY" >> application.properties
# echo "REFRESH_SECRET_KEY=$REFRESH_SECRET_KEY" >> application.properties
# echo "REDIS_HOST_IP=$REDIS_HOST_IP" >> application.properties
# echo "REDIS_PASSWORD=$REDIS_PASSWORD" >> application.properties
# echo "REDIS_PORT=$REDIS_PORT" >> application.properties
# echo "REDIS_TTL=$REDIS_TTL" >> application.properties

- name: Grant execute permission for gradlew
run: chmod +x gradlew
# - name: Gradle Caching
# uses: actions/cache@v4
# with:
# path: |
# ~/.gradle/caches
# ~/.gradle/wrapper
# key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}

- name: Test with Gradle
run: ./gradlew --info test
# - name: Grant execute permission for gradlew
# run: chmod +x gradlew

# - name: Test with Gradle
# run: ./gradlew --info test
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Mac Os
.DS_Store
.DS_Store

# VSC setting
.vscode/**
4 changes: 4 additions & 0 deletions server/spring/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ build/
!**/src/main/**/build/
!**/src/test/**/build/

### env file ###
application.properties
application-prod.properties

### STS ###
.apt_generated
.classpath
Expand Down
13 changes: 13 additions & 0 deletions server/spring/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'javax.persistence:javax.persistence-api:2.2'

implementation("org.springframework.boot:spring-boot-starter-security")
implementation("io.jsonwebtoken:jjwt-api:0.11.5")
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.11.5")
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.11.5")

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

annotationProcessor 'org.projectlombok:lombok'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
@EnableCaching
public class Application {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package catchytape.spring.auth.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

@Value("${spring.data.redis.host}")
private String host;

@Value("${spring.data.redis.port}")
private int port;

@Value("${spring.data.redis.password}")
private String password;

@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();

config.setHostName(host);
config.setPort(port);
config.setPassword(password);

return new LettuceConnectionFactory(config);
}

@Bean
public RedisTemplate<String, String> redisTemplate() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory());

return redisTemplate;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package catchytape.spring.auth.controller;

import org.springframework.web.bind.annotation.RestController;

import catchytape.spring.auth.controller.dto.UserSignupRequest;
import catchytape.spring.auth.controller.dto.UserAuthResponse;
import catchytape.spring.auth.controller.dto.UserLoginRequest;
import catchytape.spring.auth.controller.dto.UserRefreshRequest;
import catchytape.spring.auth.service.AuthService;
import catchytape.spring.auth.service.RedisService;
import catchytape.spring.common.exception.CatchyException;
import catchytape.spring.recentPlayed.RecentPlayed;
import catchytape.spring.user.User;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/users")
public class AuthController {

private final AuthService authService;

@PostMapping(value="/signup", consumes="application/json;charset=UTF-8")
public ResponseEntity<UserAuthResponse> signup(@RequestBody UserSignupRequest request) throws CatchyException{
log.info("POST /users/signup - body = nickname: " + request.nickname());

return ResponseEntity.ok(authService.signup(request.idToken(), request.nickname()));
}

@PostMapping(value="/login", consumes="application/json;charset=UTF-8")
public ResponseEntity<UserAuthResponse> login(@RequestBody UserLoginRequest request) throws CatchyException {
log.info("POST /users/signup - body = idToken: ");

return ResponseEntity.ok(authService.login(request.idToken()));
}

@PostMapping(value="/refresh", consumes="application/json;charset=UTF-8")
public ResponseEntity<UserAuthResponse> refresh(@RequestBody UserRefreshRequest request) throws CatchyException {
log.info("POST /users/refresh - body = refreshToken: ", request.refreshToken());

return ResponseEntity.ok(this.authService.refreshToken(request.refreshToken()));
}

@GetMapping("/test")
public ResponseEntity<User> test() throws CatchyException {
return ResponseEntity.ok(this.authService.test());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package catchytape.spring.auth.controller.dto;

import lombok.Builder;

@Builder
public record UserAuthResponse(String accessToken, String refreshToken) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package catchytape.spring.auth.controller.dto;

public record UserLoginRequest(String idToken) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package catchytape.spring.auth.controller.dto;

public record UserRefreshRequest(String refreshToken) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package catchytape.spring.auth.controller.dto;

public record UserSignupRequest(String nickname, String idToken) {
}
Loading

0 comments on commit 4cd43cc

Please sign in to comment.