package com.lqkj.cmlcp.config; import com.lqkj.cmlcp.config.auth.LoginAuthenticationProvider; import com.lqkj.cmlcp.filter.JwtAuthFilter; import com.lqkj.cmlcp.module.authority.service.impl.DatabaseUserDetailService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.dao.DaoAuthenticationProvider; import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; 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.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; @Configuration @EnableWebSecurity public class WebSecurityConfig { private final JwtAuthFilter authFilter; private final DatabaseUserDetailService userDetailService; public WebSecurityConfig(JwtAuthFilter authFilter, DatabaseUserDetailService userDetailService) { this.authFilter = authFilter; this.userDetailService = userDetailService; } @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { return http .csrf(AbstractHttpConfigurer::disable) .authorizeHttpRequests((requests) -> requests .requestMatchers( "/jwt/token", "/jwt/getAdminToken", "/encrypt/**", "/swagger-ui.html", "/swagger-ui/**", "/v3/api-docs/**", "/geom/all", "/test/**", "/upload/**") .permitAll() .requestMatchers("/**") .authenticated()) .sessionManagement((session) -> session .sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .authenticationProvider(authenticationProvider()) .addFilterBefore(authFilter, UsernamePasswordAuthenticationFilter.class) .build(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Bean public AuthenticationProvider authenticationProvider(){ DaoAuthenticationProvider authenticationProvider=new LoginAuthenticationProvider(userDetailService, passwordEncoder()); authenticationProvider.setUserDetailsService(userDetailService); authenticationProvider.setPasswordEncoder(passwordEncoder()); return authenticationProvider; } @Bean public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception { return config.getAuthenticationManager(); } }