-
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.
Merge pull request #30 from inhooo00/feature/filter
Feat(#29) 필터 기능 구현
- Loading branch information
Showing
18 changed files
with
586 additions
and
69 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
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
3 changes: 3 additions & 0 deletions
3
src/main/java/shop/kkeujeok/kkeujeokbackend/auth/application/AuthService.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,9 +1,12 @@ | ||
package shop.kkeujeok.kkeujeokbackend.auth.application; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import shop.kkeujeok.kkeujeokbackend.auth.api.dto.response.UserInfo; | ||
|
||
public interface AuthService { | ||
UserInfo getUserInfo(String authCode); | ||
|
||
String getProvider(); | ||
|
||
JsonNode getIdToken(String code); | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/main/java/shop/kkeujeok/kkeujeokbackend/auth/exception/EmailNotFoundException.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 shop.kkeujeok.kkeujeokbackend.auth.exception; | ||
|
||
import shop.kkeujeok.kkeujeokbackend.global.error.exception.NotFoundGroupException; | ||
|
||
public class EmailNotFoundException extends NotFoundGroupException { | ||
public EmailNotFoundException(String message) { | ||
super(message); | ||
} | ||
|
||
public EmailNotFoundException() { | ||
this("존재하지 않는 이메일 입니다."); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/shop/kkeujeok/kkeujeokbackend/auth/exception/ExistsMemberEmailException.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 shop.kkeujeok.kkeujeokbackend.auth.exception; | ||
|
||
import shop.kkeujeok.kkeujeokbackend.global.error.exception.InvalidGroupException; | ||
|
||
public class ExistsMemberEmailException extends InvalidGroupException { | ||
public ExistsMemberEmailException(String message) { | ||
super(message); | ||
} | ||
|
||
public ExistsMemberEmailException() { | ||
this("이미 가입한 계정이 있는 이메일 입니다."); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/shop/kkeujeok/kkeujeokbackend/auth/exception/InvalidTokenException.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 shop.kkeujeok.kkeujeokbackend.auth.exception; | ||
|
||
import shop.kkeujeok.kkeujeokbackend.global.error.exception.AuthGroupException; | ||
|
||
public class InvalidTokenException extends AuthGroupException { | ||
public InvalidTokenException(String message) { | ||
super(message); | ||
} | ||
|
||
public InvalidTokenException() { | ||
this("토큰이 유효하지 않습니다."); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/shop/kkeujeok/kkeujeokbackend/global/config/WebConfig.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,35 @@ | ||
package shop.kkeujeok.kkeujeokbackend.global.config; | ||
|
||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import shop.kkeujeok.kkeujeokbackend.global.filter.LogFilter; | ||
import shop.kkeujeok.kkeujeokbackend.global.filter.LoginCheckFilter; | ||
import shop.kkeujeok.kkeujeokbackend.global.jwt.TokenProvider; | ||
|
||
@Configuration | ||
public class WebConfig { | ||
private final TokenProvider tokenProvider; | ||
|
||
public WebConfig(TokenProvider tokenProvider) { | ||
this.tokenProvider = tokenProvider; | ||
} | ||
|
||
@Bean | ||
public FilterRegistrationBean<LogFilter> logFilter() { | ||
FilterRegistrationBean<LogFilter> filterRegistrationBean = new FilterRegistrationBean<>(); | ||
filterRegistrationBean.setFilter(new LogFilter()); // 여기서 만든 필터 클래스 등록 | ||
filterRegistrationBean.setOrder(1); | ||
filterRegistrationBean.addUrlPatterns("/*"); | ||
return filterRegistrationBean; | ||
} | ||
|
||
@Bean | ||
public FilterRegistrationBean<LoginCheckFilter> loginCheckFilter() { | ||
FilterRegistrationBean<LoginCheckFilter> filterRegistrationBean = new FilterRegistrationBean<>(); | ||
filterRegistrationBean.setFilter(new LoginCheckFilter(tokenProvider)); // JWT 토큰 유효성 검사를 위한 필터 클래스 등록 | ||
filterRegistrationBean.setOrder(2); // 1번인 로그필터 다음으로 수행 | ||
filterRegistrationBean.addUrlPatterns("/*"); | ||
return filterRegistrationBean; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/shop/kkeujeok/kkeujeokbackend/global/filter/LogFilter.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,36 @@ | ||
package shop.kkeujeok.kkeujeokbackend.global.filter; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
|
||
import jakarta.servlet.*; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.filter.GenericFilterBean; | ||
|
||
@Slf4j | ||
public class LogFilter extends GenericFilterBean { | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
log.info("log filter doFilter"); | ||
|
||
HttpServletRequest httpRequest = (HttpServletRequest) request; | ||
String requestURI = httpRequest.getRequestURI(); | ||
|
||
String uuid = UUID.randomUUID().toString(); | ||
|
||
try { | ||
log.info("REQUEST [{}][{}]", uuid, requestURI); | ||
chain.doFilter(request, response); | ||
// chain이 없으면 여기서 끝난다. 즉, 로그만 띄우고 컨트롤러까지 가지 않아서 백지만 나온다. | ||
// chain doFilter로 다시 호출해주면 controller로 넘어가서 정상적으로 페이지를 띄운다. | ||
} catch (Exception e) { | ||
throw e; | ||
} finally { | ||
log.info("REQUEST [{}][{}]", uuid, requestURI); | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/shop/kkeujeok/kkeujeokbackend/global/filter/LoginCheckFilter.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 shop.kkeujeok.kkeujeokbackend.global.filter; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.util.PatternMatchUtils; | ||
import org.springframework.web.filter.GenericFilterBean; | ||
import shop.kkeujeok.kkeujeokbackend.global.filter.exceptiton.AuthenticationException; | ||
import shop.kkeujeok.kkeujeokbackend.global.jwt.TokenProvider; | ||
|
||
import java.io.IOException; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class LoginCheckFilter extends GenericFilterBean { | ||
|
||
private static final String[] whiteList = { | ||
"*", // 일단 다 열어둠 | ||
// "/", | ||
// "/api/oauth2/callback/**", | ||
// "/api/*/token", | ||
// "/api/token/access", | ||
}; | ||
|
||
private final TokenProvider tokenProvider; | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
HttpServletRequest httpRequest = (HttpServletRequest) request; | ||
HttpServletResponse httpResponse = (HttpServletResponse) response; | ||
String requestURI = httpRequest.getRequestURI(); | ||
try { | ||
log.info("인증 체크 필터 시작{}", requestURI); | ||
if (!isLoginCheckPath(requestURI)) { | ||
log.info("인증 체크 로직 실행{}", requestURI); | ||
String token = resolveToken(httpRequest); | ||
if (token == null || !tokenProvider.validateToken(token)) { | ||
log.info("미인증 사용자 요청 {}", requestURI); | ||
httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); | ||
return; | ||
} | ||
// 토큰이 유효한 경우 사용자 정보를 로그로 출력 | ||
} | ||
chain.doFilter(request, response); | ||
} catch (AuthenticationException e) { | ||
throw e; | ||
} finally { | ||
log.info("인증 체크 필터 종료{}", requestURI); | ||
} | ||
} | ||
|
||
private boolean isLoginCheckPath(String requestURI) { | ||
return PatternMatchUtils.simpleMatch(whiteList, requestURI); // 화이트리스트에 있는 경로는 true 반환 | ||
} | ||
|
||
private String resolveToken(HttpServletRequest request) { | ||
String bearerToken = request.getHeader("Authorization"); | ||
if (bearerToken != null && bearerToken.startsWith("Bearer ")) { | ||
return bearerToken.substring(7); | ||
} | ||
return null; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
.../java/shop/kkeujeok/kkeujeokbackend/global/filter/exceptiton/AuthenticationException.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,14 @@ | ||
package shop.kkeujeok.kkeujeokbackend.global.filter.exceptiton; | ||
|
||
import shop.kkeujeok.kkeujeokbackend.global.error.exception.AuthGroupException; | ||
|
||
public class AuthenticationException extends AuthGroupException { | ||
|
||
public AuthenticationException(String message) { | ||
super(message); | ||
} | ||
|
||
public AuthenticationException() { | ||
this("인증에 실패했습니다. 자격 증명을 확인하고 다시 시도하십시오."); | ||
} | ||
} |
Oops, something went wrong.