From 738ccb105e0f69076cf5a9cb730ae8891a29c53e Mon Sep 17 00:00:00 2001 From: Cho Sangwook <82208159+Sangwook02@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:43:26 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20postgresql=20=EC=97=B0=EA=B2=B0=20(#93?= =?UTF-8?q?)=20(#171)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: postgresql 연결 (#93) * chore: jpa 설정 변경 (#93) * refactor: 회원가입 요청에 JWT validation을 적용하지 않도록 수정 (#93) * fix: anonymous 사용자의 User 모델의 username 필드가 null이 되지 않도록 수정 (#93) * remove: h2Console 제거 (#93) * refactor: 회원가입 후 access token 재발급 (#93) (#169) * remove: 불필요한 todo 제거 --- build.gradle | 5 ++++- .../common/auth/config/WebSecurityConfig.java | 2 -- .../common/auth/jwt/TokenAuthenticationFilter.java | 4 +++- .../reservation/common/auth/jwt/TokenProvider.java | 2 +- .../reservation/controller/UserApiController.java | 11 +++++++++-- .../com/newfit/reservation/service/UserService.java | 3 ++- src/main/resources/application.properties | 8 ++------ 7 files changed, 21 insertions(+), 14 deletions(-) diff --git a/build.gradle b/build.gradle index 5fbd6024..270fca6a 100644 --- a/build.gradle +++ b/build.gradle @@ -35,11 +35,14 @@ dependencies { // aws implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' + // postgresql + implementation 'org.postgresql:postgresql:42.6.0' + runtimeOnly 'org.postgresql:postgresql' + //redis implementation 'org.springframework.boot:spring-boot-starter-data-redis' compileOnly 'org.projectlombok:lombok' - runtimeOnly 'com.h2database:h2' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/src/main/java/com/newfit/reservation/common/auth/config/WebSecurityConfig.java b/src/main/java/com/newfit/reservation/common/auth/config/WebSecurityConfig.java index 25e10d11..88448198 100644 --- a/src/main/java/com/newfit/reservation/common/auth/config/WebSecurityConfig.java +++ b/src/main/java/com/newfit/reservation/common/auth/config/WebSecurityConfig.java @@ -21,7 +21,6 @@ import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import java.util.stream.Stream; -import static org.springframework.boot.autoconfigure.security.servlet.PathRequest.toH2Console; @EnableWebSecurity @Configuration @@ -47,7 +46,6 @@ public class WebSecurityConfig { @Bean public WebSecurityCustomizer configure() { return (web) -> web.ignoring() - .requestMatchers(toH2Console()) .requestMatchers(AntPathRequestMatcher.antMatcher("/static/**")); } diff --git a/src/main/java/com/newfit/reservation/common/auth/jwt/TokenAuthenticationFilter.java b/src/main/java/com/newfit/reservation/common/auth/jwt/TokenAuthenticationFilter.java index 2ac07842..5a7115b0 100644 --- a/src/main/java/com/newfit/reservation/common/auth/jwt/TokenAuthenticationFilter.java +++ b/src/main/java/com/newfit/reservation/common/auth/jwt/TokenAuthenticationFilter.java @@ -14,7 +14,6 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.filter.OncePerRequestFilter; - import java.io.IOException; import static com.newfit.reservation.exception.ErrorCode.*; @@ -79,6 +78,9 @@ private boolean requiresValidityCheck(HttpServletRequest request) { if (request.getRequestURI().equals("/api/v1/gyms") && request.getMethod().equals(HttpMethod.GET.toString())) { return false; } + if (request.getRequestURI().equals("/api/v1/users") && request.getMethod().equals(HttpMethod.POST.toString())) { + return false; + } return true; } } diff --git a/src/main/java/com/newfit/reservation/common/auth/jwt/TokenProvider.java b/src/main/java/com/newfit/reservation/common/auth/jwt/TokenProvider.java index 2f312a4b..b4cd404b 100644 --- a/src/main/java/com/newfit/reservation/common/auth/jwt/TokenProvider.java +++ b/src/main/java/com/newfit/reservation/common/auth/jwt/TokenProvider.java @@ -122,7 +122,7 @@ public Authentication getAnonymousAuthentication(String token) { Claims claims = getClaims(token); Set authorities = Collections.singleton(new SimpleGrantedAuthority(Role.GUEST.getDescription())); - return new UsernamePasswordAuthenticationToken(new org.springframework.security.core.userdetails.User(claims.getSubject(), "", authorities), token, authorities); + return new UsernamePasswordAuthenticationToken(new org.springframework.security.core.userdetails.User("anonymous", "", authorities), token, authorities); } private List getAuthorityIdList(String token) { diff --git a/src/main/java/com/newfit/reservation/controller/UserApiController.java b/src/main/java/com/newfit/reservation/controller/UserApiController.java index e1ffca61..aec57c91 100644 --- a/src/main/java/com/newfit/reservation/controller/UserApiController.java +++ b/src/main/java/com/newfit/reservation/controller/UserApiController.java @@ -2,10 +2,13 @@ import com.newfit.reservation.common.auth.AuthorityCheckService; +import com.newfit.reservation.common.auth.jwt.TokenProvider; +import com.newfit.reservation.domain.User; import com.newfit.reservation.dto.request.UserSignUpRequest; import com.newfit.reservation.dto.request.UserUpdateRequest; import com.newfit.reservation.dto.response.UserDetailResponse; import com.newfit.reservation.service.UserService; +import jakarta.servlet.http.HttpServletResponse; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; @@ -20,6 +23,7 @@ public class UserApiController { private final UserService userService; private final AuthorityCheckService authorityCheckService; + private final TokenProvider tokenProvider; @PatchMapping public ResponseEntity modify(Authentication authentication, @@ -51,8 +55,11 @@ public ResponseEntity drop(Authentication authentication, @PostMapping public ResponseEntity signUp(@RequestHeader(value = "oauth-history-id") Long oauthHistoryId, - @Valid @RequestBody UserSignUpRequest request) { - userService.signUp(oauthHistoryId, request); + @Valid @RequestBody UserSignUpRequest request, + HttpServletResponse response) { + User user = userService.signUp(oauthHistoryId, request); + String accessToken = tokenProvider.generateAccessToken(user); + response.setHeader("access-token", accessToken); return ResponseEntity .status(CREATED) .build(); diff --git a/src/main/java/com/newfit/reservation/service/UserService.java b/src/main/java/com/newfit/reservation/service/UserService.java index 03ee9f8a..17a2bd8e 100644 --- a/src/main/java/com/newfit/reservation/service/UserService.java +++ b/src/main/java/com/newfit/reservation/service/UserService.java @@ -83,12 +83,13 @@ public User findOneById(Long userId) { .orElseThrow(() -> new CustomException(USER_NOT_FOUND)); } - public void signUp(Long oauthHistoryId, UserSignUpRequest request) { + public User signUp(Long oauthHistoryId, UserSignUpRequest request) { OAuthHistory oAuthHistory = oAuthHistoryRepository .findById(oauthHistoryId) .orElseThrow(() -> new CustomException(OAUTH_HISTORY_NOT_FOUND)); User user = User.userSignUp(request); userRepository.save(user); oAuthHistory.signUp(user); + return user; } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index bad28544..f10b9a64 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,12 +1,8 @@ -# h2 database -spring.datasource.driver-class-name=org.h2.Driver -spring.datasource.url=jdbc:h2:mem:testdb -spring.datasource.username=sa -spring.h2.console.enabled=true # jpa -spring.jpa.defer-datasource-initialization=true +spring.jpa.database=postgresql spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.use_sql_comments=true +spring.jpa.defer-datasource-initialization=true #security spring.profiles.include=security