Skip to content

Commit

Permalink
chore: postgresql 연결 (#93) (#171)
Browse files Browse the repository at this point in the history
* 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 제거
  • Loading branch information
Sangwook02 authored Oct 10, 2023
1 parent df05255 commit 738ccb1
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 14 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -47,7 +46,6 @@ public class WebSecurityConfig {
@Bean
public WebSecurityCustomizer configure() {
return (web) -> web.ignoring()
.requestMatchers(toH2Console())
.requestMatchers(AntPathRequestMatcher.antMatcher("/static/**"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public Authentication getAnonymousAuthentication(String token) {
Claims claims = getClaims(token);
Set<SimpleGrantedAuthority> 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<Integer> getAuthorityIdList(String token) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +23,7 @@ public class UserApiController {

private final UserService userService;
private final AuthorityCheckService authorityCheckService;
private final TokenProvider tokenProvider;

@PatchMapping
public ResponseEntity<Void> modify(Authentication authentication,
Expand Down Expand Up @@ -51,8 +55,11 @@ public ResponseEntity<Void> drop(Authentication authentication,

@PostMapping
public ResponseEntity<Void> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
8 changes: 2 additions & 6 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 738ccb1

Please sign in to comment.