content: package com.alkl1m.taskmanager.configuration;
  
  import com.alkl1m.taskmanager.service.auth.UserDetailsServiceImpl;
  import com.alkl1m.taskmanager.util.AuthEntryPointJwt;
  import com.alkl1m.taskmanager.util.AuthTokenFilter;
  import lombok.AllArgsConstructor;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
  import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
  import org.springframework.security.config.http.SessionCreationPolicy;
  import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  import org.springframework.security.crypto.password.PasswordEncoder;
  import org.springframework.security.web.SecurityFilterChain;
  import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  
  @Configuration
  @EnableMethodSecurity
  @AllArgsConstructor
  public class SecurityConfig {
      UserDetailsServiceImpl userDetailsService;
      private AuthEntryPointJwt unauthorizedHandler;
  
      @Bean
      public AuthTokenFilter authenticationJwtTokenFilter() {
          return new AuthTokenFilter();
      }
  
      @Bean
      public DaoAuthenticationProvider authenticationProvider() {
          DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
  
          authenticationProvider.setUserDetailsService(userDetailsService);
          authenticationProvider.setPasswordEncoder(passwordEncoder());
  
          return authenticationProvider;
      }
  
      @Bean
      public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
          return authenticationConfiguration.getAuthenticationManager();
      }
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder();
      }
  
      @Bean
      public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
          http
                  .csrf(AbstractHttpConfigurer::disable)
                  .exceptionHandling(exception -> exception.authenticationEntryPoint(unauthorizedHandler))
                  .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                  .authorizeHttpRequests(auth ->
                          auth.requestMatchers("/api/auth/**").permitAll()
                                  .requestMatchers("/v3/api-docs/**").permitAll()
                                  .requestMatchers("/swagger-ui/**").permitAll()
                                  .requestMatchers("/api/user/**").permitAll()
                                  .anyRequest().authenticated()
                          );
          http.authenticationProvider(authenticationProvider());
          http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
  
          return http.build();
      }
  }
  