-
Notifications
You must be signed in to change notification settings - Fork 0
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
RequestContextHolder를 이용한 세션 관리
- Loading branch information
Showing
8 changed files
with
139 additions
and
24 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
27 changes: 27 additions & 0 deletions
27
src/main/java/com/market/saessag/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,27 @@ | ||
package com.market.saessag.global.config; | ||
|
||
import com.market.saessag.global.interceptor.SessionInterceptor; | ||
import com.market.saessag.global.util.PathConst; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.context.request.RequestContextListener; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
@Autowired | ||
private SessionInterceptor sessionInterceptor; | ||
@Bean | ||
public RequestContextListener requestContextListener() { | ||
return new RequestContextListener(); | ||
} | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(sessionInterceptor) | ||
.addPathPatterns("/**") // 모든 경로에 인터셉터 적용 | ||
.excludePathPatterns(PathConst.EXCLUDED_PATHS); // 제외할 경로 설정 | ||
} | ||
} |
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
29 changes: 15 additions & 14 deletions
29
src/main/java/com/market/saessag/global/exception/GlobalExceptionHandler.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
31 changes: 31 additions & 0 deletions
31
src/main/java/com/market/saessag/global/interceptor/SessionInterceptor.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,31 @@ | ||
package com.market.saessag.global.interceptor; | ||
|
||
import com.market.saessag.global.exception.CustomException; | ||
import com.market.saessag.global.exception.ErrorCode; | ||
import com.market.saessag.global.util.PathConst; | ||
import com.market.saessag.global.util.SessionUtil; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
@Component | ||
public class SessionInterceptor implements HandlerInterceptor { | ||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
String requestUrl = request.getRequestURI(); | ||
|
||
// PathConst의 허용된 URL 목록 사용 | ||
for (String url : PathConst.EXCLUDED_PATHS) { | ||
if (requestUrl.startsWith(url)) { | ||
return true; | ||
} | ||
} | ||
|
||
// 그 외의 URL은 세션 체크 후 에러 처리 | ||
if (SessionUtil.getData("user") == null) { | ||
throw new CustomException(ErrorCode.UNAUTHORIZED); | ||
} | ||
return true; | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/main/java/com/market/saessag/global/util/PathConst.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 com.market.saessag.global.util; | ||
|
||
// 제외할 경로 공통 상수 클래스 | ||
public class PathConst { | ||
public static final String[] EXCLUDED_PATHS = { | ||
"/api/sign-up", | ||
"/api/sign-in", | ||
"/error" | ||
}; | ||
|
||
private PathConst() {} // 인스턴스화 방지 | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/market/saessag/global/util/SessionUtil.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 com.market.saessag.global.util; | ||
|
||
import jakarta.servlet.http.HttpSession; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
@Component | ||
public class SessionUtil { | ||
// 세션 가져오기 | ||
public static HttpSession getSession() { | ||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); | ||
return attributes.getRequest().getSession(); | ||
} | ||
|
||
// 세션에 데이터 저장 | ||
public static void setData(String key, Object value) { | ||
getSession().setAttribute(key, value); | ||
} | ||
|
||
// 세션에서 데이터 가져오기 | ||
public static Object getData(String key) { | ||
return getSession().getAttribute(key); | ||
} | ||
|
||
// 특정 세션 삭제 | ||
public static void removeData(String key) { | ||
getSession().removeAttribute(key); | ||
} | ||
|
||
// 모든 세션 삭제 | ||
public static void clear() { | ||
getSession().invalidate(); | ||
} | ||
} | ||
|