DELETE 메소드는 리소스 삭제에 사용된다. 전달 값을 URL로 받기 때문에 추가 보안 조치가 필요한 부분이 있으며, 해당 인증을 JWT를 사용하여 구현하였다.
프로젝트 환경설정
Framework : SpringBoot 2.7.12 | JDK : JAVA 11 | IDE : IntelliJ | DB : OracleDB | build Tool : Gradle
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.3.1'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.oracle.database.jdbc:ojdbc8'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5'
implementation group: 'com.auth0', name: 'java-jwt', version: '4.0.0'
#Application.properties
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.username=C##APITEST
spring.datasource.password=APITEST
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
mybatis.mapper-locations=mapper/*.xml
mybatis.type-aliases-package=com.newbie.training
mybatis.configuration.map-underscore-to-camel-case=true
DELETE Method 구현
DELETE /post/delete/{postNo} 로 요청시 postNo를 통해 일치하는 게시글이 있는지 조회한다.
JWT 토큰을 jwtProvider.parseJwtToken을 사용하여 claim 으로 파싱한다.
- 게시글이 없을 경우 404 not found 를 리턴한다.
- 게시글이 있을 경우 게시글의 작성자와 jwt에서 추출한 id 값이 동일한지 비교한다.
동일할 경우 postNo를 통해 게시글을 삭제하고
동일하지 않을 경우 403 forbidden 을 리턴한다.
Service 에서는 삭제의 성공 여부를 가지고 성공의 경우 200 OK 를 리턴하고
실패시 500 Bad Gateway를 리턴한다.
실행
작성자 이외의 유저가 삭제 요청시 403 에러가 발생한다.
존재하지 않는 리소스를 삭제하려고 요청하면 404 에러가 발생한다.
정상 요청에 게시글 삭제시 200 OK가 리턴된다.