Skip to content

Commit

Permalink
Merge branch 'master' into stage
Browse files Browse the repository at this point in the history
  • Loading branch information
GodCipher committed May 29, 2024
2 parents b7ac7d0 + b111241 commit 51882af
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;

@Order(Ordered.HIGHEST_PRECEDENCE)
@Slf4j
public class ApiKeyAuthFilter extends AbstractAuthenticationProcessingFilter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
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;
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter;
import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint;

@Configuration
@EnableWebSecurity
Expand All @@ -38,11 +39,14 @@ public ApiKeyAuthFilter apiKeyAuthFilter(AuthenticationManager authenticationMan
public SecurityFilterChain securityFilterChain(
HttpSecurity http, AuthenticationManager authenticationManager) throws Exception {
http.csrf(AbstractHttpConfigurer::disable)
.addFilterBefore(
apiKeyAuthFilter(authenticationManager), UsernamePasswordAuthenticationFilter.class)
.addFilterAfter(
apiKeyAuthFilter(authenticationManager), AnonymousAuthenticationFilter.class)
.authorizeHttpRequests(authorize -> authorize.anyRequest().authenticated())
.sessionManagement(
session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.anonymous(AbstractHttpConfigurer::disable)
.exceptionHandling(
exception -> exception.authenticationEntryPoint(new Http403ForbiddenEntryPoint()));

log.debug("API key: {}", apiKey);
log.debug("API key header: {}", apiKeyHeader);
Expand Down

0 comments on commit 51882af

Please sign in to comment.