11package clap .server .config .security ;
22
3+ import clap .server .adapter .inbound .security .LoginAttemptFilter ;
4+ import clap .server .adapter .inbound .security .filter .JwtAuthenticationFilter ;
5+ import clap .server .adapter .inbound .security .filter .JwtExceptionFilter ;
36import lombok .RequiredArgsConstructor ;
47import org .springframework .boot .autoconfigure .security .ConditionalOnDefaultWebSecurity ;
58import org .springframework .boot .autoconfigure .security .SecurityProperties ;
69import org .springframework .boot .autoconfigure .security .servlet .PathRequest ;
710import org .springframework .context .annotation .Bean ;
811import org .springframework .context .annotation .Configuration ;
9- import org .springframework .context .annotation .Profile ;
1012import org .springframework .core .annotation .Order ;
1113import org .springframework .http .HttpMethod ;
12- import org .springframework .security .config . Customizer ;
14+ import org .springframework .security .authentication . dao . DaoAuthenticationProvider ;
1315import org .springframework .security .config .annotation .web .AbstractRequestMatcherRegistry ;
1416import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
1517import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
1921import org .springframework .security .web .AuthenticationEntryPoint ;
2022import org .springframework .security .web .SecurityFilterChain ;
2123import org .springframework .security .web .access .AccessDeniedHandler ;
24+ import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
2225import org .springframework .web .cors .CorsConfigurationSource ;
2326
2427import static clap .server .config .security .WebSecurityUrl .*;
2528
29+
2630@ Configuration
2731@ EnableWebSecurity
2832@ ConditionalOnDefaultWebSecurity
2933@ RequiredArgsConstructor
3034public class SecurityConfig {
31- private final SecurityAdapterConfig securityAdapterConfig ;
35+ private final JwtAuthenticationFilter jwtAuthenticationFilter ;
36+ private final JwtExceptionFilter jwtExceptionFilter ;
37+ private final LoginAttemptFilter loginAttemptFilter ;
38+
39+ private final DaoAuthenticationProvider daoAuthenticationProvider ;
3240 private final CorsConfigurationSource corsConfigurationSource ;
3341 private final AccessDeniedHandler accessDeniedHandler ;
3442 private final AuthenticationEntryPoint authenticationEntryPoint ;
3543
3644 @ Bean
37- @ Profile ({"local" , "dev" })
3845 @ Order (SecurityProperties .BASIC_AUTH_ORDER )
39- public SecurityFilterChain filterChainForDev (HttpSecurity http ) throws Exception {
46+ public SecurityFilterChain defaultFilterChain (HttpSecurity http ) throws Exception {
4047 return defaultSecurity (http )
48+ .exceptionHandling (
49+ exception -> exception
50+ .accessDeniedHandler (accessDeniedHandler )
51+ .authenticationEntryPoint (authenticationEntryPoint )
52+ )
4153 .cors (cors -> cors .configurationSource (corsConfigurationSource ))
54+ .addFilterBefore (jwtAuthenticationFilter , UsernamePasswordAuthenticationFilter .class )
55+ .addFilterBefore (jwtExceptionFilter , JwtAuthenticationFilter .class )
56+ .addFilterBefore (loginAttemptFilter , JwtExceptionFilter .class )
4257 .authorizeHttpRequests (
4358 auth ->
4459 defaultAuthorizeHttpRequest (auth )
4560 .requestMatchers (SWAGGER_ENDPOINTS ).permitAll ()
61+ .requestMatchers (LOGIN_ENDPOINT ).permitAll ()
4662 .anyRequest ().authenticated ()
4763 ).build ();
4864 }
4965
50- @ Bean
51- @ Profile ({"prod" })
52- @ Order (SecurityProperties .BASIC_AUTH_ORDER )
53- public SecurityFilterChain filterChainForProd (HttpSecurity http ) throws Exception {
54- return defaultSecurity (http )
55- .cors (cors -> cors .configurationSource (corsConfigurationSource ))
56- .authorizeHttpRequests (auth -> defaultAuthorizeHttpRequest (auth ).anyRequest ().authenticated ()
57- ).build ();
58- }
59-
6066 private HttpSecurity defaultSecurity (HttpSecurity http ) throws Exception {
6167 return http
6268 .httpBasic (AbstractHttpConfigurer ::disable )
@@ -66,12 +72,8 @@ private HttpSecurity defaultSecurity(HttpSecurity http) throws Exception {
6672 )
6773 .formLogin (AbstractHttpConfigurer ::disable )
6874 .logout (AbstractHttpConfigurer ::disable )
69- .with (securityAdapterConfig , Customizer .withDefaults ())
70- .exceptionHandling (
71- exception -> exception
72- .accessDeniedHandler (accessDeniedHandler )
73- .authenticationEntryPoint (authenticationEntryPoint )
74- );
75+ .authenticationProvider (daoAuthenticationProvider )
76+ ;
7577 }
7678
7779 private AbstractRequestMatcherRegistry <AuthorizeHttpRequestsConfigurer <HttpSecurity >.AuthorizedUrl > defaultAuthorizeHttpRequest (
@@ -83,8 +85,7 @@ private AbstractRequestMatcherRegistry<AuthorizeHttpRequestsConfigurer<HttpSecur
8385 .requestMatchers (HttpMethod .GET , READ_ONLY_PUBLIC_ENDPOINTS ).permitAll ()
8486 .requestMatchers (HEALTH_CHECK_ENDPOINT ).permitAll ()
8587 .requestMatchers (REISSUANCE_ENDPOINTS ).permitAll ()
86- .requestMatchers (AUTHENTICATED_ENDPOINTS ).authenticated ()
87- .requestMatchers (ANONYMOUS_ENDPOINTS ).permitAll ();
88+ .requestMatchers (SWAGGER_ENDPOINTS ).permitAll ();
8889 }
8990
9091}
0 commit comments