-
Notifications
You must be signed in to change notification settings - Fork 8
feat: 웹, 어드민 간 refresh token 분리 #732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
whqtker
wants to merge
7
commits into
develop
Choose a base branch
from
fix/730-seperate-token-admin-web
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1c53aaa
feat: AdminRefreshToken 클래스 작성 및 관련 설정 추가
whqtker c9ed44f
feat: cookie manager 추가
whqtker a08cef1
feat: 어드민 refresh token 관련 error code 작성
whqtker 8db2d89
feat: provider에 관련 메서드 추가
whqtker 644dd3e
feat: 어드민 로그인 관련 비즈니즈 로직, DTO 작성
whqtker bf17d49
feat: 어드민 로그인 관련 컨트롤러 구현
whqtker 563c0f1
feat: 어드민 로그인 관련은 인증 없이 접근 가능하도록 스프링 시큐리티 설정 변경
whqtker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/main/java/com/example/solidconnection/admin/auth/controller/AdminAuthController.java
This file contains hidden or 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,63 @@ | ||
| package com.example.solidconnection.admin.auth.controller; | ||
|
|
||
| import com.example.solidconnection.admin.auth.dto.AdminReissueResponse; | ||
| import com.example.solidconnection.admin.auth.dto.AdminSignInRequest; | ||
| import com.example.solidconnection.admin.auth.dto.AdminSignInResponse; | ||
| import com.example.solidconnection.admin.auth.dto.AdminSignInResult; | ||
| import com.example.solidconnection.admin.auth.service.AdminAuthService; | ||
| import com.example.solidconnection.common.exception.CustomException; | ||
| import com.example.solidconnection.common.exception.ErrorCode; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/admin/auth") | ||
| @RequiredArgsConstructor | ||
| public class AdminAuthController { | ||
|
|
||
| private final AdminAuthService adminAuthService; | ||
| private final AdminRefreshTokenCookieManager adminRefreshTokenCookieManager; | ||
|
|
||
| @PostMapping("/sign-in") | ||
| public ResponseEntity<AdminSignInResponse> signIn( | ||
| @RequestBody @Valid AdminSignInRequest request, | ||
| HttpServletResponse response | ||
| ) { | ||
| AdminSignInResult result = adminAuthService.signIn(request); | ||
| adminRefreshTokenCookieManager.setCookie(response, result.adminRefreshToken()); | ||
| return ResponseEntity.ok(AdminSignInResponse.from(result.accessToken())); | ||
| } | ||
|
|
||
| @PostMapping("/reissue") | ||
| public ResponseEntity<AdminReissueResponse> reissue(HttpServletRequest request) { | ||
| String adminRefreshToken = adminRefreshTokenCookieManager.getAdminRefreshToken(request); | ||
| AdminReissueResponse reissueResponse = adminAuthService.reissue(adminRefreshToken); | ||
| return ResponseEntity.ok(reissueResponse); | ||
| } | ||
|
|
||
| @PostMapping("/sign-out") | ||
| public ResponseEntity<Void> signOut( | ||
| Authentication authentication, | ||
| HttpServletResponse response | ||
| ) { | ||
| String accessToken = getAccessToken(authentication); | ||
| adminAuthService.signOut(accessToken); | ||
| adminRefreshTokenCookieManager.deleteCookie(response); | ||
| return ResponseEntity.ok().build(); | ||
| } | ||
|
|
||
| private String getAccessToken(Authentication authentication) { | ||
| if (authentication == null || !(authentication.getCredentials() instanceof String accessToken)) { | ||
| throw new CustomException(ErrorCode.AUTHENTICATION_FAILED, "엑세스 토큰이 없습니다."); | ||
| } | ||
| return accessToken; | ||
| } | ||
| } |
69 changes: 69 additions & 0 deletions
69
...ava/com/example/solidconnection/admin/auth/controller/AdminRefreshTokenCookieManager.java
This file contains hidden or 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,69 @@ | ||
| package com.example.solidconnection.admin.auth.controller; | ||
|
|
||
| import static com.example.solidconnection.common.exception.ErrorCode.ADMIN_REFRESH_TOKEN_NOT_EXISTS; | ||
|
|
||
| import com.example.solidconnection.admin.auth.controller.config.AdminRefreshTokenCookieProperties; | ||
| import com.example.solidconnection.auth.token.config.TokenProperties; | ||
| import com.example.solidconnection.common.exception.CustomException; | ||
| import jakarta.servlet.http.Cookie; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import java.time.Duration; | ||
| import java.util.Arrays; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.boot.web.server.Cookie.SameSite; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.http.ResponseCookie; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class AdminRefreshTokenCookieManager { | ||
|
|
||
| private static final String PATH = "/"; | ||
|
|
||
| private final AdminRefreshTokenCookieProperties properties; | ||
| private final TokenProperties tokenProperties; | ||
|
|
||
| public void setCookie(HttpServletResponse response, String adminRefreshToken) { | ||
| Duration tokenExpireTime = tokenProperties.adminRefresh().expireTime(); | ||
| long cookieMaxAge = tokenExpireTime.toSeconds(); | ||
| setAdminRefreshTokenCookie(response, adminRefreshToken, cookieMaxAge); | ||
| } | ||
|
|
||
| public void deleteCookie(HttpServletResponse response) { | ||
| setAdminRefreshTokenCookie(response, "", 0); | ||
| } | ||
|
|
||
| private void setAdminRefreshTokenCookie( | ||
| HttpServletResponse response, String adminRefreshToken, long maxAge | ||
| ) { | ||
| ResponseCookie cookie = ResponseCookie.from(properties.cookieName(), adminRefreshToken) | ||
| .httpOnly(true) | ||
| .secure(true) | ||
| .path(PATH) | ||
| .maxAge(maxAge) | ||
| .domain(properties.cookieDomain()) | ||
| .sameSite(SameSite.LAX.attributeValue()) | ||
| .build(); | ||
| response.addHeader(HttpHeaders.SET_COOKIE, cookie.toString()); | ||
| } | ||
|
|
||
| public String getAdminRefreshToken(HttpServletRequest request) { | ||
| Cookie[] cookies = request.getCookies(); | ||
| if (cookies == null || cookies.length == 0) { | ||
| throw new CustomException(ADMIN_REFRESH_TOKEN_NOT_EXISTS); | ||
| } | ||
|
|
||
| Cookie adminRefreshTokenCookie = Arrays.stream(cookies) | ||
| .filter(cookie -> properties.cookieName().equals(cookie.getName())) | ||
| .findFirst() | ||
| .orElseThrow(() -> new CustomException(ADMIN_REFRESH_TOKEN_NOT_EXISTS)); | ||
|
|
||
| String adminRefreshToken = adminRefreshTokenCookie.getValue(); | ||
| if (adminRefreshToken == null || adminRefreshToken.isBlank()) { | ||
| throw new CustomException(ADMIN_REFRESH_TOKEN_NOT_EXISTS); | ||
| } | ||
| return adminRefreshToken; | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
...ample/solidconnection/admin/auth/controller/config/AdminRefreshTokenCookieProperties.java
This file contains hidden or 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,11 @@ | ||
| package com.example.solidconnection.admin.auth.controller.config; | ||
|
|
||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
|
||
| @ConfigurationProperties(prefix = "token.admin-refresh") | ||
| public record AdminRefreshTokenCookieProperties( | ||
| String cookieName, | ||
| String cookieDomain | ||
| ) { | ||
|
|
||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/example/solidconnection/admin/auth/dto/AdminReissueResponse.java
This file contains hidden or 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,12 @@ | ||
| package com.example.solidconnection.admin.auth.dto; | ||
|
|
||
| import com.example.solidconnection.auth.domain.AccessToken; | ||
|
|
||
| public record AdminReissueResponse( | ||
| String accessToken | ||
| ) { | ||
|
|
||
| public static AdminReissueResponse from(AccessToken accessToken) { | ||
| return new AdminReissueResponse(accessToken.token()); | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/example/solidconnection/admin/auth/dto/AdminSignInRequest.java
This file contains hidden or 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,10 @@ | ||
| package com.example.solidconnection.admin.auth.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
|
|
||
| public record AdminSignInRequest( | ||
| @NotBlank String email, | ||
| @NotBlank String password | ||
| ) { | ||
|
|
||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/example/solidconnection/admin/auth/dto/AdminSignInResponse.java
This file contains hidden or 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,10 @@ | ||
| package com.example.solidconnection.admin.auth.dto; | ||
|
|
||
| public record AdminSignInResponse( | ||
| String accessToken | ||
| ) { | ||
|
|
||
| public static AdminSignInResponse from(String accessToken) { | ||
| return new AdminSignInResponse(accessToken); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/example/solidconnection/admin/auth/dto/AdminSignInResult.java
This file contains hidden or 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,17 @@ | ||
| package com.example.solidconnection.admin.auth.dto; | ||
|
|
||
| import com.example.solidconnection.auth.domain.AccessToken; | ||
| import com.example.solidconnection.auth.domain.AdminRefreshToken; | ||
|
|
||
| public record AdminSignInResult( | ||
| String accessToken, | ||
| String adminRefreshToken | ||
| ) { | ||
|
|
||
| public static AdminSignInResult of( | ||
| AccessToken accessToken, | ||
| AdminRefreshToken adminRefreshToken | ||
| ) { | ||
| return new AdminSignInResult(accessToken.token(), adminRefreshToken.token()); | ||
| } | ||
| } |
82 changes: 82 additions & 0 deletions
82
src/main/java/com/example/solidconnection/admin/auth/service/AdminAuthService.java
This file contains hidden or 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,82 @@ | ||
| package com.example.solidconnection.admin.auth.service; | ||
|
|
||
| import static com.example.solidconnection.common.exception.ErrorCode.ADMIN_REFRESH_TOKEN_EXPIRED; | ||
| import static com.example.solidconnection.common.exception.ErrorCode.NOT_ADMIN_USER; | ||
| import static com.example.solidconnection.common.exception.ErrorCode.SIGN_IN_FAILED; | ||
|
|
||
| import com.example.solidconnection.admin.auth.dto.AdminReissueResponse; | ||
| import com.example.solidconnection.admin.auth.dto.AdminSignInRequest; | ||
| import com.example.solidconnection.admin.auth.dto.AdminSignInResult; | ||
| import com.example.solidconnection.auth.domain.AccessToken; | ||
| import com.example.solidconnection.auth.domain.AdminRefreshToken; | ||
| import com.example.solidconnection.auth.exception.AuthException; | ||
| import com.example.solidconnection.auth.service.AuthTokenProvider; | ||
| import com.example.solidconnection.auth.token.TokenBlackListService; | ||
| import com.example.solidconnection.common.exception.CustomException; | ||
| import com.example.solidconnection.siteuser.domain.AuthType; | ||
| import com.example.solidconnection.siteuser.domain.Role; | ||
| import com.example.solidconnection.siteuser.domain.SiteUser; | ||
| import com.example.solidconnection.siteuser.repository.SiteUserRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class AdminAuthService { | ||
|
|
||
| private final AuthTokenProvider authTokenProvider; | ||
| private final TokenBlackListService tokenBlackListService; | ||
| private final SiteUserRepository siteUserRepository; | ||
| private final PasswordEncoder passwordEncoder; | ||
|
|
||
| @Transactional | ||
| public AdminSignInResult signIn(AdminSignInRequest request) { | ||
| SiteUser siteUser = getEmailMatchingUserOrThrow(request.email()); | ||
| validatePassword(request.password(), siteUser.getPassword()); | ||
| validateAdminRole(siteUser); | ||
| resetQuitedAt(siteUser); | ||
| AccessToken accessToken = authTokenProvider.generateAccessToken(siteUser); | ||
| AdminRefreshToken adminRefreshToken = authTokenProvider.generateAndSaveAdminRefreshToken(siteUser); | ||
| return AdminSignInResult.of(accessToken, adminRefreshToken); | ||
| } | ||
|
|
||
| private SiteUser getEmailMatchingUserOrThrow(String email) { | ||
| return siteUserRepository.findByEmailAndAuthType(email, AuthType.EMAIL) | ||
| .orElseThrow(() -> new CustomException(SIGN_IN_FAILED)); | ||
| } | ||
|
|
||
| private void validatePassword(String rawPassword, String encodedPassword) { | ||
| if (!passwordEncoder.matches(rawPassword, encodedPassword)) { | ||
| throw new CustomException(SIGN_IN_FAILED); | ||
| } | ||
| } | ||
|
|
||
| private void validateAdminRole(SiteUser siteUser) { | ||
| if (!Role.ADMIN.equals(siteUser.getRole())) { | ||
| throw new CustomException(NOT_ADMIN_USER); | ||
| } | ||
| } | ||
|
|
||
| private void resetQuitedAt(SiteUser siteUser) { | ||
| if (siteUser.getQuitedAt() == null) { | ||
| return; | ||
| } | ||
| siteUser.setQuitedAt(null); | ||
| } | ||
|
|
||
| public AdminReissueResponse reissue(String requestedAdminRefreshToken) { | ||
| if (!authTokenProvider.isValidAdminRefreshToken(requestedAdminRefreshToken)) { | ||
|
whqtker marked this conversation as resolved.
|
||
| throw new AuthException(ADMIN_REFRESH_TOKEN_EXPIRED); | ||
| } | ||
| SiteUser siteUser = authTokenProvider.parseSiteUser(requestedAdminRefreshToken); | ||
| AccessToken newAccessToken = authTokenProvider.generateAccessToken(siteUser); | ||
| return AdminReissueResponse.from(newAccessToken); | ||
| } | ||
|
|
||
| public void signOut(String accessToken) { | ||
| tokenBlackListService.addToBlacklist(accessToken); | ||
| authTokenProvider.deleteAdminRefreshTokenByAccessToken(accessToken); | ||
| } | ||
| } | ||
7 changes: 7 additions & 0 deletions
7
src/main/java/com/example/solidconnection/auth/domain/AdminRefreshToken.java
This file contains hidden or 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,7 @@ | ||
| package com.example.solidconnection.auth.domain; | ||
|
|
||
| public record AdminRefreshToken( | ||
| String token | ||
| ) implements Token { | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
기존 LAX 방식이었던 이유가 어떤 거였는지 알려주실 수 있나요?? 인증 정보가 관리지 웹(www.admins.solid-connection.com)과 서비스 웹(www.solid-connection.com)으로 완전 분리되었으니까 Strict를 해도 되지 않을까라는 생각이 들어 의견을 듣고 싶습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저 합류 이전이라 근거는 잘 모르겠습니다.
그리고 현재 쿠키 도메인이
.solid-connection.com로 설정되어 있어,Strict을 사용해도 쿠키를 분리하는 효과를 기대하긴 어려울 거 같습니다 ..!