diff --git "a/\352\271\200\354\210\230\353\271\210/EC2 \354\235\270\354\212\244\355\204\264\354\212\244 \353\217\204\353\251\224\354\235\270.md" "b/\352\271\200\354\210\230\353\271\210/EC2 \354\235\270\354\212\244\355\204\264\354\212\244 \353\217\204\353\251\224\354\235\270.md" new file mode 100644 index 00000000..9d92ea69 --- /dev/null +++ "b/\352\271\200\354\210\230\353\271\210/EC2 \354\235\270\354\212\244\355\204\264\354\212\244 \353\217\204\353\251\224\354\235\270.md" @@ -0,0 +1 @@ +http://ec2-43-201-121-159.ap-northeast-2.compute.amazonaws.com:8080/ \ No newline at end of file diff --git "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/.gitignore" "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/.gitignore" index 00ee314e..f3a28fab 100644 --- "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/.gitignore" +++ "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/.gitignore" @@ -1,3 +1,4 @@ .gradle .idea /src/main/java/org/example/ +application-oauth.properties diff --git "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/build.gradle" "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/build.gradle" new file mode 100644 index 00000000..f173f485 --- /dev/null +++ "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/build.gradle" @@ -0,0 +1,60 @@ + + +plugins { + id 'org.springframework.boot' version '2.6.7' + id 'io.spring.dependency-management' version '1.0.14.RELEASE' + id 'java' +} + +group 'org.example' +version '1.0-SNAPSHOT' + +group = 'com.spring-study' +version = '0.0.1-SNAPSHOT' +// Java 소스를 컴파일할 때 사용할 Java 버전 +sourceCompatibility = '14' + +repositories { + mavenCentral() +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-web' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testImplementation 'org.projectlombok:lombok:1.18.22' + compileOnly 'org.projectlombok:lombok' + annotationProcessor 'org.projectlombok:lombok' + + //3장 + //implementation 'org.springframework.boot:spring-boot-starter-web' + //implementation 'org.projectlombok:lombok' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + //implementation 'com.h2database:h2' 버전 수정하기 + implementation 'com.h2database:h2:1.4.197' + //testImplementation 'org.springframework.boot:spring-boot-starter-test' + + //4장 + implementation('org.springframework.boot:spring-boot-starter-mustache') + + //5장 + // spring security + implementation('org.springframework.boot:spring-boot-starter-oauth2-client') + testImplementation('org.springframework.security:spring-security-test') + + // session + implementation('org.springframework.session:spring-session-jdbc') + + //8장(MaridDB 드라이버 등록) + implementation('org.mariadb.jdbc:mariadb-java-client') +} + +test { + useJUnitPlatform() +} + +//8장 +//-plain.jar를 생성하지 않기 위해 +jar { + enabled = false +} + diff --git "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/settings.gradle" "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/settings.gradle" new file mode 100644 index 00000000..613f88b7 --- /dev/null +++ "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/settings.gradle" @@ -0,0 +1,2 @@ +rootProject.name = 'freelec-springboot2-websevice' + diff --git "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/java/com/book/springboot/config/auth/SecurityConfig.java" "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/java/com/book/springboot/config/auth/SecurityConfig.java" index aade0748..6039c392 100644 --- "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/java/com/book/springboot/config/auth/SecurityConfig.java" +++ "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/java/com/book/springboot/config/auth/SecurityConfig.java" @@ -20,15 +20,19 @@ protected void configure(HttpSecurity http) throws Exception { .headers().frameOptions().disable() .and() .authorizeRequests() - .antMatchers("/", "/css/**", "/images/**", "/js/**", "/h2-console/**").permitAll() + .antMatchers("/", "/css/**", "/images/**", "/js/**", "/h2-console/**", "/profile").permitAll() + //.antMatchers("/", "/css/**", "/images/**", "/js/**", "/h2-console/**").permitAll() .antMatchers("/api/v1/**").hasRole(Role.USER.name()) .anyRequest().authenticated() .and() - .logout().logoutSuccessUrl("/") + .logout() + .logoutSuccessUrl("/") + .and() .oauth2Login() - .userInfoEndpoint() - .userService(customOAuth2UserService); + .userInfoEndpoint() + .userService(customOAuth2UserService); + } } diff --git "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/java/com/book/springboot/web/PostsApiController.java" "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/java/com/book/springboot/web/PostsApiController.java" index 5cfd9934..88905bd5 100644 --- "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/java/com/book/springboot/web/PostsApiController.java" +++ "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/java/com/book/springboot/web/PostsApiController.java" @@ -1,46 +1,45 @@ package com.book.springboot.web; -import com.book.springboot.web.dto.PostsSaveRequestDto; import com.book.springboot.service.posts.PostsService; +import com.book.springboot.web.dto.PostsListResponseDto; import com.book.springboot.web.dto.PostsResponseDto; -import com.book.springboot.web.dto.PostsUpdateRequestDto; // - -import com.book.springboot.web.dto.PostsListResponseDto; // -import java.util.List; // - +import com.book.springboot.web.dto.PostsSaveRequestDto; +import com.book.springboot.web.dto.PostsUpdateRequestDto; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RequiredArgsConstructor +@RequestMapping("/api/v1/posts") @RestController public class PostsApiController { private final PostsService postsService; - @PostMapping("/api/v1/posts") - public Long save(@RequestBody PostsSaveRequestDto requestDto) { + @PostMapping + public Long save(@RequestBody PostsSaveRequestDto requestDto){ return postsService.save(requestDto); } - @PostMapping("/api/v1/posts/{id}") - public Long update(@PathVariable Long id, @RequestBody PostsUpdateRequestDto requestDto) { + @PutMapping("{id}") + public Long update(@PathVariable Long id, @RequestBody PostsUpdateRequestDto requestDto){ return postsService.update(id, requestDto); } - @GetMapping("/api/v1/posts/{id}") - public PostsResponseDto findById (@PathVariable Long id) { + @GetMapping("{id}") + public PostsResponseDto findById(@PathVariable Long id){ return postsService.findById(id); } - @DeleteMapping("/api/v1/posts/{id}") - public Long delete(@PathVariable Long id) { + @DeleteMapping("{id}") + public Long delete(@PathVariable Long id){ postsService.delete(id); return id; } - //목록 얻어오기 - @GetMapping("/api/v1/posts/") + + @GetMapping public List getPostsList(){ return postsService.findAllDesc(); } - -} +} \ No newline at end of file diff --git "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/application-real.properties" "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/application-real.properties" new file mode 100644 index 00000000..96e781b5 --- /dev/null +++ "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/application-real.properties" @@ -0,0 +1,3 @@ +spring.profiles.include=oauth,real-db +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect +spring.session.store-type=jdbc \ No newline at end of file diff --git "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/application.properties" "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/application.properties" index 704b2f78..9561a41a 100644 --- "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/application.properties" +++ "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/application.properties" @@ -2,4 +2,8 @@ spring.jpa.show_sql = true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect -spring.h2.console.enabled=true \ No newline at end of file +spring.h2.console.enabled=true + +spring.profiles.include=oauth + +spring.session.store-type=jdbc \ No newline at end of file diff --git "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/static/js/app/index.js" "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/static/js/app/index.js" index 9a050a1d..28d322f8 100644 --- "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/static/js/app/index.js" +++ "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/static/js/app/index.js" @@ -9,6 +9,9 @@ var main = { _this.update(); }); + $('#btn-delete').on('click', function () { + _this.delete(); + }); }, save : function () { var data = { @@ -50,6 +53,21 @@ var main = { }).fail(function (error) { alert(JSON.stringify(error)); }); + }, + delete : function () { + var id = $('#id').val(); + + $.ajax({ + type: 'DELETE', + url: '/api/v1/posts/'+id, + dataType: 'json', + contentType:'application/json; charset=utf-8' + }).done(function() { + alert('글이 삭제되었습니다.'); + window.location.href = '/'; + }).fail(function (error) { + alert(JSON.stringify(error)); + }); } }; diff --git "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/index.mustache" "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/index.mustache" index 66c49d81..a8dca2d4 100644 --- "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/index.mustache" +++ "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/index.mustache" @@ -42,7 +42,7 @@ {{/userName}} {{^userName}} Google Login - Naver Login + {{/userName}} diff --git "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/layout/footer.mustache" "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/layout/footer.mustache" index 168de242..c265b12e 100644 --- "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/layout/footer.mustache" +++ "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/layout/footer.mustache" @@ -1,6 +1,5 @@ - diff --git "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/layout/header.mustache" "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/layout/header.mustache" index 742a914b..e848e1f1 100644 --- "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/layout/header.mustache" +++ "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/layout/header.mustache" @@ -3,7 +3,6 @@ 스프링부트 웹서비스 - \ No newline at end of file diff --git "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/posts-save.mustache" "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/posts-save.mustache" index 06d1f88c..8eb0bb51 100644 --- "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/posts-save.mustache" +++ "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/main/resources/templates/posts-save.mustache" @@ -1,29 +1,25 @@ {{>layout/header}} -

