Skip to content

Commit

Permalink
Merge pull request #19 from KUIT-Space/develop
Browse files Browse the repository at this point in the history
배포 확인 PR
  • Loading branch information
drbug2000 authored Jul 17, 2024
2 parents ce6cb6a + c783a56 commit ed4fe73
Show file tree
Hide file tree
Showing 46 changed files with 1,038 additions and 99 deletions.
26 changes: 23 additions & 3 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ name: CI/CD gradle

on:
push:
branches: [ "main" ]
branches:
- "main"
- "develop"
pull_request:
branches: [ "main" ]
branches:
- "main"
- "develop"

env:
AWS_REGION: ap-northeast-2
Expand Down Expand Up @@ -62,6 +66,7 @@ jobs:
name: deploy
runs-on: ubuntu-latest
needs: build
if: github.ref == 'refs/heads/main'
environment: production
steps:
# (1) 기본 체크아웃
Expand All @@ -83,7 +88,22 @@ jobs:
with:
distribution: 'temurin'
java-version: '17'
# (5) Gradle build (Test 제외)
# (4.5) application.yml file 설정
- name: Set yml file
uses: microsoft/variable-substitution@v1
with:
files: ${{ env.RESOURCE_PATH }}
env:
spring.datasource.url: ${{ secrets.RDS_HOST }}
spring.datasource.username: ${{ secrets.RDS_USERNAME }}
spring.datasource.password: ${{ secrets.RDS_PASSWORD }}
secret.jwt-secret-key: ${{secrets.JWT_SECRET_KEY}}
secret.jwt-login-secret-key: ${{secrets.JWT_SECRET_KEY_1}}
secret.jwt-user-secret-key: ${{secrets.JWT_SECRET_KEY_2}}
secret.jwt-expired-in: ${{secrets.JWT_EXPIRED_IN}}
spring.datasource.driver-class-name: com.mysql.cj.jdbc.Driver

# (5) Gradle build (Test 제외)
- name: Build with Gradle
uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee
with:
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ out/

### VS Code ###
.vscode/

### application-local.yml ###
src/main/resource/application-local.yml
/**/application-local.yml
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
// runtimeOnly 'com.mysql:mysql-connector-j'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
Expand Down
34 changes: 0 additions & 34 deletions src/main/java/space/space_spring/JwtProvider.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package space.space_spring.argument_resolver.jwtLogin;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface JwtLoginAuth {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package space.space_spring.argument_resolver.jwtLogin;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

@Component
public class JwtLoginAuthHandlerArgumentResolver implements HandlerMethodArgumentResolver {

@Override
public boolean supportsParameter(MethodParameter parameter) {
// 일단 parameter의 return value type 을 검사하지는 X
return parameter.hasParameterAnnotation(JwtLoginAuth.class);
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
return request.getAttribute("userId"); // jwt를 복호화해서 얻은 userId get
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package space.space_spring.argument_resolver.jwtUserSpace;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface JwtUserSpaceAuth {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package space.space_spring.argument_resolver.jwtUserSpace;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

@Component
public class JwtUserSpaceAuthHandlerArgumentResolver implements HandlerMethodArgumentResolver {

@Override
public boolean supportsParameter(MethodParameter parameter) {
// 일단 parameter의 return value type 을 검사하지는 X
return parameter.hasParameterAnnotation(JwtUserSpaceAuth.class);
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
return request.getAttribute("jwtPayloadDto"); // jwt를 복호화해서 얻은 userId get
}
}
37 changes: 37 additions & 0 deletions src/main/java/space/space_spring/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package space.space_spring.config;

import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
// csrf 설정을 disable
.csrf((csrfConfig) ->
csrfConfig.disable()
)
// h2 console 화면을 사용하기 위해 해당 옵션들 disable
.headers((headerConfig) ->
headerConfig.frameOptions(frameOptionsConfig ->
frameOptionsConfig.disable()
)
);
// 요청별 권한 설정
// .authorizeHttpRequests((authorizeRequests) ->
// authorizeRequests
// .requestMatchers(PathRequest.toH2Console()).permitAll()
// .requestMatchers("/", "/user/signup", "/user/login").permitAll()
//// .anyRequest().authenticated()
// );

return http.build();
}
}
41 changes: 41 additions & 0 deletions src/main/java/space/space_spring/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package space.space_spring.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuthHandlerArgumentResolver;
import space.space_spring.argument_resolver.jwtUserSpace.JwtUserSpaceAuthHandlerArgumentResolver;
import space.space_spring.interceptor.jwtLogin.JwtLoginAuthInterceptor;
import space.space_spring.interceptor.jwtUserSpace.JwtUserSpaceAuthInterceptor;

import java.util.List;

@Configuration
@RequiredArgsConstructor
public class WebConfig implements WebMvcConfigurer {

private final JwtLoginAuthInterceptor jwtLoginAuthInterceptor;
private final JwtUserSpaceAuthInterceptor jwtUserSpaceAuthInterceptor;

private final JwtLoginAuthHandlerArgumentResolver jwtLoginAuthHandlerArgumentResolver;
private final JwtUserSpaceAuthHandlerArgumentResolver jwtUserSpaceAuthHandlerArgumentResolver;

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(jwtLoginAuthInterceptor)
.order(1)
.addPathPatterns("/space/**", "/test/**");

registry.addInterceptor(jwtUserSpaceAuthInterceptor)
.order(2)
.addPathPatterns("/test111/**"); // interceptor 적용되야하는 url enum으로 만들어서 여기에 달면 될듯
}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(jwtLoginAuthHandlerArgumentResolver);
argumentResolvers.add(jwtUserSpaceAuthHandlerArgumentResolver);
}
}
45 changes: 45 additions & 0 deletions src/main/java/space/space_spring/controller/SpaceController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package space.space_spring.controller;

