Skip to content

Commit

Permalink
feat: 토큰 저장
Browse files Browse the repository at this point in the history
  • Loading branch information
YoujeongChoi committed Feb 4, 2024
1 parent ced3b3f commit d3c48f7
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.woowaSisters.woowaSisters.domain.subBook.SubBookRepository;
import com.woowaSisters.woowaSisters.domain.test.Test;
import com.woowaSisters.woowaSisters.domain.test.TestRepository;
import com.woowaSisters.woowaSisters.domain.token.JwtToken;
import com.woowaSisters.woowaSisters.domain.token.JwtTokenRepository;
import com.woowaSisters.woowaSisters.domain.user.UserRepository;
import com.woowaSisters.woowaSisters.domain.park.Parks;
import com.woowaSisters.woowaSisters.domain.user.User;
Expand All @@ -22,8 +24,8 @@
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EntityScan(basePackageClasses = {Parks.class, User.class, Meeting.class, Bookmark.class, Community.class, Test.class, Comment.class, Meeting.class, SubBook.class})
@EnableJpaRepositories (basePackageClasses = {UserRepository.class, ParkRepository.class, MeetingRepository.class, BookmarkRepository.class, CommunityRepository.class, TestRepository.class, CommentRepository.class, MeetingRepository.class, SubBookRepository.class})
@EntityScan(basePackageClasses = {Parks.class, User.class, Meeting.class, Bookmark.class, Community.class, Test.class, Comment.class, Meeting.class, SubBook.class, JwtToken.class})
@EnableJpaRepositories (basePackageClasses = {UserRepository.class, ParkRepository.class, MeetingRepository.class, BookmarkRepository.class, CommunityRepository.class, TestRepository.class, CommentRepository.class, MeetingRepository.class, SubBookRepository.class, JwtTokenRepository.class})

public class WoowaSistersApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"com.woowaSisters.woowaSisters.domain.test",
"com.woowaSisters.woowaSisters.domain.meeting",
"com.woowaSisters.woowaSisters.domain.comment",
"com.woowaSisters.woowaSisters.domain.subBook"
"com.woowaSisters.woowaSisters.domain.subBook",
"com.woowaSisters.woowaSisters.domain.token",

})
public class JpaConfig {
@Bean
Expand Down Expand Up @@ -55,7 +57,8 @@ public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource da
"com.woowaSisters.woowaSisters.domain.likes",
"com.woowaSisters.woowaSisters.domain.test",
"com.woowaSisters.woowaSisters.domain.comment",
"com.woowaSisters.woowaSisters.domain.subBook"
"com.woowaSisters.woowaSisters.domain.subBook",
"com.woowaSisters.woowaSisters.domain.token"
);

JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class TestController {
public TestController(TestService testService) {
this.testService = testService;
}


// 회원가입
@PostMapping("/user")
public ResponseEntity<Test> join(@Valid @RequestBody TestRequestDto joinRequest, BindingResult bindingResult) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.woowaSisters.woowaSisters.controller.token;

import com.woowaSisters.woowaSisters.domain.token.JwtToken;
import com.woowaSisters.woowaSisters.service.token.JwtTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@RestController
@RequestMapping("/v1/token")
public class JwtTokenController {

private final JwtTokenService jwtTokenService;

@Autowired
public JwtTokenController(JwtTokenService jwtTokenService) {
this.jwtTokenService = jwtTokenService;
}

@PostMapping("/save")
public ResponseEntity<JwtToken> saveToken(@RequestBody String tokenValue) {
JwtToken jwtToken = jwtTokenService.saveToken(tokenValue);
return ResponseEntity.ok(jwtToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.woowaSisters.woowaSisters.domain.token;

import lombok.*;
import javax.persistence.*;
import java.util.UUID;

@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "jwt_token")
public class JwtToken {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "token_uuid", columnDefinition = "BINARY(16)")
private UUID tokenUuid;

@Column(name = "token_value", nullable = false, unique = true, columnDefinition = "TEXT")
private String tokenValue;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.woowaSisters.woowaSisters.domain.token;


import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.UUID;

@Repository
public interface JwtTokenRepository extends JpaRepository<JwtToken, UUID> {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.woowaSisters.woowaSisters.service.token;


import com.woowaSisters.woowaSisters.domain.token.JwtToken;
import com.woowaSisters.woowaSisters.domain.token.JwtTokenRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class JwtTokenService {

private final JwtTokenRepository jwtTokenRepository;

@Autowired
public JwtTokenService(JwtTokenRepository jwtTokenRepository) {
this.jwtTokenRepository = jwtTokenRepository;
}

public JwtToken saveToken(String tokenValue) {
JwtToken jwtToken = JwtToken.builder()
.tokenValue(tokenValue)
.build();
return jwtTokenRepository.save(jwtToken);
}
}
39 changes: 39 additions & 0 deletions src/test/java/com/woowaSisters/woowaSisters/OAuthServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.woowaSisters.woowaSisters;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class OAuthServiceTest {

@Autowired
private TestRestTemplate restTemplate;

@Test
public void givenUserLogin_whenRequestGoogleLogin_thenReceiveAuthToken() {
// 로그인 요청 시뮬레이션
ResponseEntity<String> response = restTemplate.getForEntity("/google/login", String.class);

// 응답 상태 코드 확인
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);

// JWT 토큰이 포함되어 있는지 확인
// 실제 테스트에서는 응답 바디나 헤더에서 JWT 토큰을 찾아야 합니다.
// 이 예시에서는 응답 헤더에 "Authorization" 키로 토큰이 포함되어 있다고 가정합니다.
// 실제로는 이 방식이 아닌, OAuth 프로세스에 따라 토큰이 반환되는 방식을 확인해야 합니다.
// String token = response.getHeaders().getFirst("Authorization");
// assertThat(token).isNotNull();

// 아래는 응답 바디가 JSON 형태로 토큰을 포함하는 경우의 예시입니다.
// 실제로는 응답 형태에 맞춰서 JSON 파싱 로직을 추가해야 합니다.
// 예: JSON 객체에서 "access_token" 필드 값을 추출
// String body = response.getBody();
// assertThat(body).contains("access_token");
}
}

0 comments on commit d3c48f7

Please sign in to comment.