게시글 수정

+

게시글 등록

-
- - -
- +
- +
- +
취소 - +
diff --git "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/test/java/com/book/springboot/web/PostsApiControllerTest.java" "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/test/java/com/book/springboot/web/PostsApiControllerTest.java" index b4dfdbe1..1014fedd 100644 --- "a/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/test/java/com/book/springboot/web/PostsApiControllerTest.java" +++ "b/\352\271\200\354\210\230\353\271\210/freelec-springboot2-websevice/src/test/java/com/book/springboot/web/PostsApiControllerTest.java" @@ -1,47 +1,35 @@ package com.book.springboot.web; +import com.fasterxml.jackson.databind.ObjectMapper; import com.book.springboot.domain.posts.Posts; -import com.book.springboot.web.dto.PostsUpdateRequestDto; - import com.book.springboot.domain.posts.PostsRepository; import com.book.springboot.web.dto.PostsSaveRequestDto; +import com.book.springboot.web.dto.PostsUpdateRequestDto; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; - 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.boot.test.web.server.LocalServerPort; import org.springframework.boot.web.server.LocalServerPort; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; - -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpMethod; - -//import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.context.junit.jupiter.SpringExtension; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; - -//5장 import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import org.junit.jupiter.api.BeforeEach; -import com.fasterxml.jackson.databind.ObjectMapper; @ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) // 호스트가 사용하지 않는 랜덤포트를 사용 public class PostsApiControllerTest { @LocalServerPort @@ -53,11 +41,6 @@ public class PostsApiControllerTest { @Autowired private PostsRepository postsRepository; - @AfterEach - public void tearDown() throws Exception { - postsRepository.deleteAll(); - } - @Autowired private WebApplicationContext context; @@ -71,9 +54,14 @@ public void setup(){ .build(); } + @AfterEach + public void tearDown() throws Exception{ + postsRepository.deleteAll(); + } + @Test - @WithMockUser(roles="USER") - public void Posts_등록된다() throws Exception { + @WithMockUser(roles="USER") // 인증된 모의 사용자를 만들어서 사용 + public void Posts_등록된다() throws Exception{ //given String title = "title"; String content = "content"; @@ -82,28 +70,24 @@ public void setup(){ .content(content) .author("author") .build(); + String url = "http://localhost:" + port + "/api/v1/posts"; //when - //ResponseEntity responseEntity = restTemplate.postForEntity(url, requestDto, Long.class); mvc.perform(post(url) .contentType(MediaType.APPLICATION_JSON_UTF8) .content(new ObjectMapper().writeValueAsString(requestDto))) .andExpect(status().isOk()); //then - //assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); - //assertThat(responseEntity.getBody()).isGreaterThan(0L); - List all = postsRepository.findAll(); assertThat(all.get(0).getTitle()).isEqualTo(title); assertThat(all.get(0).getContent()).isEqualTo(content); - } @Test @WithMockUser(roles="USER") - public void Posts_수정된다() throws Exception { + public void Posts_수정된다() throws Exception{ //given Posts savedPosts = postsRepository.save(Posts.builder() .title("title") @@ -122,23 +106,16 @@ public void setup(){ String url = "http://localhost:" + port + "/api/v1/posts/" + updateId; - HttpEntity requestEntity = new HttpEntity<>(requestDto); - //when - //ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, Long.class); mvc.perform(put(url) .contentType(MediaType.APPLICATION_JSON_UTF8) .content(new ObjectMapper().writeValueAsString(requestDto))) - .andExpect(status().isOk()); + .andExpect(status().isOk()); //then - //assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); - //assertThat(responseEntity.getBody()).isGreaterThan(0L); List all = postsRepository.findAll(); assertThat(all.get(0).getTitle()).isEqualTo(expectedTitle); assertThat(all.get(0).getContent()).isEqualTo(expectedContent); } - - -} +} \ No newline at end of file