-
Notifications
You must be signed in to change notification settings - Fork 2
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
…rization [Feat] #2 authorization을 구현했어요.
- Loading branch information
Showing
21 changed files
with
483 additions
and
64 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/main/java/team7/inplace/global/exception/InplaceException.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 team7.inplace.global.exception; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
import team7.inplace.global.exception.code.ErrorCode; | ||
|
||
@Getter | ||
public class InplaceException extends RuntimeException { | ||
|
||
private final HttpStatus httpStatus; | ||
private final String errorCode; | ||
private final String errorMessage; | ||
|
||
private InplaceException(ErrorCode errorCode) { | ||
super(errorCode.message()); | ||
this.httpStatus = errorCode.httpStatus(); | ||
this.errorCode = errorCode.code(); | ||
this.errorMessage = errorCode.message(); | ||
} | ||
|
||
public static InplaceException of(ErrorCode errorCode) { | ||
return new InplaceException(errorCode); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/team7/inplace/global/exception/code/AuthorizationErrorCode.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,32 @@ | ||
package team7.inplace.global.exception.code; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public enum AuthorizationErrorCode implements ErrorCode { | ||
TOKEN_IS_EMPTY(HttpStatus.BAD_REQUEST, "A001", "Token is Empty"), | ||
INVALID_TOKEN(HttpStatus.BAD_REQUEST, "A002", "Invalid Token"), | ||
TOKEN_IS_EXPIRED(HttpStatus.BAD_REQUEST, "A003", "Token is Expired"); | ||
|
||
private final HttpStatus httpStatus; | ||
private final String errorCode; | ||
private final String message; | ||
|
||
@Override | ||
public HttpStatus httpStatus() { | ||
return httpStatus; | ||
} | ||
|
||
@Override | ||
public String code() { | ||
return errorCode; | ||
} | ||
|
||
@Override | ||
public String message() { | ||
return message; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/team7/inplace/global/exception/code/ErrorCode.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,12 @@ | ||
package team7.inplace.global.exception.code; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public interface ErrorCode { | ||
|
||
HttpStatus httpStatus(); | ||
|
||
String code(); | ||
|
||
String message(); | ||
} |
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
23 changes: 23 additions & 0 deletions
23
src/main/java/team7/inplace/security/config/SecurityFilterConfig.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,23 @@ | ||
package team7.inplace.security.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.stereotype.Component; | ||
import team7.inplace.security.filter.AuthorizationFilter; | ||
import team7.inplace.security.filter.ExceptionHandlingFilter; | ||
import team7.inplace.security.util.JwtUtil; | ||
|
||
@Component | ||
@Configuration | ||
public class SecurityFilterConfig { | ||
|
||
@Bean | ||
public AuthorizationFilter authorizationFilter(JwtUtil jwtUtil) { | ||
return new AuthorizationFilter(jwtUtil); | ||
} | ||
|
||
@Bean | ||
public ExceptionHandlingFilter exceptionHandlingFilter() { | ||
return new ExceptionHandlingFilter(); | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
src/main/java/team7/inplace/security/filter/AuthorizationFilter.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,70 @@ | ||
package team7.inplace.security.filter; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
import team7.inplace.global.exception.InplaceException; | ||
import team7.inplace.global.exception.code.AuthorizationErrorCode; | ||
import team7.inplace.security.application.dto.CustomOAuth2User; | ||
import team7.inplace.security.util.JwtUtil; | ||
|
||
public class AuthorizationFilter extends OncePerRequestFilter { | ||
|
||
private final JwtUtil jwtUtil; | ||
|
||
public AuthorizationFilter(JwtUtil jwtUtil) { | ||
this.jwtUtil = jwtUtil; | ||
} | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, | ||
FilterChain filterChain) | ||
throws ServletException, IOException { | ||
Cookie[] cookies = request.getCookies(); | ||
if (Objects.isNull(cookies)) { | ||
filterChain.doFilter(request, response); | ||
return; | ||
} | ||
String token = getTokenCookie(cookies).getValue(); | ||
addUserToAuthentication(token); | ||
filterChain.doFilter(request, response); | ||
} | ||
|
||
private Cookie getTokenCookie(Cookie[] cookies) throws InplaceException { | ||
Cookie tokenCookie = Arrays.stream(cookies) | ||
.filter(cookie -> cookie.getName().equals("Authorization")) | ||
.findFirst() | ||
.orElseThrow(() -> InplaceException.of(AuthorizationErrorCode.TOKEN_IS_EMPTY)); | ||
validateToken(tokenCookie); | ||
return tokenCookie; | ||
} | ||
|
||
private void addUserToAuthentication(String token) throws InplaceException { | ||
String username = jwtUtil.getUsername(token); | ||
Long id = jwtUtil.getId(token); | ||
CustomOAuth2User customOAuth2User = new CustomOAuth2User(username, id, false); | ||
Authentication authToken = new UsernamePasswordAuthenticationToken(customOAuth2User, null); | ||
SecurityContextHolder.getContext().setAuthentication(authToken); | ||
} | ||
|
||
private void validateToken(Cookie authorizationCookie) throws InplaceException { | ||
validateTokenEmpty(authorizationCookie); | ||
jwtUtil.validateExpired(authorizationCookie.getValue()); | ||
} | ||
|
||
private void validateTokenEmpty(Cookie authorizationCookie) throws InplaceException { | ||
if (authorizationCookie.getValue() == null) { | ||
throw InplaceException.of(AuthorizationErrorCode.TOKEN_IS_EMPTY); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.