1
1
package page .clab .api .global .auth .filter ;
2
2
3
+ import io .ipinfo .api .IPinfo ;
4
+ import io .ipinfo .api .errors .RateLimitedException ;
3
5
import io .ipinfo .api .model .IPResponse ;
6
+ import io .ipinfo .spring .strategies .attribute .AttributeStrategy ;
7
+ import io .ipinfo .spring .strategies .interceptor .InterceptorStrategy ;
4
8
import jakarta .servlet .Filter ;
5
9
import jakarta .servlet .FilterChain ;
6
10
import jakarta .servlet .FilterConfig ;
10
14
import jakarta .servlet .http .HttpServletRequest ;
11
15
import lombok .extern .slf4j .Slf4j ;
12
16
import org .springframework .stereotype .Component ;
17
+ import page .clab .api .global .config .IPInfoConfig ;
13
18
import page .clab .api .global .util .HttpReqResUtil ;
14
- import page .clab .api .global .util .IpInfoUtil ;
15
19
16
20
import java .io .IOException ;
17
21
18
22
@ Component
19
23
@ Slf4j
20
24
public class IpAuthenticationFilter implements Filter {
21
25
26
+ private final IPinfo ipInfo ;
27
+
28
+ private final AttributeStrategy attributeStrategy ;
29
+
30
+ private final InterceptorStrategy interceptorStrategy ;
31
+
32
+ public IpAuthenticationFilter (IPInfoConfig ipInfoConfig ) {
33
+ ipInfo = ipInfoConfig .ipInfo ();
34
+ attributeStrategy = ipInfoConfig .attributeStrategy ();
35
+ interceptorStrategy = ipInfoConfig .interceptorStrategy ();
36
+ }
37
+
22
38
@ Override
23
39
public void doFilter (ServletRequest request , ServletResponse response , FilterChain chain ) throws IOException , ServletException {
24
- String ipAddress = HttpReqResUtil .getClientIpAddressIfServletRequestExist ();
25
- IPResponse ipResponse = IpInfoUtil .getIpInfo ((HttpServletRequest ) request );
26
- String country = ipResponse == null ? null : ipResponse .getCountryCode ();
27
- if (country != null && !country .equals ("KR" )) {
28
- log .info ("[{}:{}] 허용되지 않은 국가로부터의 접근입니다." , ipAddress , country );
29
- return ;
40
+ HttpServletRequest httpRequest = (HttpServletRequest ) request ;
41
+ String clientIpAddress = HttpReqResUtil .getClientIpAddressIfServletRequestExist ();
42
+ try {
43
+ if (shouldProcessRequest (httpRequest , clientIpAddress )) {
44
+ chain .doFilter (request , response );
45
+ return ;
46
+ }
47
+ IPResponse ipResponse = storeIpInformation (clientIpAddress , httpRequest );
48
+ if (isNonPermittedCountry (ipResponse )) {
49
+ log .warn ("Access from non-permitted country: {}" , ipResponse .getCountryCode ());
50
+ return ;
51
+ }
52
+ } catch (RateLimitedException e ) {
53
+ log .error ("Rate limit exceeded while getting IP information." );
54
+ } catch (Exception e ) {
55
+ log .error ("Failed to get IP information." );
30
56
}
31
57
chain .doFilter (request , response );
32
58
}
33
59
60
+ private boolean shouldProcessRequest (HttpServletRequest httpRequest , String clientIpAddress ) {
61
+ return !interceptorStrategy .shouldRun (httpRequest )
62
+ || attributeStrategy .hasAttribute (httpRequest )
63
+ || HttpReqResUtil .isLocalRequest (clientIpAddress )
64
+ || clientIpAddress .equals ("0.0.0.0" );
65
+ }
66
+
67
+ private IPResponse storeIpInformation (String clientIpAddress , HttpServletRequest httpRequest ) throws RateLimitedException {
68
+ IPResponse ipResponse = ipInfo .lookupIP (clientIpAddress );
69
+ attributeStrategy .storeAttribute (httpRequest , ipResponse );
70
+ return ipResponse ;
71
+ }
72
+
73
+ private boolean isNonPermittedCountry (IPResponse ipResponse ) {
74
+ String country = ipResponse .getCountryCode ();
75
+ return country != null && !country .equals ("KR" );
76
+ }
77
+
34
78
@ Override
35
79
public void init (FilterConfig filterConfig ) {
36
- log .info ("IP Authentication Filter Init. ." );
80
+ log .info ("IP Authentication Filter initialized ." );
37
81
}
38
82
39
83
@ Override
40
84
public void destroy () {
41
- log .info ("IP Authentication Filter Destroy. ." );
85
+ log .info ("IP Authentication Filter destroyed ." );
42
86
}
43
87
44
88
}
0 commit comments