Skip to content

Commit 12ca992

Browse files
authored
Merge pull request #177 from GoToBILL/feature/alert
fix: 유효하지 않은 토큰을 헤더에 넣을시에 permitALl() 엔드포인트임에도 불구하고 토큰 에러나는 것을 수정했습니다.
2 parents 68ef5da + 21df7ba commit 12ca992

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

src/main/java/com/example/cherrydan/oauth/config/SecurityConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example.cherrydan.oauth.config;
22

3+
import com.example.cherrydan.oauth.security.jwt.CustomAuthenticationEntryPoint;
34
import com.example.cherrydan.oauth.security.jwt.JwtAuthenticationFilter;
45
import lombok.RequiredArgsConstructor;
56
import org.springframework.context.annotation.Bean;
@@ -22,6 +23,7 @@
2223
public class SecurityConfig {
2324

2425
private final JwtAuthenticationFilter jwtAuthenticationFilter;
26+
private final CustomAuthenticationEntryPoint customAuthenticationEntryPoint;
2527

2628
@Bean
2729
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
@@ -46,11 +48,17 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
4648
// 공지사항/홈 광고 배너 관련 경로
4749
.requestMatchers("/api/noticeboard/**").permitAll()
4850
.requestMatchers("/api/mypage/version").permitAll()
51+
// sns 연동 콜백
52+
.requestMatchers("/api/v1/sns/oauth/**").permitAll()
4953
// 헬스 체크 관련
5054
.requestMatchers("/actuator/**").permitAll()
5155
// 나머지는 인증 필요
5256
.anyRequest().authenticated()
5357
)
58+
// 인증 실패 시 커스텀 EntryPoint 사용
59+
.exceptionHandling(exception -> exception
60+
.authenticationEntryPoint(customAuthenticationEntryPoint)
61+
)
5462
// JWT 필터 추가
5563
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
5664

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.example.cherrydan.oauth.security.jwt;
2+
3+
import com.example.cherrydan.common.response.ApiResponse;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import jakarta.servlet.http.HttpServletRequest;
6+
import jakarta.servlet.http.HttpServletResponse;
7+
import lombok.RequiredArgsConstructor;
8+
import lombok.extern.slf4j.Slf4j;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.http.MediaType;
11+
import org.springframework.security.core.AuthenticationException;
12+
import org.springframework.security.web.AuthenticationEntryPoint;
13+
import org.springframework.stereotype.Component;
14+
15+
import java.io.IOException;
16+
17+
@Slf4j
18+
@Component
19+
@RequiredArgsConstructor
20+
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
21+
22+
private static final String INVALID_TOKEN_MESSAGE = "유효하지 않은 토큰입니다.";
23+
24+
private final ObjectMapper objectMapper;
25+
26+
@Override
27+
public void commence(HttpServletRequest request, HttpServletResponse response,
28+
AuthenticationException authException) throws IOException {
29+
log.warn("인증되지 않은 사용자의 접근 시도: {}", request.getRequestURI());
30+
31+
response.setStatus(HttpStatus.UNAUTHORIZED.value());
32+
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
33+
response.setCharacterEncoding("UTF-8");
34+
35+
ApiResponse<Void> errorResponse = ApiResponse.error(
36+
HttpStatus.UNAUTHORIZED.value(),
37+
INVALID_TOKEN_MESSAGE
38+
);
39+
40+
response.getWriter().write(objectMapper.writeValueAsString(errorResponse));
41+
}
42+
}

src/main/java/com/example/cherrydan/oauth/security/jwt/JwtAuthenticationFilter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,13 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
7272
filterChain.doFilter(request, response);
7373
} catch (ExpiredJwtException e) {
7474
log.warn("토큰이 만료되었습니다: {}", e.getMessage());
75-
sendErrorResponse(response, HttpStatus.UNAUTHORIZED, "토큰이 만료되었습니다.");
75+
filterChain.doFilter(request, response);
76+
// sendErrorResponse(response, HttpStatus.UNAUTHORIZED, "토큰이 만료되었습니다.");
7677
} catch (Exception ex) {
7778
log.error("JWT 인증 처리 중 오류 발생: {}", ex.getMessage());
7879
SecurityContextHolder.clearContext();
79-
sendErrorResponse(response, HttpStatus.UNAUTHORIZED, "유효하지 않은 토큰입니다.");
80+
filterChain.doFilter(request, response);
81+
// sendErrorResponse(response, HttpStatus.UNAUTHORIZED, "유효하지 않은 토큰입니다.");
8082
}
8183
}
8284

src/main/java/com/example/cherrydan/oauth/security/jwt/JwtTokenProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public void validateToken(String token) {
9494
throw e;
9595
} catch (UnsupportedJwtException e) {
9696
log.error("지원되지 않는 JWT 토큰입니다: {}", e.getMessage());
97+
throw e;
9798
} catch (MalformedJwtException e) {
9899
log.error("잘못된 형식의 JWT 토큰입니다: {}", e.getMessage());
99100
throw e;

0 commit comments

Comments
 (0)