import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
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;
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuth;
import space.space_spring.argument_resolver.jwtUserSpace.JwtUserSpaceAuth;
import space.space_spring.dto.jwt.JwtPayloadDto;
import space.space_spring.dto.space.PostSpaceCreateRequest;
import space.space_spring.dto.space.PostSpaceCreateResponse;

import space.space_spring.dto.space.SpaceCreateDto;
import space.space_spring.exception.SpaceException;
import space.space_spring.response.BaseResponse;
import space.space_spring.service.SpaceService;

import static space.space_spring.response.status.BaseExceptionResponseStatus.INVALID_SPACE_CREATE;
import static space.space_spring.util.BindingResultUtils.getErrorMessage;

@RestController
@RequiredArgsConstructor
@RequestMapping("/space")
public class SpaceController {

private final SpaceService spaceService;

@PostMapping("/create")
public BaseResponse<PostSpaceCreateResponse> createSpace(@JwtLoginAuth Long userId, @Validated @RequestBody PostSpaceCreateRequest postSpaceCreateRequest, BindingResult bindingResult, HttpServletResponse response) {
if (bindingResult.hasErrors()) {
throw new SpaceException(INVALID_SPACE_CREATE, getErrorMessage(bindingResult));
}

SpaceCreateDto spaceCreateDto = spaceService.createSpace(userId, postSpaceCreateRequest);
String jwtUserSpace = spaceCreateDto.getJwtUserSpace();
response.setHeader("Authorization", "Bearer " + jwtUserSpace);

return new BaseResponse<>(new PostSpaceCreateResponse(spaceCreateDto.getSpaceId()));
}

}
18 changes: 18 additions & 0 deletions src/main/java/space/space_spring/controller/TestController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package space.space_spring.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuth;
import space.space_spring.argument_resolver.jwtUserSpace.JwtUserSpaceAuth;
import space.space_spring.dto.jwt.JwtPayloadDto;
import space.space_spring.response.BaseResponse;

@RestController
@Slf4j
public class TestController {

@GetMapping("/test")
public String test(){
return "CI/CD 환경 구축 테스트 중. 이 메세지가 보인다면 성공입니다";
}

@GetMapping("/test/jwt")
public BaseResponse<String> jwtLoginTest(@JwtLoginAuth Long userId) {
log.info("userId = {}", userId);
return new BaseResponse<>("jwt login test 성공");
}

@GetMapping("/test111")
public BaseResponse<String> jwtUserSpaceTest(@JwtUserSpaceAuth JwtPayloadDto jwtPayloadDto) {
log.info("jwtPayloadDto = {}", jwtPayloadDto);
return new BaseResponse<>("jwt user space test 성공");
}
}
23 changes: 13 additions & 10 deletions src/main/java/space/space_spring/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package space.space_spring.controller;

import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
Expand All @@ -8,15 +9,15 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import space.space_spring.dto.PostUserLoginRequest;
import space.space_spring.dto.PostUserLoginResponse;
import space.space_spring.dto.PostUserSignupRequest;
import space.space_spring.dto.PostUserSignupResponse;
import space.space_spring.dto.user.PostUserLoginRequest;
import space.space_spring.dto.user.PostUserLoginResponse;
import space.space_spring.dto.user.PostUserSignupRequest;
import space.space_spring.dto.user.PostUserSignupResponse;
import space.space_spring.exception.UserException;
import space.space_spring.response.BaseResponse;
import space.space_spring.service.UserService;

import static space.space_spring.response.status.BaseExceptionResponseStatus.INVALID_USER_VALUE;
import static space.space_spring.response.status.BaseExceptionResponseStatus.*;
import static space.space_spring.util.BindingResultUtils.getErrorMessage;

@Slf4j
Expand All @@ -32,9 +33,8 @@ public class UserController {
*/
@PostMapping("/signup")
public BaseResponse<PostUserSignupResponse> signup(@Validated @RequestBody PostUserSignupRequest postUserSignupRequest, BindingResult bindingResult) {
log.info("<UserController> Signup request: {}", postUserSignupRequest);
if (bindingResult.hasErrors()) {
throw new UserException(INVALID_USER_VALUE, getErrorMessage(bindingResult));
throw new UserException(INVALID_USER_SIGNUP, getErrorMessage(bindingResult));
}
return new BaseResponse<>(userService.signup(postUserSignupRequest));
}
Expand All @@ -43,11 +43,14 @@ public BaseResponse<PostUserSignupResponse> signup(@Validated @RequestBody PostU
* 로그인
*/
@PostMapping("/login")
public BaseResponse<PostUserLoginResponse> login(@Validated @RequestBody PostUserLoginRequest postUserLoginRequest, BindingResult bindingResult) {
public BaseResponse<PostUserLoginResponse> login(@Validated @RequestBody PostUserLoginRequest postUserLoginRequest, BindingResult bindingResult, HttpServletResponse response) {
if (bindingResult.hasErrors()) {
throw new UserException(INVALID_USER_VALUE, getErrorMessage(bindingResult));
throw new UserException(INVALID_USER_LOGIN, getErrorMessage(bindingResult));
}
return new BaseResponse<>(userService.login(postUserLoginRequest));

String jwtLogin = userService.login(postUserLoginRequest);
response.setHeader("Authorization", "Bearer " + jwtLogin);
return new BaseResponse<>(new PostUserLoginResponse("로그인 성공"));
}


Expand Down
Loading

0 comments on commit ed4fe73

Please sign in to comment.