๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์ „์ฒด ๊ธ€199

[Spring Boot/RESTful] Spring Security๋ฅผ ์ด์šฉํ•œ ๋กœ๊ทธ์ธ [๊ธฐ๋ณธ] dependency ์ถ”๊ฐ€ org.springframework.boot spring-boot-starter-security 1. ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” username, password๋ฅผ ์ด์šฉ 1) ๊ธฐ๋ณธ password ํ™•์ธ dependency๋ฅผ ์„ค์ •ํ•œ ํ›„, ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜์—ฌ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค. (์›น์„œ๋ฒ„๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”) 2) ์›น ๋ธŒ๋ผ์šฐ์ € ์š”์ฒญ ์ธ์ฆ์ด ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด 401 Unauthorized๊ฐ€ ๋œฌ๋‹ค. ์—ฌ๊ธฐ์—์„œ username ๊ฐ’์„ user๋กœ ํ•ด๋„ ๋ถˆ๋Ÿฌ์™€์ง€๋Š” ์ด์œ ๋Š” spring security์˜ ๊ธฐ๋ณธ ์„ค์ • ๋•Œ๋ฌธ์ด๋‹ค. Spring Security ๊ธฐ๋ณธ ์„ค์ • spring.security.user.name / spring.security.user.password๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ.. 2022. 2. 18.
[Spring Boot/RESTful] HAL Browser(Explorer) (+๋ฒ„์ „ ์—๋Ÿฌ) HAL Browser Hypertext Application Language REST API ์„ค๊ณ„ ์‹œ Response message์˜ ํฌ๋งท๊ณผ๋Š” ์ƒ๊ด€์—†์ด API๋ฅผ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”ํƒ€์ •๋ณด๋ฅผ ํ•˜์ดํผ๋งํฌ ํ˜•์‹์œผ๋กœ ์ œ๊ณตํ•œ๋‹ค. API ๋ฆฌ์†Œ์Šค ์‚ฌ์ด์—์„œ ์ผ๊ด€์ ์ธ ํ•˜์ดํผ๋งํฌ๋ฅผ ์ œ๊ณต API ์„ค๊ณ„์—์„œ HAL์„ ๋„์ž…ํ•˜๊ฒŒ ๋˜๋ฉด API ๊ฐ„ ์‰ฌ์šด ๊ฒ€์ƒ‰์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค. → ๋” ๋‚˜์€ ๊ฐœ๋ฐœํ™˜๊ฒฝ ์ œ๊ณต dependency ์ถ”๊ฐ€ org.springframework.data spring-data-rest-hal-browser HAL Browser๊ฐ€ deprecated ๋˜์–ด์„œ ์ตœ์‹  spring boot ๋ฒ„์ „์—์„œ๋Š” HAL explorer๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. (์•„๋ž˜์—์„œ ๋ฒ„์ „ ์ถ”๊ฐ€ ์•ˆ ํ•˜๋ฉด ์—๋Ÿฌ๋‚จ) org.springframework.dat.. 2022. 2. 18.
[Spring Boot/RESTful] REST API Monitoring์„ ์œ„ํ•œ Actuator(+spring fox PathPatternParser ์ด์Šˆ) Actuator ํ˜„์žฌ ๊ธฐ๋™ํ•˜๊ณ  ์žˆ๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ƒํƒœ๋ฅผ ์‰ฝ๊ฒŒ monitoringํ•  ์ˆ˜ ์žˆ๋‹ค. ํ˜„์žฌ ์„œ๋ฒ„๊ฐ€ ๊ตฌ๋™ ์ค‘์ธ์ง€๋„ ํ™•์ธ ๊ฐ€๋Šฅ ๊ธฐ๋ณธ ์ œ๊ณตํ•˜๋Š” ๋งํฌ๋ฅผ ๋ณ„๋„๋กœ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋” ์ข‹๋‹ค. Actuator ๊ตฌํ˜„ dependency ์ถ”๊ฐ€ org.springframework.boot spring-boot-starter-actuator ์—๋Ÿฌ Actuator๋Š” PathPatternParser๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ง€๋‚œ Swagger ์„ค์ •์—์„œ ํ—ˆ์šฉํ•ด์คฌ๋˜ yml ์„ค์ •์œผ๋กœ๋Š” ํ•ด๊ฒฐ๋˜์ง€ ์•Š๋Š”๋‹ค. org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; https://git.. 2022. 2. 18.
[Spring Boot/RESTful] Swagger ์„ค์ •(+๋ฒ„์ „ ์˜ค๋ฅ˜) Swagger API ์„ค๊ณ„, ๋นŒ๋“œ, ๋ฌธ์„œํ™”, ์‚ฌ์šฉ์— ๋„์›€์„ ์ฃผ๋Š” ์˜คํ”ˆ์†Œ์Šค ํ”„๋ ˆ์ž„์›Œํฌ Swagger ๊ตฌํ˜„ 1. dependency ์„ค์ • io.springfox springfox-boot-starter 3.0.0 io.springfox springfox-swagger-ui 3.0.0 ๋ฒ„์ „ ์˜ค๋ฅ˜ spring boot 2.6 ๋ฒ„์ „ ์ดํ›„์— spring.mvc.pathmatch.matching-strategy ๊ฐ’์ด ant_path_matcher์—์„œ path_pattern_parser๋กœ ๋ณ€๊ฒฝ๋˜๋ฉด์„œ ๋ช‡๋ช‡ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(swaggerํฌํ•จ)์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPo.. 2022. 2. 18.
[Spring Boot/RESTful] REST API 3๋‹จ๊ณ„๋ฅผ ์œ„ํ•œ HATEOAS ์„ค์ • HATEOAS Hypermedia As the Engine Of Application State ํ˜„์žฌ ๋ฆฌ์†Œ์Šค์™€ ์—ฐ๊ด€๋œ(ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•œ) ์ž์› ์ƒํƒœ ์ •๋ณด๋ฅผ ์ œ๊ณต(mapping) ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ช…์‹œ์ ์œผ๋กœ ๋งํฌ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ์„œ๋ฒ„ ์ธก์—์„œ ๋ฐ›์€ ๋งํฌ์˜ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ํ†ตํ•ด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ƒํƒœ ๋ฐ ์ „์ด๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์žฅ์  - ์š”์ฒญ URI๊ฐ€ ๋ณ€๊ฒฝ๋˜๋”๋ผ๋„ ํด๋ผ์ด์–ธํŠธ์—์„œ ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋œ URI๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ, ํด๋ผ์ด์–ธํŠธ๊ฐ€ URI ์ˆ˜์ •์— ๋”ฐ๋ฅธ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ํŽธ๋ฆฌํ•จ์„ ์ œ๊ณตํ•œ๋‹ค. - URI ์ •๋ณด๋ฅผ ํ†ตํ•ด ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ์„ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. - Resource๊ฐ€ ํฌํ•จ๋œ URI๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ๋•Œ๋ฌธ์—, Resource์— ๋Œ€ํ•œ ํ™•์‹ ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. REST API ์„ค๊ณ„ ๋‹จ๊ณ„ 0. The Swamp of POX ํŠน์ •ํ•œ .. 2022. 2. 18.
[Spring boot/RESTful] REST API ๋ฒ„์ „ ๊ด€๋ฆฌ ๋ฒ„์ „ ๊ด€๋ฆฌ ๋‹จ์ˆœํ•˜๊ฒŒ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ฃผ๋Š” ํ•ญ๋ชฉ์„ ์ œ์–ดํ•˜๋Š” ์šฉ๋„๊ฐ€ ์•„๋‹ˆ๋ผ REST API์˜ ์„ค๊ณ„๊ฐ€ ๋ณ€๊ฒฝ๋˜๊ฑฐ๋‚˜ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ตฌ์กฐ๊ฐ€ ๋ฐ”๋€” ๋•Œ๋„ ๋ฒ„์ „์„ ๋ณ€๊ฒฝํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์‚ฌ์šฉ์ž์—๊ฒŒ๋Š” ์–ด๋– ํ•œ API ๋ฒ„์ „์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€ ๋ช…์‹œํ•ด์•ผ ํ•œ๋‹ค. ์ข…๋ฅ˜ 1) URI Versioning : Twitter 2) Request Parameter Versioning : Amazon 3) (Custom) Headers Versioning(MIME) : Microsoft 4) Media type Versioning(content negotiation, accept header) : Github ์ผ๋ฐ˜ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‹คํ–‰ ๊ฐ€๋Šฅ : URI, Request Parameter ์ผ๋ฐ˜ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‹คํ–‰ ๋ถˆ๊ฐ€ : (Custom) Headers Ve.. 2022. 2. 18.
[Spring boot/RESTful] Response Filtering Response ๋ฐ์ดํ„ฐ ์ œ์–ด๋ฅผ ์œ„ํ•œ Filtering ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌ๋˜๋Š” ์ •๋ณด์˜ ๊ฐ’์„ ์ œ์–ด ๋ณดํ†ต 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ์˜ค๋Š˜์€ ์ด ์ค‘ 3๋ฒˆ์งธ์— ๋Œ€ํ•ด ๋‹ค๋ฃฌ๋‹ค. ๊ฐ’์„ ์•”ํ˜ธํ™”ํ•˜๊ฑฐ๋‚˜ ์น˜ํ™˜ํ•˜์—ฌ ์ „๋‹ฌ null๋กœ ๋ฐ˜ํ™˜ ์•„์˜ˆ ์ œ์™ธํ•˜๊ณ  ๋ฐ˜ํ™˜ @JsonIgnore Response ๋ฐ˜ํ™˜ ์‹œ Json๊ฐ’์— ํฌํ•จ๋˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” annotation. ๊ฐœ๋ณ„์ ์œผ๋กœ ํ•„๋“œ ๊ฐ’์„ ์ œ์–ดํ•œ๋‹ค. @JsonIgnore // ๋ฐ˜ํ™˜ ์‹œ Json๊ฐ’์— ํฌํ•จ๋˜์ง€ ์•Š์Œ ->๋ฐ˜ํ™˜๊ฐ’ ์ œ์–ด privateString password; @JsonIgnore privateString ssn; @JsonIgnoreProperties ํ•ด๋‹น ํด๋ž˜์Šค ๋ธ”๋ก ๋‹จ์œ„๋กœ ํ•„ํ„ฐ๋ง @JsonIgnoreProperties(value={"password", "ssn"}) public c.. 2022. 2. 18.
[Spring Boot/RESTful] Response ๋ฐ์ดํ„ฐ ํ˜•์‹ ๋ฐ˜ํ™˜(XML format) XML ํŒŒ์ผ ํ˜ธ์ถœ Postman header์—์„œ key: Accept, value: application/xml๋กœ ๋ณ€ํ™˜ ์‹œ xmlํŒŒ์ผ ํ˜ธ์ถœ ๊ฐ€๋Šฅ XML ํŒŒ์ผ ๋ฐ˜ํ™˜ ํ—ˆ์šฉ pom.xml ํŒŒ์ผ์— ๊ด€๋ จ dependency ์ถ”๊ฐ€ com.fasterxml.jackson.dataformat jackson-dataformat-xml 2.10.2 ⇒ ์„ค์ •ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ 406 ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค 406 Not Acceptable ์„œ๋ฒ„๊ฐ€ ์š”์ฒญ์˜ ์‚ฌ์ „ ์ฝ˜ํ…์ธ  ํ˜‘์ƒ ํ—ค๋”์— ์ ์šฉ๋œ ํ—ˆ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฐ’ ๋ชฉ๋ก๊ณผ ์ผ์น˜ํ•˜๋Š” ์‘๋‹ต์„ ์ƒ์„ฑํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ์„œ๋ฒ„๊ฐ€ ๊ธฐ๋ณธ ํ‘œํ˜„์„ ์ œ๊ณตํ•  ์˜์‚ฌ๊ฐ€ ์—†์–ด์„œ ๋ฐœ์ƒ [์‚ฌ์ „ ์ฝ˜ํ…์ธ  ํ˜‘์ƒ ํ—ค๋”์˜ ์ข…๋ฅ˜] [Accept]() [Accept-Encoding]() [Accept-Language]() ์„œ๋ฒ„๋Š” ๋ณดํ†ต ๊ด€๋ จ ํ—ค๋”๋ฅผ ๋ฌด.. 2022. 2. 18.
[Spring Boot/RESTful] Validation API์™€ Internationalization Validation API ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ์ฒดํฌ -> @Valid annotation ์‚ฌ์šฉ 1. dependency ์ถ”๊ฐ€ org.hibernate.validator hibernate-validator 6.0.7.Final 2. dto์— ์ œ์•ฝ์กฐ๊ฑด ์ถ”๊ฐ€ @Data @AllArgsConstructor public class User { private Integer id; @Size(min=2, message = "Name์€ 2๊ธ€์ž ์ด์ƒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.") private String name; private String password; @Past // ๊ณผ๊ฑฐ ๋‚ ์งœ๋งŒ ๊ฐ€๋Šฅํ•œ ์ œ์•ฝ ์กฐ๊ฑด private Date joinDate; } 3. controller์— ์ถ”๊ฐ€ JDK์— ํฌํ•จ๋œ API์™€ hibe.. 2022. 2. 18.