Skip to content

Commit

Permalink
πŸ› 였λ₯˜ μˆ˜μ • - μ˜¬λ°”λ₯΄μ§€ μ•Šμ€ 토큰값 전솑 μ‹œ μ„œλ²„ λ‚΄λΆ€ μ—λŸ¬κ°€ λ°˜ν™˜λ˜λŠ” 였λ₯˜ μˆ˜μ •
Browse files Browse the repository at this point in the history
Resolves: #150
  • Loading branch information
Aleph-Kim committed Jan 6, 2025
1 parent 2e51a35 commit bfb6026
Showing 1 changed file with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package darkoverload.itzip.feature.jwt.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import darkoverload.itzip.feature.jwt.infrastructure.CustomUserDetails;
import darkoverload.itzip.feature.jwt.infrastructure.JwtAuthenticationToken;
import darkoverload.itzip.feature.jwt.util.JwtTokenizer;
import darkoverload.itzip.feature.user.entity.Authority;
import darkoverload.itzip.global.config.response.code.CommonExceptionCode;
import darkoverload.itzip.global.config.response.code.ResponseCode;
import darkoverload.itzip.global.config.response.exception.RestApiException;
import darkoverload.itzip.global.config.response.response.ExceptionResponse;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.UnsupportedJwtException;
import io.jsonwebtoken.security.SignatureException;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -23,7 +27,6 @@
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

Expand All @@ -34,6 +37,7 @@
@RequiredArgsConstructor
public class TokenAuthenticationFilter extends OncePerRequestFilter {
private final JwtTokenizer jwtTokenizer;
private final ObjectMapper objectMapper;

/**
* ν•„ν„° λ©”μ„œλ“œ
Expand All @@ -53,16 +57,21 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
if (StringUtils.hasText(accessToken)) {
try {
getAuthentication(accessToken); // 토큰을 μ‚¬μš©ν•˜μ—¬ 인증 μ„€μ •
} catch (ExpiredJwtException e) {
throw new RestApiException(CommonExceptionCode.JWT_UNKNOWN_ERROR);
} catch (UnsupportedJwtException e) {
throw new RestApiException(CommonExceptionCode.JWT_UNSUPPORTED_ERROR);
} catch (MalformedJwtException e) {
throw new RestApiException(CommonExceptionCode.JWT_INVALID_ERROR);
} catch (IllegalArgumentException e) {
throw new RestApiException(CommonExceptionCode.JWT_UNKNOWN_ERROR);
} catch (Exception e) {
throw new RestApiException(CommonExceptionCode.JWT_INTERNAL_ERROR);
} catch (ExpiredJwtException | IllegalArgumentException e) { // μ•Œ 수 μ—†λŠ” 토큰 였λ₯˜
setErrorResponse(response, CommonExceptionCode.JWT_UNKNOWN_ERROR);
return;
} catch (UnsupportedJwtException e) { // μ§€μ›ν•˜μ§€ μ•ŠλŠ” 토큰 였λ₯˜
setErrorResponse(response, CommonExceptionCode.JWT_UNSUPPORTED_ERROR);
return;
} catch (SignatureException | MalformedJwtException e) { // μœ νš¨ν•˜μ§€ μ•Šμ€ 토큰 였λ₯˜
setErrorResponse(response, CommonExceptionCode.JWT_INVALID_ERROR);
return;
} catch (RestApiException e) { // 이 전에 μ˜ˆμ™Έκ°€ λ°œμƒν•œ 경우
setErrorResponse(response, e.getExceptionCode());
return;
} catch (Exception e) { // μ•Œ 수 μ—†λŠ” 였λ₯˜
setErrorResponse(response, CommonExceptionCode.JWT_INTERNAL_ERROR);
return;
}
}
filterChain.doFilter(request, response); // λ‹€μŒ ν•„ν„°λ‘œ μš”μ²­μ„ 전달
Expand All @@ -79,10 +88,33 @@ private void getAuthentication(String token) {
String nickname = claims.get("nickname", String.class); // 이름을 κ°€μ Έμ˜΄
Authority authority = Authority.valueOf(claims.get("authority", String.class)); // μ‚¬μš©μž κΆŒν•œμ„ κ°€μ Έμ˜΄

Collection<? extends GrantedAuthority> authorities = Collections.singletonList(authority);
List<GrantedAuthority> authorities = Collections.singletonList(authority);

CustomUserDetails userDetails = new CustomUserDetails(email, "", nickname, (List<GrantedAuthority>) authorities);
CustomUserDetails userDetails = new CustomUserDetails(email, "", nickname, authorities);
Authentication authentication = new JwtAuthenticationToken(authorities, userDetails, null); // 인증 객체 생성
SecurityContextHolder.getContext().setAuthentication(authentication); // SecurityContextHolder에 인증 객체 μ„€μ •
}

/**
* 핉터 λ‚΄ μ˜ˆμ™Έ λ°œμƒ μ‹œ μ˜ˆμ™Έ 응닡 λ°˜ν™˜
* @param response 응닡 객체
* @param code 응닡 μ½”λ“œ
* @throws IOException
*/
private void setErrorResponse(HttpServletResponse response, ResponseCode code) throws IOException {
// 401 μΈμ¦μ‹€νŒ¨ 응닡 μ„€μ •
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setContentType("application/json;charset=UTF-8");

// 응닡 λ°”λ”” μž‘μ„±
ExceptionResponse errorResponse = ExceptionResponse.builder()
.status(code.getHttpStatus().toString())
.code(code.name())
.data(code.getData())
.build();

String jsonResponse = objectMapper.writeValueAsString(errorResponse);

response.getWriter().write(jsonResponse);
}
}

0 comments on commit bfb6026

Please sign in to comment.