Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import static com.project.common.util.WebSecurityUrl.getHealthCheckEndpoints;
import static com.project.common.util.WebSecurityUrl.getReadOnlyPublicEndpoints;
import static com.project.common.util.WebSecurityUrl.getActuatorEndpoints;
import static com.project.common.util.WebSecurityUrl.getAnonymousEndpoints;

@Slf4j
Expand All @@ -44,6 +45,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {

protected static final String[] PUBLIC_ENDPOINTS = Stream.of(
getHealthCheckEndpoints(),
getActuatorEndpoints(),
getReadOnlyPublicEndpoints()
).flatMap(Arrays::stream).toArray(String[]::new);

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/project/common/util/WebSecurityUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ private WebSecurityUrl() {
public static final String LOCAL_LOGIN_ENDPOINT = "/oauth/local/login";
public static final String LOCAL_SIGN_ENDPOINT = "/oauth/local/sign";
private static final String[] HEALTH_CHECK_ENDPOINTS = {"/health", "/actuator/health"};
private static final String[] ACTUATOR_ENDPOINTS = {"/actuator/prometheus", "/actuator/metrics", "/actuator/info"};
private static final String[] READ_ONLY_PUBLIC_ENDPOINTS = {"/favicon.ico"};
private static final String[] ANONYMOUS_ENDPOINTS = {
LOGIN_ENDPOINT, REISSUE_ENDPOINT, LOCAL_LOGIN_ENDPOINT, LOCAL_SIGN_ENDPOINT};
Expand All @@ -19,6 +20,10 @@ public static String[] getHealthCheckEndpoints() {
return HEALTH_CHECK_ENDPOINTS.clone();
}

public static String[] getActuatorEndpoints() {
return ACTUATOR_ENDPOINTS.clone();
}

public static String[] getReadOnlyPublicEndpoints() {
return READ_ONLY_PUBLIC_ENDPOINTS.clone();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.project.config.security;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.authorization.AuthorizationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.access.intercept.RequestAuthorizationContext;
import org.springframework.stereotype.Component;

import java.util.function.Supplier;

@Component
public class ActuatorAuthorizationManager implements AuthorizationManager<RequestAuthorizationContext> {

private final String monitoringHost;

public ActuatorAuthorizationManager(@Value("${LOKI_URI:localhost}") String monitoringHost) {
this.monitoringHost = monitoringHost;
}

@Override
public AuthorizationDecision check(Supplier<Authentication> authenticationSupplier, RequestAuthorizationContext context) {
HttpServletRequest request = context.getRequest();
String remoteAddr = request.getRemoteAddr();

boolean isAllowed = isLocalhost(remoteAddr) || monitoringHost.equals(remoteAddr);

return new AuthorizationDecision(isAllowed);
}

private boolean isLocalhost(String ip) {
return "127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip) || "localhost".equals(ip);
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/project/config/security/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.project.common.util.WebSecurityUrl.getReadOnlyPublicEndpoints;
import static com.project.common.util.WebSecurityUrl.getHealthCheckEndpoints;
import static com.project.common.util.WebSecurityUrl.getActuatorEndpoints;
import static com.project.common.util.WebSecurityUrl.LOGIN_ENDPOINT;
import static com.project.common.util.WebSecurityUrl.REISSUE_ENDPOINT;
import static com.project.common.util.WebSecurityUrl.LOCAL_LOGIN_ENDPOINT;
Expand All @@ -33,6 +34,7 @@ public class SecurityConfig {
private final JwtAuthenticationFilter jwtAuthenticationFilter;
private final AccessDeniedHandler accessDeniedHandler;
private final AuthenticationEntryPoint authenticationEntryPoint;
private final ActuatorAuthorizationManager actuatorAuthorizationManager;

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
Expand All @@ -55,6 +57,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.requestMatchers("/slack/**").permitAll()
.requestMatchers(HttpMethod.GET, getReadOnlyPublicEndpoints()).permitAll()
.requestMatchers(getHealthCheckEndpoints()).permitAll()
.requestMatchers(getActuatorEndpoints()).access(actuatorAuthorizationManager)
.requestMatchers(LOCAL_LOGIN_ENDPOINT).permitAll()
.requestMatchers(LOCAL_SIGN_ENDPOINT).permitAll()
.requestMatchers(LOGIN_ENDPOINT).permitAll()
Expand Down
Loading