1
1
package nextstep .security .filter ;
2
2
3
- import jakarta .servlet .*;
3
+ import jakarta .servlet .FilterChain ;
4
+ import jakarta .servlet .ServletException ;
4
5
import jakarta .servlet .http .HttpServletRequest ;
5
6
import jakarta .servlet .http .HttpServletResponse ;
6
7
import nextstep .security .AuthenticationException ;
7
8
import nextstep .security .authentication .*;
8
9
import nextstep .security .util .Base64Convertor ;
9
10
import org .slf4j .Logger ;
10
11
import org .slf4j .LoggerFactory ;
12
+ import org .springframework .web .filter .OncePerRequestFilter ;
11
13
12
14
import java .io .IOException ;
13
15
import java .util .List ;
14
16
15
- public class BasicAuthenticationFilter implements Filter {
17
+ public class BasicAuthenticationFilter extends OncePerRequestFilter {
16
18
17
19
private static final Logger log = LoggerFactory .getLogger (BasicAuthenticationFilter .class );
18
20
@@ -27,37 +29,39 @@ public BasicAuthenticationFilter(UserDetailsService userDetailsService) {
27
29
}
28
30
29
31
@ Override
30
- public void doFilter (
31
- ServletRequest servletRequest ,
32
- ServletResponse servletResponse ,
33
- FilterChain filterChain
34
- ) throws IOException , ServletException {
35
- HttpServletRequest httpRequest = (HttpServletRequest ) servletRequest ;
36
-
37
- if (!AUTHENTICATION_NEED_PATHS .contains (httpRequest .getRequestURI ())) {
38
- filterChain .doFilter (servletRequest , servletResponse );
39
- return ;
40
- }
41
-
42
- HttpServletResponse httpResponse = (HttpServletResponse ) servletResponse ;
32
+ protected boolean shouldNotFilter (HttpServletRequest request ) {
33
+ // '/members' 에 대한 요청이 아니면 필터링 하지 말라는 의미. 즉, '/members'에 대한 요청만 필터링
34
+ return !AUTHENTICATION_NEED_PATHS .contains (request .getRequestURI ());
35
+ }
43
36
37
+ @ Override
38
+ protected void doFilterInternal (
39
+ HttpServletRequest request ,
40
+ HttpServletResponse response ,
41
+ FilterChain filterChain
42
+ ) throws ServletException , IOException {
44
43
try {
45
- String authorization = httpRequest .getHeader ("Authorization" );
46
- String credentials = authorization .split (" " )[1 ]; // "Basic " 뒤의 문자열
47
- String decodedString = Base64Convertor .decode (credentials );
48
- String [] usernameAndPassword = decodedString .split (":" );
49
- String username = usernameAndPassword [0 ];
50
- String password = usernameAndPassword [1 ];
44
+ Authentication authenticated = attemptAuthentication (request );
45
+ request .setAttribute ("userDetails" , authenticated ); // 이후 필터에서 사용 가능하도록 인증된 사용자 정보를 저장
51
46
52
- Authentication authRequest = UsernamePasswordAuthenticationToken .unAuthenticated (username , password );
53
- Authentication authenticated = this .authenticationManager .authenticate (authRequest );
54
-
55
- httpRequest .setAttribute ("userDetails" , authenticated ); // 이후 필터에서 사용 가능하도록 인증된 사용자 정보를 저장
56
-
57
- filterChain .doFilter (servletRequest , servletResponse );
47
+ filterChain .doFilter (request , response );
58
48
} catch (AuthenticationException | RuntimeException e ) {
59
49
log .debug ("Authentication failed" , e );
60
- httpResponse .sendError (HttpServletResponse .SC_UNAUTHORIZED , "Authentication failed" );
50
+ response .sendError (HttpServletResponse .SC_UNAUTHORIZED , "Authentication failed" );
61
51
}
62
52
}
53
+
54
+ private Authentication attemptAuthentication (HttpServletRequest request ) throws AuthenticationException {
55
+ String authorization = request .getHeader ("Authorization" );
56
+ String credentials = authorization .split (" " )[1 ];
57
+ String decodedString = Base64Convertor .decode (credentials );
58
+ String [] usernameAndPassword = decodedString .split (":" );
59
+
60
+ Authentication authRequest = UsernamePasswordAuthenticationToken .unAuthenticated (
61
+ usernameAndPassword [0 ],
62
+ usernameAndPassword [1 ]
63
+ );
64
+
65
+ return this .authenticationManager .authenticate (authRequest );
66
+ }
63
67
}
0 commit comments