Skip to content

Commit 3411772

Browse files
committed
refactor: 정규식 간소화
1 parent 005f1f1 commit 3411772

File tree

1 file changed

+9
-22
lines changed

1 file changed

+9
-22
lines changed

src/main/java/com/example/log4u/common/oauth2/jwt/JwtAuthenticationFilter.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@
33
import java.io.IOException;
44
import java.io.PrintWriter;
55

6-
import org.springframework.beans.factory.annotation.Value;
76
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
87
import org.springframework.security.core.Authentication;
98
import org.springframework.security.core.context.SecurityContextHolder;
10-
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
11-
import org.springframework.stereotype.Component;
129
import org.springframework.web.filter.OncePerRequestFilter;
1310

1411
import com.example.log4u.common.oauth2.dto.CustomOAuth2User;
15-
import com.example.log4u.common.oauth2.dto.UserCreateRequestDto;
16-
import com.example.log4u.domain.user.entity.User;
1712
import com.example.log4u.domain.user.service.UserService;
1813

1914
import io.jsonwebtoken.ExpiredJwtException;
@@ -32,32 +27,26 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
3227
private final JwtUtil jwtUtil;
3328
private final UserService userService;
3429

35-
@Value("${jwt.access-token-expire-time-seconds}")
36-
private long accessTokenValiditySeconds;
37-
38-
@Value("${jwt.refresh-token-expire-time-seconds}")
39-
private long refreshTokenValiditySeconds;
40-
4130
@Override
4231
protected void doFilterInternal(
4332
HttpServletRequest request,
4433
@NonNull HttpServletResponse response,
4534
@NonNull FilterChain filterChain
4635
) throws ServletException, IOException {
4736
String requestUri = request.getRequestURI();
48-
if (requestUri.matches("^\\/login(?:\\/.*)?$")) {
37+
if (requestUri.matches("^/login(/.*)?$")) {
4938
filterChain.doFilter(request, response);
5039
return;
5140
}
52-
if (requestUri.matches("^\\/oauth2(?:\\/.*)?$")) {
41+
if (requestUri.matches("^/oauth2(/.*)?$")) {
5342
filterChain.doFilter(request, response);
5443
return;
5544
}
5645

5746
// 쿠키에서 access키에 담긴 토큰 추출
5847
String accessToken = null;
5948
Cookie[] cookies = request.getCookies();
60-
for (Cookie cookie : cookies){
49+
for (Cookie cookie : cookies) {
6150
if (cookie.getName().equals("access")) {
6251
accessToken = cookie.getValue();
6352
}
@@ -69,18 +58,16 @@ protected void doFilterInternal(
6958
return;
7059
}
7160

72-
log.info("필터에서 추출한 access: " + accessToken + "\n");
73-
61+
log.debug("필터에서 추출한 access: " + accessToken + "\n");
7462

7563
// 토큰 만료 확인 , 만료 시 다음 필터로 넘기지 않음(재발급 필요)
7664
try {
77-
System.out.println("만료확인");
78-
System.out.println("token type : " + jwtUtil.getTokenType(accessToken));
79-
System.out.println("userId : " + jwtUtil.getUserId(accessToken));
80-
System.out.println("role : " + jwtUtil.getRole(accessToken));
65+
log.debug("만료확인체크" + "\n");
66+
log.debug("토큰타입 : " + jwtUtil.getTokenType(accessToken) + "\n");
67+
log.debug("유저 ID : " + jwtUtil.getUserId(accessToken) + "\n");
68+
log.debug("role : " + jwtUtil.getRole(accessToken) + "\n");
8169
jwtUtil.isExpired(accessToken);
8270
} catch (ExpiredJwtException e) {
83-
e.printStackTrace();
8471
PrintWriter writer = response.getWriter();
8572
writer.print("토큰이 만료되었습니다.");
8673
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
@@ -89,7 +76,7 @@ protected void doFilterInternal(
8976

9077
// 토큰이 access인지 확인 (발급 시 페이로드에 명시)
9178
String tokenType = jwtUtil.getTokenType(accessToken);
92-
79+
9380
// 이상한 값일 경우
9481
if (!tokenType.equals("access")) {
9582
PrintWriter writer = response.getWriter();

0 commit comments

Comments
 (0)