-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REFACTOR(auth) :: security를 custom intercepter로 구현
- Loading branch information
1 parent
ddf7e7e
commit 44d18e6
Showing
122 changed files
with
794 additions
and
800 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/main/java/com/woongeya/zoing/domain/annotation/AdminOnly.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,13 @@ | ||
package com.woongeya.zoing.domain.annotation; | ||
|
||
import static java.lang.annotation.ElementType.*; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@LoginRequired | ||
public @interface AdminOnly { | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/woongeya/zoing/domain/annotation/LoginOrNot.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,13 @@ | ||
package com.woongeya.zoing.domain.annotation; | ||
|
||
import static java.lang.annotation.ElementType.*; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
|
||
@Target({METHOD, ANNOTATION_TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface LoginOrNot { | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/woongeya/zoing/domain/annotation/LoginRequired.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,13 @@ | ||
package com.woongeya.zoing.domain.annotation; | ||
|
||
import static java.lang.annotation.ElementType.*; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({METHOD, ANNOTATION_TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@LoginOrNot | ||
public @interface LoginRequired { | ||
} |
6 changes: 3 additions & 3 deletions
6
...ain/java/com/woongeya/zoing/domain/application/exception/AlreadyApplicationException.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
6 changes: 3 additions & 3 deletions
6
...in/java/com/woongeya/zoing/domain/application/exception/ApplicationNotFoundException.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
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
8 changes: 8 additions & 0 deletions
8
src/main/java/com/woongeya/zoing/domain/auth/domain/Token.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 com.woongeya.zoing.domain.auth.domain; | ||
|
||
|
||
public record Token ( | ||
String accessToken, | ||
String refreshToken | ||
) { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/woongeya/zoing/domain/auth/exception/TokenExpiredException.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,11 @@ | ||
package com.woongeya.zoing.domain.auth.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import com.woongeya.zoing.global.error.JJoingException; | ||
|
||
public class TokenExpiredException extends JJoingException { | ||
public TokenExpiredException() { | ||
super(HttpStatus.FORBIDDEN, "토큰이 만료되었습니다."); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/woongeya/zoing/domain/auth/exception/TokenInvalidException.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,11 @@ | ||
package com.woongeya.zoing.domain.auth.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import com.woongeya.zoing.global.error.JJoingException; | ||
|
||
public class TokenInvalidException extends JJoingException { | ||
public TokenInvalidException() { | ||
super(HttpStatus.UNAUTHORIZED, "토큰이 유효하지 않습니다."); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/woongeya/zoing/domain/auth/exception/TokenMissingException.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,11 @@ | ||
package com.woongeya.zoing.domain.auth.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import com.woongeya.zoing.global.error.JJoingException; | ||
|
||
public class TokenMissingException extends JJoingException { | ||
public TokenMissingException() { | ||
super(HttpStatus.UNAUTHORIZED, "토큰이 없습니다."); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/woongeya/zoing/domain/auth/exception/TokenNotExistException.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,11 @@ | ||
package com.woongeya.zoing.domain.auth.exception; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
|
||
import com.woongeya.zoing.global.error.JJoingException; | ||
|
||
public class TokenNotExistException extends JJoingException { | ||
public TokenNotExistException() { | ||
super(FORBIDDEN, "토큰이 넘어오지 않았습니다."); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/woongeya/zoing/domain/auth/exception/UserIsNotAdminException.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,11 @@ | ||
package com.woongeya.zoing.domain.auth.exception; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
|
||
import com.woongeya.zoing.global.error.JJoingException; | ||
|
||
public class UserIsNotAdminException extends JJoingException { | ||
public UserIsNotAdminException() { | ||
super(UNAUTHORIZED, "사용자가 어드민이 아닙니다."); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/woongeya/zoing/domain/auth/exception/UserNotLoginException.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,13 @@ | ||
package com.woongeya.zoing.domain.auth.exception; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import com.woongeya.zoing.global.error.JJoingException; | ||
|
||
public class UserNotLoginException extends JJoingException { | ||
public UserNotLoginException() { | ||
super(FORBIDDEN, "유저가 로그인하지 않았습니다."); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/com/woongeya/zoing/domain/auth/intercepter/AuthInterceptor.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,69 @@ | ||
package com.woongeya.zoing.domain.auth.intercepter; | ||
|
||
import static org.springframework.http.HttpHeaders.*; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.web.method.HandlerMethod; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import com.woongeya.zoing.domain.annotation.AdminOnly; | ||
import com.woongeya.zoing.domain.annotation.LoginOrNot; | ||
import com.woongeya.zoing.domain.annotation.LoginRequired; | ||
import com.woongeya.zoing.domain.auth.exception.TokenNotExistException; | ||
import com.woongeya.zoing.domain.auth.exception.UserIsNotAdminException; | ||
import com.woongeya.zoing.domain.auth.repository.AuthRepository; | ||
import com.woongeya.zoing.domain.auth.util.BearerTokenExtractor; | ||
import com.woongeya.zoing.domain.auth.util.JwtParser; | ||
import com.woongeya.zoing.domain.user.UserFacade; | ||
import com.woongeya.zoing.domain.user.domain.User; | ||
import com.woongeya.zoing.domain.user.domain.autority.Authority; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class AuthInterceptor implements HandlerInterceptor { | ||
|
||
private final JwtParser jwtParser; | ||
private final AuthRepository authRepository; | ||
private final AuthRepository authRepository; | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
if (handler instanceof HandlerMethod hm) { | ||
if (hm.hasMethodAnnotation(LoginOrNot.class)) { | ||
String bearer = request.getHeader(AUTHORIZATION); | ||
|
||
if (bearer == null) { | ||
authRepository.updateCurrentUser(null); | ||
} else { | ||
String jwt = BearerTokenExtractor.extract(bearer); | ||
Long userId = jwtParser.getIdFromJwt(jwt); | ||
User user = userFacade.getUserById(userId); | ||
authRepository.updateCurrentUser(user); | ||
} | ||
} | ||
|
||
if (hm.hasMethodAnnotation(LoginRequired.class)) { | ||
if (authRepository.getCurrentUser() == null) { | ||
throw new TokenNotExistException(); | ||
} | ||
} | ||
if (hm.hasMethodAnnotation(AdminOnly.class)) { | ||
User currentUser = authRepository.getCurrentUser(); | ||
shouldUserAdmin(currentUser); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static void shouldUserAdmin(User currentUser) { | ||
if (currentUser.getAuthority() != Authority.ADMIN) { | ||
throw new UserIsNotAdminException(); | ||
} | ||
} | ||
} |
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
6 changes: 6 additions & 0 deletions
6
...main/java/com/woongeya/zoing/domain/auth/presetation/dto/request/RefreshTokenRequest.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,6 @@ | ||
package com.woongeya.zoing.domain.auth.presetation.dto.request; | ||
|
||
public record RefreshTokenRequest ( | ||
String refreshToken | ||
) { | ||
} |
6 changes: 4 additions & 2 deletions
6
src/main/java/com/woongeya/zoing/domain/auth/presetation/dto/response/TokenResponse.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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
package com.woongeya.zoing.domain.auth.presetation.dto.response; | ||
|
||
import com.woongeya.zoing.domain.auth.domain.Token; | ||
|
||
public record TokenResponse ( | ||
String accessToken, | ||
String refreshToken | ||
) { | ||
|
||
public static TokenResponse of(String accessToken, String refreshToken) { | ||
return new TokenResponse(accessToken, refreshToken); | ||
public static TokenResponse from(Token token) { | ||
return new TokenResponse(token.accessToken(), token.refreshToken()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/woongeya/zoing/domain/auth/repository/AuthRepository.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,28 @@ | ||
package com.woongeya.zoing.domain.auth.repository; | ||
|
||
import org.springframework.stereotype.Repository; | ||
import org.springframework.web.context.annotation.RequestScope; | ||
|
||
import com.woongeya.zoing.domain.auth.exception.UserNotLoginException; | ||
import com.woongeya.zoing.domain.user.domain.User; | ||
|
||
@Repository | ||
@RequestScope | ||
public class AuthRepository { | ||
private User currentUser; | ||
|
||
public User getCurrentUser() { | ||
if (currentUser == null) { | ||
throw new UserNotLoginException(); | ||
} | ||
return currentUser; | ||
} | ||
|
||
public User getNullableCurrentUser() { | ||
return currentUser; | ||
} | ||
|
||
public void updateCurrentUser(User currentUser) { | ||
this.currentUser = currentUser; | ||
} | ||
} |
Oops, something went wrong.