-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Spring Security 관련 기능
- Loading branch information
Showing
14 changed files
with
139 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/jungle/HandTris/application/impl/ReissueServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package jungle.HandTris.application.impl; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jungle.HandTris.application.service.ReissueService; | ||
import jungle.HandTris.domain.Member; | ||
import jungle.HandTris.domain.exception.InvalidTokenFormatException; | ||
import jungle.HandTris.domain.exception.RefreshTokenExpiredException; | ||
import jungle.HandTris.domain.exception.UnauthorizedAccessException; | ||
import jungle.HandTris.domain.repo.MemberRepository; | ||
import jungle.HandTris.global.jwt.JWTUtil; | ||
import jungle.HandTris.presentation.dto.response.ReissueTokenRes; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ReissueServiceImpl implements ReissueService { | ||
|
||
private final JWTUtil jwtUtil; | ||
private final MemberRepository memberRepository; | ||
|
||
public ReissueTokenRes reissue (HttpServletRequest request, String requestUsername) { | ||
String refreshToken = jwtUtil.resolveRefreshToken(request); | ||
|
||
//토큰 소멸 시간 검증 | ||
if (jwtUtil.isExpired(refreshToken)) { | ||
throw new RefreshTokenExpiredException(); | ||
} | ||
|
||
String subject = jwtUtil.getSubject(refreshToken); | ||
|
||
if(!subject.equals("RefreshToken")) { | ||
throw new InvalidTokenFormatException(); | ||
} | ||
|
||
String nickname = jwtUtil.getNickname(refreshToken); | ||
Member member = memberRepository.findByUsername(requestUsername); | ||
|
||
if(!member.getRefreshToken().equals(refreshToken)) { | ||
throw new UnauthorizedAccessException(); | ||
} | ||
|
||
String newAccessToken = jwtUtil.createAccessToken(nickname); | ||
String newRefreshToken = jwtUtil.createRefreshToken(nickname); | ||
|
||
member.updateRefreshToken(newRefreshToken); | ||
memberRepository.save(member); | ||
|
||
ReissueTokenRes token = new ReissueTokenRes(newAccessToken, newRefreshToken); | ||
|
||
return token; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/jungle/HandTris/application/service/ReissueService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package jungle.HandTris.application.service; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jungle.HandTris.presentation.dto.response.ReissueTokenRes; | ||
|
||
public interface ReissueService { | ||
ReissueTokenRes reissue (HttpServletRequest request, String requestUsername); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/jungle/HandTris/presentation/ReissueController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package jungle.HandTris.presentation; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jungle.HandTris.application.service.ReissueService; | ||
import jungle.HandTris.global.dto.ResponseEnvelope; | ||
import jungle.HandTris.presentation.dto.response.ReissueTokenRes; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class ReissueController { | ||
|
||
private final ReissueService reissueService; | ||
|
||
@PostMapping("/reissue/{username}") | ||
public ResponseEnvelope<ReissueTokenRes> reissue (HttpServletRequest request, @PathVariable("username") String requestUsername) { | ||
ReissueTokenRes token = reissueService.reissue(request, requestUsername); | ||
|
||
return ResponseEnvelope.of(token); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/jungle/HandTris/presentation/dto/response/ReissueTokenRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package jungle.HandTris.presentation.dto.response; | ||
|
||
public record ReissueTokenRes (String access, | ||
String refresh) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters