SecurityConfiguration.java
Bridget/payment_solutions/src/main/java/com/checkout/payment/gateway/configuration/SecurityConfiguration.java
package com.checkout.payment.gateway.configuration;
import com.checkout.payment.gateway.security.ApiKeyAuthenticationFilter;
import com.checkout.payment.gateway.security.RateLimitingFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@Configuration
@EnableWebSecurity
public class SecurityConfiguration {
@Bean
public RateLimitingFilter rateLimitingFilter(
@Value("${gateway.ratelimit.enabled:true}") boolean rateLimitEnabled,
@Value("${gateway.security.enabled:true}") boolean securityEnabled,
@Value("${gateway.ratelimit.capacity-per-window:120}") int capacity,
@Value("${gateway.ratelimit.window:1m}") String window) {
return new RateLimitingFilter(rateLimitEnabled, securityEnabled, capacity, window);
}
@Bean
public SecurityFilterChain securityFilterChain(
HttpSecurity http,
ApiKeyAuthenticationFilter apiKeyAuthenticationFilter,
RateLimitingFilter rateLimitingFilter,
@Value("${gateway.security.enabled:true}") boolean securityEnabled) throws Exception {
http.csrf(AbstractHttpConfigurer::disable);
http.sessionManagement(s -> s.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
if (!securityEnabled) {
http.authorizeHttpRequests(auth -> auth.anyRequest().permitAll());
return http.build();
}
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/actuator/health", "/actuator/health/**").permitAll()
.requestMatchers("/actuator/prometheus").hasRole("ADMIN")
.requestMatchers("/swagger-ui/**", "/swagger-ui.html", "/v3/api-docs/**").permitAll()
.requestMatchers("/api/**").authenticated()
.anyRequest().denyAll());
http.addFilterBefore(apiKeyAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
http.addFilterAfter(rateLimitingFilter, ApiKeyAuthenticationFilter.class);
return http.build();
}
}
Artigos relacionados
PaymentGatewayApplication.java
PaymentGatewayApplication.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/PaymentGatewayApplication.java).
Ler artigo →ApplicationConfiguration.java
ApplicationConfiguration.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/configuration/ApplicationConfiguration.java).
Ler artigo →RequestResponseLogger.java
RequestResponseLogger.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/configuration/RequestResponseLogger.java).
Ler artigo →PaymentGatewayController.java
PaymentGatewayController.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/controller/PaymentGatewayController.java).
Ler artigo →PaymentStatus.java
PaymentStatus.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/enums/PaymentStatus.java).
Ler artigo →BankErrorException.java
BankErrorException.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/exception/BankErrorException.java).
Ler artigo →