From f00bab378304b08702e7ec1edc35fffa9d733dd2 Mon Sep 17 00:00:00 2001 From: TheWhiteDog9487 <60037547+TheWhiteDog9487@users.noreply.github.com> Date: Tue, 4 Nov 2025 18:42:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9Spring=20Security=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E4=BD=BF=E4=B8=8D=E9=9C=80=E8=A6=81=E9=AA=8C?= =?UTF-8?q?=E8=AF=81API=E5=AF=86=E9=92=A5=E7=9A=84=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E6=94=BE=E8=A1=8C=20=E4=B9=9F=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E4=BA=86=E8=AF=B7=E6=B1=82=E4=B8=80=E4=B8=AA=E6=9C=AA?= =?UTF-8?q?=E6=98=8E=E7=A1=AE=E4=B8=BA=E4=B8=8D=E9=9C=80=E8=A6=81=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=9A=84=E8=B7=AF=E5=BE=84=E6=97=B6=E4=BC=9A=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E8=BF=94=E5=9B=9E=E2=80=9C=E9=9C=80=E8=A6=81API?= =?UTF-8?q?=E5=AF=86=E9=92=A5=E2=80=9D=E7=9A=84=E4=BF=A1=E6=81=AF=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Security/ApiKeyAuthenticationFilter.java | 27 ++----------- .../SpringSecurityConfiguration.java | 39 +++++++++++++------ .../WebAPI/GlobalSharedBean.java | 4 -- 3 files changed, 31 insertions(+), 39 deletions(-) diff --git a/src/main/java/xyz/thewhitedog9487/WebAPI/Configuration/Security/ApiKeyAuthenticationFilter.java b/src/main/java/xyz/thewhitedog9487/WebAPI/Configuration/Security/ApiKeyAuthenticationFilter.java index ca52d54..e675bea 100644 --- a/src/main/java/xyz/thewhitedog9487/WebAPI/Configuration/Security/ApiKeyAuthenticationFilter.java +++ b/src/main/java/xyz/thewhitedog9487/WebAPI/Configuration/Security/ApiKeyAuthenticationFilter.java @@ -4,13 +4,12 @@ import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.stereotype.Component; import org.springframework.web.filter.OncePerRequestFilter; import xyz.thewhitedog9487.WebAPI.Controller.ResponseData; import xyz.thewhitedog9487.WebAPI.Data.Entity.AccessLog; @@ -24,29 +23,11 @@ import java.util.Locale; import java.util.Map; @Slf4j -@Component +@AllArgsConstructor public class ApiKeyAuthenticationFilter extends OncePerRequestFilter { - @Autowired List ApiKeyList; - @Autowired AccessLogRepository AccessLogRepository; - - @Override - protected boolean shouldNotFilter(HttpServletRequest request) { - var ServletPath = request.getServletPath(); - var PermitPrefix = List.of( - "/ip/", - "/v3/api-docs", - "/swagger-ui/" ); - var FullyMatchList = List.of( - "/", - "/swagger-ui.html" ); - for (String Prefix : PermitPrefix) { - if ( ServletPath.startsWith(Prefix) ) { - return true; } } - for (String FullyMatch : FullyMatchList) { - if ( ServletPath.equals(FullyMatch) ) { - return true; } } - return false; } + List ApiKeyList; + AccessLogRepository AccessLogRepository; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { diff --git a/src/main/java/xyz/thewhitedog9487/WebAPI/Configuration/SpringSecurityConfiguration.java b/src/main/java/xyz/thewhitedog9487/WebAPI/Configuration/SpringSecurityConfiguration.java index 649bf91..220226c 100644 --- a/src/main/java/xyz/thewhitedog9487/WebAPI/Configuration/SpringSecurityConfiguration.java +++ b/src/main/java/xyz/thewhitedog9487/WebAPI/Configuration/SpringSecurityConfiguration.java @@ -1,36 +1,51 @@ package xyz.thewhitedog9487.WebAPI.Configuration; +import jakarta.servlet.FilterChain; import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.Order; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import xyz.thewhitedog9487.WebAPI.Configuration.Security.ApiKeyAuthenticationFilter; +import xyz.thewhitedog9487.WebAPI.Data.Repository.AccessLogRepository; + +import java.util.List; @Configuration class SpringSecurityConfiguration { - @Autowired ApiKeyAuthenticationFilter ApiKeyAuthenticationFilter; + + @Autowired List ApiKeyList; + @Autowired AccessLogRepository AccessLogRepository; /** - * @see ApiKeyAuthenticationFilter#shouldNotFilter(HttpServletRequest) + * @see ApiKeyAuthenticationFilter#doFilterInternal(HttpServletRequest, HttpServletResponse, FilterChain) */ + @Order(1) @Bean - SecurityFilterChain CustomSecurityFilterChain(HttpSecurity Security) throws Exception { + SecurityFilterChain RequireAPIKey(HttpSecurity Security) throws Exception { + Security + .securityMatcher("/message/**", "/accesslog/**") + .csrf(AbstractHttpConfigurer::disable) + .authorizeHttpRequests(AuthorizationManagerRequestMatcherRegistry -> { + AuthorizationManagerRequestMatcherRegistry + .anyRequest() + .authenticated(); }) + .addFilterBefore(new ApiKeyAuthenticationFilter(ApiKeyList, AccessLogRepository), UsernamePasswordAuthenticationFilter.class); + return Security.build(); } + + @Order(2) + @Bean + SecurityFilterChain PermitAll(HttpSecurity Security) throws Exception { Security .csrf(AbstractHttpConfigurer::disable) .authorizeHttpRequests(AuthorizationManagerRequestMatcherRegistry -> { AuthorizationManagerRequestMatcherRegistry - .requestMatchers("/ip/**") - .permitAll() - .requestMatchers("/", "/v3/api-docs/**","swagger-ui/**", "/swagger-ui.html") - .permitAll() - .requestMatchers("/message/**", "/accesslog/**") - .authenticated() .anyRequest() - .denyAll(); }) - .addFilterBefore(ApiKeyAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); + .permitAll(); }); return Security.build(); } -} +} \ No newline at end of file diff --git a/src/main/java/xyz/thewhitedog9487/WebAPI/GlobalSharedBean.java b/src/main/java/xyz/thewhitedog9487/WebAPI/GlobalSharedBean.java index d1eec4d..f420964 100644 --- a/src/main/java/xyz/thewhitedog9487/WebAPI/GlobalSharedBean.java +++ b/src/main/java/xyz/thewhitedog9487/WebAPI/GlobalSharedBean.java @@ -49,10 +49,6 @@ class GlobalSharedBean { System.exit(-1); } return null; } } - @Bean - ApiKeyAuthenticationFilter ApiKeyAuthenticationFilter(){ - return new ApiKeyAuthenticationFilter(); } - @Bean Lock SQLiteWriteLock(){ return new ReentrantLock(); }