11package com .spring .monew .auth .config ;
22
33import lombok .RequiredArgsConstructor ;
4+ import lombok .extern .slf4j .Slf4j ;
45import org .springframework .beans .factory .annotation .Value ;
56import org .springframework .context .annotation .Bean ;
67import org .springframework .context .annotation .Configuration ;
1415import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
1516import com .spring .monew .common .filter .RequestIdFilter ;
1617
18+ @ Slf4j
1719@ RequiredArgsConstructor
1820@ Configuration
1921@ EnableWebSecurity
@@ -22,20 +24,26 @@ public class SecurityConfig {
2224 private final HeaderAuthFilter headerAuthFilter ;
2325 private final RequestIdFilter requestIdFilter ;
2426
25- @ Value ("${monitoring.prometheus.allow-ip}" )
26- private String prometheusAllowIp ; // yml 속성 주입 (기본값은 localhost)
27+ @ Value ("${monitoring.prometheus.allow-ip:127.0.0.1 }" )
28+ private String prometheusAllowIp ;
2729
2830 @ Bean
2931 public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
3032 http
31- .csrf (AbstractHttpConfigurer ::disable ) // CSRF 보안 비활성화 (개발용)
32-
33+ .csrf (AbstractHttpConfigurer ::disable )
3334 .authorizeHttpRequests (authorize -> authorize
3435 .requestMatchers ("/actuator/prometheus" )
3536 .access ((authentication , context ) -> {
3637 String remoteAddr = context .getRequest ().getRemoteAddr ();
37- boolean equals = remoteAddr .equals (prometheusAllowIp );// Prometheus IP
38- return new AuthorizationDecision (equals );
38+ boolean allowed =
39+ remoteAddr .equals ("127.0.0.1" ) ||
40+ remoteAddr .equals ("0:0:0:0:0:0:0:1" ) || // IPv6 localhost
41+ remoteAddr .equals ("localhost" ) ||
42+ remoteAddr .startsWith ("172." ) || // Docker 내부 네트워크
43+ remoteAddr .startsWith ("192.168." ) ||
44+ remoteAddr .equals (prometheusAllowIp );
45+
46+ return new AuthorizationDecision (allowed );
3947 })
4048 .requestMatchers ("/actuator/health" , "/actuator/info" ,
4149 "/actuator/loggers" ).permitAll () //Actuator 허용 (원래는 이렇게 하면 안됨)
@@ -52,7 +60,6 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
5260 return http .build ();
5361 }
5462
55-
5663 @ Bean
5764 public PasswordEncoder passwordEncoder () {
5865 return new BCryptPasswordEncoder ();
0 commit comments