Skip to content

Commit 73efb6b

Browse files
authored
merge: 화이트리스트 토큰 검증 로직 수정 PERMIT-SEOUL#255
feat: 화이트리스트 토큰 검증 로직 수정 PERMIT-SEOUL#255
2 parents dffc961 + 2030bfc commit 73efb6b

File tree

4 files changed

+32
-44
lines changed

4 files changed

+32
-44
lines changed

src/main/java/com/permitseoul/permitserver/domain/admin/timetable/block/api/controller/AdminNotionTimetableBlockController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.permitseoul.permitserver.global.response.ApiResponseUtil;
77
import com.permitseoul.permitserver.global.response.BaseResponse;
88
import com.permitseoul.permitserver.global.response.code.SuccessCode;
9-
import jakarta.validation.Valid;
109
import lombok.RequiredArgsConstructor;
1110
import org.springframework.http.ResponseEntity;
1211
import org.springframework.web.bind.annotation.*;

src/main/java/com/permitseoul/permitserver/global/config/SecurityConfig.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,14 @@ public class SecurityConfig {
2626
private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
2727
private final ExceptionHandlerFilter exceptionHandlerFilter;
2828

29-
private static final String[] whiteURIList = {
29+
private static final String[] whiteURIListNotUsingToken = {
3030
"/actuator/health",
3131
"/api/users/signup",
3232
"/api/users/login",
3333
"/api/users/reissue",
3434
"/api/events",
3535
"/api/events/detail/*",
3636
"/api/users/email-check",
37-
"/api/events/*/timetables",
38-
"/api/events/timetables/*",
3937
"/api/tickets/info/*",
4038
"/api/tickets/door/staff/confirm",
4139
"/api/tickets/door/validation/*",
@@ -44,6 +42,11 @@ public class SecurityConfig {
4442
"/api/events/*/sitemap",
4543
};
4644

45+
private static final String[] whiteURIListUsingToken = {
46+
"/api/events/*/timetables", // userId 있으면 개인화
47+
"/api/events/timetables/*", // userId 있으면 개인화
48+
};
49+
4750
private static final String[] adminURIList = {
4851
"/api/admin/**"
4952
};
@@ -70,17 +73,16 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti
7073
.csrf(AbstractHttpConfigurer::disable)
7174
.formLogin(AbstractHttpConfigurer::disable)
7275
.httpBasic(AbstractHttpConfigurer::disable)
73-
.sessionManagement(sessionManagementConfigurer ->
74-
sessionManagementConfigurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
75-
.exceptionHandling(exceptionHandlingConfigurer ->
76-
exceptionHandlingConfigurer.authenticationEntryPoint(jwtAuthenticationEntryPoint))
76+
.sessionManagement(sessionManagementConfigurer -> sessionManagementConfigurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
77+
.exceptionHandling(exceptionHandlingConfigurer -> exceptionHandlingConfigurer.authenticationEntryPoint(jwtAuthenticationEntryPoint))
7778
.authorizeHttpRequests(auth -> auth
78-
.requestMatchers(whiteURIList).permitAll() // 로그인 상관 X
79-
.requestMatchers(adminURIList).hasRole(UserRole.ADMIN.name()) // ADMIN 권한 필요
80-
.requestMatchers(staffURIList).hasAnyRole(UserRole.STAFF.name(), UserRole.ADMIN.name()) //staff 권한 이상
79+
.requestMatchers(adminURIList).hasRole(UserRole.ADMIN.name()) // ADMIN// 권한 필요
80+
.requestMatchers(staffURIList).hasAnyRole(UserRole.STAFF.name(), UserRole.ADMIN.name()) // staff 권한 이상
8181
.requestMatchers(authRequiredURIList).authenticated() // 로그인 필수
82-
)
83-
.addFilterBefore(new JwtAuthenticationFilter(jwtProvider, List.of(whiteURIList)), UsernamePasswordAuthenticationFilter.class)
82+
.requestMatchers(whiteURIListNotUsingToken).permitAll() // 로그인 상관 X + AccessToken 사용X
83+
.requestMatchers(whiteURIListUsingToken).permitAll() // 로그인 상관 X + AccessToken 있으면 사용
84+
.anyRequest().denyAll())
85+
.addFilterBefore(new JwtAuthenticationFilter(jwtProvider, List.of(whiteURIListNotUsingToken), List.of(whiteURIListUsingToken)), UsernamePasswordAuthenticationFilter.class)
8486
.addFilterBefore(exceptionHandlerFilter, JwtAuthenticationFilter.class)
8587
.build();
8688
}

src/main/java/com/permitseoul/permitserver/global/filter/JwtAuthenticationFilter.java

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
import com.permitseoul.permitserver.domain.auth.core.exception.AuthWrongJwtException;
66
import com.permitseoul.permitserver.domain.auth.core.jwt.CookieExtractor;
77
import com.permitseoul.permitserver.domain.auth.core.jwt.JwtProvider;
8-
import com.permitseoul.permitserver.global.Constants;
98
import com.permitseoul.permitserver.global.domain.CookieType;
109
import com.permitseoul.permitserver.global.exception.FilterException;
1110
import com.permitseoul.permitserver.global.response.code.ErrorCode;
1211
import jakarta.servlet.FilterChain;
1312
import jakarta.servlet.ServletException;
14-
import jakarta.servlet.http.Cookie;
1513
import jakarta.servlet.http.HttpServletRequest;
1614
import jakarta.servlet.http.HttpServletResponse;
1715
import lombok.NonNull;
@@ -26,38 +24,38 @@
2624
import org.springframework.web.filter.OncePerRequestFilter;
2725

2826
import java.io.IOException;
29-
import java.util.Enumeration;
3027
import java.util.List;
3128

32-
3329
@RequiredArgsConstructor
3430
@Slf4j
3531
public class JwtAuthenticationFilter extends OncePerRequestFilter {
3632
private final JwtProvider jwtProvider;
37-
private final List<String> whiteURIList;
33+
private final List<String> whiteURIListNotUsingToken;
34+
private final List<String> whiteURIListUsingToken;
3835
private final AntPathMatcher pathMatcher = new AntPathMatcher();
39-
private static final String REISSUE_URI = "/api/users/reissue";
40-
private static final String LOGIN_URI = "/api/users/login";
4136
private static final String USER_ID_MDC_KEY = "user_id";
4237
private static final String ANONYMOUS_USER_ID = "anonymous";
4338

39+
@Override
40+
protected boolean shouldNotFilter(@NonNull final HttpServletRequest request) {
41+
return whiteURIListNotUsingToken.stream()
42+
.anyMatch(pattern -> pathMatcher.match(pattern, request.getRequestURI()));
43+
}
44+
4445
@Override
4546
protected void doFilterInternal(@NonNull final HttpServletRequest request,
46-
@NonNull final HttpServletResponse response,
47-
@NonNull final FilterChain filterChain) throws ServletException, IOException {
47+
@NonNull final HttpServletResponse response,
48+
@NonNull final FilterChain filterChain) throws ServletException, IOException {
4849
final String uri = request.getRequestURI();
4950
try {
5051
MDC.put(USER_ID_MDC_KEY, ANONYMOUS_USER_ID);
5152

52-
if(isHealthCheckUri(uri) || isLoginOrReissue(uri)) {
53-
filterChain.doFilter(request, response);
54-
return;
55-
}
5653
setAuthentication(request);
5754
filterChain.doFilter(request, response);
5855
} catch (AuthCookieException e) {
59-
if(isWhiteListUrl(uri)) {
60-
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(null, null, null));
56+
if (isUsingTokenUrl(uri)) {
57+
SecurityContextHolder.getContext().setAuthentication(
58+
new UsernamePasswordAuthenticationToken(null, null, null));
6159
filterChain.doFilter(request, response);
6260
} else {
6361
throw new FilterException(ErrorCode.NOT_FOUND_AT_COOKIE);
@@ -69,14 +67,12 @@ protected void doFilterInternal(@NonNull final HttpServletRequest request,
6967
} catch (ServletException | IOException e) {
7068
log.error("[JWT Filter] unexpected error. ua={}",
7169
request.getHeader("User-Agent"),
72-
e
73-
);
70+
e);
7471
throw new FilterException(ErrorCode.INTERNAL_FILTER_ERROR);
7572
} catch (Exception e) {
7673
log.error("[JWT Filter] unexpected error. ua={}",
7774
request.getHeader("User-Agent"),
78-
e
79-
);
75+
e);
8076
throw new FilterException(ErrorCode.INTERNAL_SERVER_ERROR);
8177
} finally {
8278
MDC.remove(USER_ID_MDC_KEY);
@@ -93,16 +89,7 @@ private void setAuthentication(final HttpServletRequest request) {
9389
new UsernamePasswordAuthenticationToken(userId, null, authorities));
9490
}
9591

96-
private boolean isWhiteListUrl(final String requestURI) {
97-
return whiteURIList.stream().anyMatch(pattern -> pathMatcher.match(pattern, requestURI));
98-
}
99-
100-
private boolean isHealthCheckUri(final String uri) {
101-
return pathMatcher.match(Constants.HEALTH_CHECK_URL, uri);
102-
}
103-
104-
private boolean isLoginOrReissue(final String uri) {
105-
return pathMatcher.match(LOGIN_URI, uri)
106-
|| pathMatcher.match(REISSUE_URI, uri);
92+
private boolean isUsingTokenUrl(final String requestURI) {
93+
return whiteURIListUsingToken.stream().anyMatch(pattern -> pathMatcher.match(pattern, requestURI));
10794
}
10895
}

src/main/java/com/permitseoul/permitserver/global/filter/RequestObservabilityFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
@Component
2020
@Slf4j
21-
//@Profile("!local")
21+
@Profile("!local")
2222
@Order(Ordered.HIGHEST_PRECEDENCE)
2323
class RequestObservabilityFilter extends OncePerRequestFilter {
2424
private static final String NGINX_REQUEST_ID = "X-Request-ID";

0 commit comments

Comments
 (0)