|
2 | 2 |
|
3 | 3 | import com.WhoIsRoom.WhoIs_Server.domain.auth.exception.CustomAuthenticationException; |
4 | 4 | import com.WhoIsRoom.WhoIs_Server.domain.auth.exception.CustomJwtException; |
| 5 | +import com.WhoIsRoom.WhoIs_Server.domain.auth.handler.exception.CustomAuthenticationEntryPoint; |
5 | 6 | import com.WhoIsRoom.WhoIs_Server.domain.auth.model.UserPrincipal; |
6 | 7 | import com.WhoIsRoom.WhoIs_Server.domain.auth.service.JwtService; |
7 | 8 | import com.WhoIsRoom.WhoIs_Server.domain.auth.util.JwtUtil; |
|
14 | 15 | import lombok.RequiredArgsConstructor; |
15 | 16 | import lombok.extern.slf4j.Slf4j; |
16 | 17 | import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException; |
| 18 | +import org.springframework.security.authentication.AuthenticationManager; |
17 | 19 | import org.springframework.security.authentication.BadCredentialsException; |
18 | 20 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
19 | 21 | import org.springframework.security.core.Authentication; |
|
37 | 39 | public class JwtAuthenticationFilter extends OncePerRequestFilter { |
38 | 40 | private final JwtUtil jwtUtil; |
39 | 41 | private final JwtService jwtService; |
| 42 | + private final CustomAuthenticationEntryPoint customAuthenticationEntryPoint; |
40 | 43 |
|
41 | 44 | // 인증을 안해도 되니 토큰이 필요없는 URL들 (에러: 로그인이 필요합니다) |
42 | 45 | public final static List<String> PASS_URIS = Arrays.asList( |
43 | | - "/api/users/signup", "/api/auth/**" |
| 46 | + "/api/users/signup", "/api/auth/login", |
| 47 | + "/api/auth/email/send", "/api/auth/email/validation" |
44 | 48 | ); |
45 | 49 |
|
46 | 50 | private static final AntPathMatcher ANT = new AntPathMatcher(); |
47 | 51 |
|
48 | 52 | @Override |
49 | 53 | protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { |
50 | 54 |
|
51 | | - if(isPassUri(request.getRequestURI())) { |
52 | | - log.info("JWT Filter Passed (pass uri) : {}", request.getRequestURI()); |
53 | | - filterChain.doFilter(request, response); |
54 | | - return; |
55 | | - } |
| 55 | + try { |
56 | 56 |
|
57 | | - // 엑세스 토큰이 없으면 Authentication도 없음 -> EntryPoint (401) |
58 | | - log.info("Request URI: {}", request.getRequestURI()); // 요청 URI 로깅 |
59 | | - String accessToken = jwtUtil.extractAccessToken(request) |
60 | | - .orElseThrow(() -> new CustomAuthenticationException(ErrorCode.SECURITY_UNAUTHORIZED)); |
| 57 | + if (isPassUri(request.getRequestURI())) { |
| 58 | + log.info("JWT Filter Passed (pass uri) : {}", request.getRequestURI()); |
| 59 | + filterChain.doFilter(request, response); |
| 60 | + return; |
| 61 | + } |
61 | 62 |
|
62 | | - // 토큰 유효성 검사 |
63 | | - jwtUtil.validateToken(accessToken); |
| 63 | + // 엑세스 토큰이 없으면 Authentication도 없음 -> EntryPoint (401) |
| 64 | + log.info("Request URI: {}", request.getRequestURI()); // 요청 URI 로깅 |
| 65 | + String accessToken = jwtUtil.extractAccessToken(request) |
| 66 | + .orElseThrow(() -> new CustomAuthenticationException(ErrorCode.SECURITY_UNAUTHORIZED)); |
64 | 67 |
|
65 | | - // 토큰 타입 검사 |
66 | | - if(!"access".equals(jwtUtil.getTokenType(accessToken))) { |
67 | | - throw new CustomJwtException(ErrorCode.INVALID_TOKEN_TYPE); |
68 | | - } |
| 68 | + // 토큰 유효성 검사 |
| 69 | + jwtUtil.validateToken(accessToken); |
69 | 70 |
|
70 | | - // 로그아웃 체크 |
71 | | - jwtService.checkLogout(accessToken); |
| 71 | + // 토큰 타입 검사 |
| 72 | + if (!"access".equals(jwtUtil.getTokenType(accessToken))) { |
| 73 | + throw new CustomJwtException(ErrorCode.INVALID_TOKEN_TYPE); |
| 74 | + } |
72 | 75 |
|
73 | | - // 권한 리스트 생성 |
74 | | - List<GrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority(jwtUtil.getRole(accessToken))); |
75 | | - log.info("Granted Authorities : {}", authorities); |
76 | | - UserPrincipal principal = new UserPrincipal( |
77 | | - jwtUtil.getUserId(accessToken), |
78 | | - jwtUtil.getName(accessToken), |
79 | | - null, // 패스워드는 필요 없음 |
80 | | - jwtUtil.getProviderId(accessToken), |
81 | | - authorities |
82 | | - ); |
83 | | - log.info("UserPrincipal.userId: {}", principal.getUserId()); |
84 | | - log.info("UserPrincipal.nickName: {}", principal.getUsername()); |
85 | | - log.info("UserPrincipal.providerId: {}", principal.getProviderId()); |
86 | | - log.info("UserPrincipal.role: {}", principal.getAuthorities().stream().findFirst().get().toString()); |
| 76 | + // 로그아웃 체크 |
| 77 | + jwtService.checkLogout(accessToken); |
87 | 78 |
|
88 | | - Authentication authToken = null; |
89 | | - if ("localhost".equals(principal.getProviderId())) { |
90 | | - // 폼 로그인(자체 회원) |
91 | | - authToken = new UsernamePasswordAuthenticationToken(principal, null, authorities); |
92 | | - } |
| 79 | + // 권한 리스트 생성 |
| 80 | + List<GrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority(jwtUtil.getRole(accessToken))); |
| 81 | + log.info("Granted Authorities : {}", authorities); |
| 82 | + UserPrincipal principal = new UserPrincipal( |
| 83 | + jwtUtil.getUserId(accessToken), |
| 84 | + jwtUtil.getName(accessToken), |
| 85 | + null, // 패스워드는 필요 없음 |
| 86 | + jwtUtil.getProviderId(accessToken), |
| 87 | + authorities |
| 88 | + ); |
| 89 | + log.info("UserPrincipal.userId: {}", principal.getUserId()); |
| 90 | + log.info("UserPrincipal.nickName: {}", principal.getUsername()); |
| 91 | + log.info("UserPrincipal.providerId: {}", principal.getProviderId()); |
| 92 | + log.info("UserPrincipal.role: {}", principal.getAuthorities().stream().findFirst().get().toString()); |
| 93 | + |
| 94 | + Authentication authToken = null; |
| 95 | + if ("localhost".equals(principal.getProviderId())) { |
| 96 | + // 폼 로그인(자체 회원) |
| 97 | + authToken = new UsernamePasswordAuthenticationToken(principal, null, authorities); |
| 98 | + } |
93 | 99 | // else { |
94 | 100 | // // 소셜 로그인 |
95 | 101 | // authToken = new OAuth2AuthenticationToken(principal, authorities, loginProvider); |
96 | 102 | // } |
97 | | - log.info("Authentication set in SecurityContext: {}", SecurityContextHolder.getContext().getAuthentication()); |
98 | | - log.info("Authorities in SecurityContext: {}", authToken.getAuthorities()); |
| 103 | + log.info("Authentication set in SecurityContext: {}", SecurityContextHolder.getContext().getAuthentication()); |
| 104 | + log.info("Authorities in SecurityContext: {}", authToken.getAuthorities()); |
99 | 105 |
|
100 | | - log.info("JWT Filter Success : {}", request.getRequestURI()); |
101 | | - SecurityContextHolder.getContext().setAuthentication(authToken); |
102 | | - filterChain.doFilter(request, response); |
| 106 | + log.info("JWT Filter Success : {}", request.getRequestURI()); |
| 107 | + SecurityContextHolder.getContext().setAuthentication(authToken); |
| 108 | + filterChain.doFilter(request, response); |
| 109 | + } catch (CustomAuthenticationException | AuthenticationException e) { |
| 110 | + customAuthenticationEntryPoint.commence(request, response, (org.springframework.security.core.AuthenticationException) e); |
| 111 | + } |
103 | 112 | } |
104 | | - |
105 | 113 | private boolean isPassUri(String uri) { |
106 | 114 | return PASS_URIS.stream().anyMatch(pattern -> ANT.match(pattern, uri)); |
107 | 115 | } |
|
0 commit comments