package com.yoyuzh.config; import com.fasterxml.jackson.databind.ObjectMapper; import com.yoyuzh.admin.ApiRequestMetricsFilter; import com.yoyuzh.auth.CustomUserDetailsService; import com.yoyuzh.common.ApiResponse; import com.yoyuzh.common.ErrorCode; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.HttpMethod; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.dao.DaoAuthenticationProvider; import org.springframework.security.config.Customizer; 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.configuration.EnableWebSecurity; 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; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import java.util.List; @Configuration @EnableWebSecurity @EnableMethodSecurity @RequiredArgsConstructor public class SecurityConfig { private final JwtAuthenticationFilter jwtAuthenticationFilter; private final ApiRequestMetricsFilter apiRequestMetricsFilter; private final CustomUserDetailsService userDetailsService; private final ObjectMapper objectMapper; private final CorsProperties corsProperties; @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .csrf(csrf -> csrf.disable()) .cors(Customizer.withDefaults()) .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .authorizeHttpRequests(auth -> auth .requestMatchers("/api/auth/**", "/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html") .permitAll() .requestMatchers("/api/app/android/latest", "/api/app/android/download", "/api/app/android/download/*") .permitAll() .requestMatchers(HttpMethod.GET, "/api/v2/site/ping") .permitAll() .requestMatchers("/api/v2/files/**") .authenticated() .requestMatchers("/api/transfer/**") .permitAll() .requestMatchers(HttpMethod.GET, "/api/files/share-links/*") .permitAll() .requestMatchers("/api/admin/**") .authenticated() .requestMatchers("/api/files/**", "/api/user/**") .authenticated() .anyRequest() .permitAll()) .authenticationProvider(authenticationProvider()) .exceptionHandling(ex -> ex .authenticationEntryPoint((request, response, e) -> { response.setStatus(401); response.setContentType(MediaType.APPLICATION_JSON_VALUE); objectMapper.writeValue(response.getWriter(), ApiResponse.error(ErrorCode.NOT_LOGGED_IN, "用户未登录")); }) .accessDeniedHandler((request, response, e) -> { response.setStatus(403); response.setContentType(MediaType.APPLICATION_JSON_VALUE); objectMapper.writeValue(response.getWriter(), ApiResponse.error(ErrorCode.PERMISSION_DENIED, "权限不足")); })) .addFilterBefore(apiRequestMetricsFilter, UsernamePasswordAuthenticationFilter.class) .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); return http.build(); } @Bean public AuthenticationProvider authenticationProvider() { DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); provider.setUserDetailsService(userDetailsService); provider.setPasswordEncoder(passwordEncoder()); return provider; } @Bean public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception { return configuration.getAuthenticationManager(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Bean public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); configuration.setAllowedOrigins(corsProperties.getAllowedOrigins()); configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")); configuration.setAllowedHeaders(List.of("*")); configuration.setAllowCredentials(false); configuration.setMaxAge(3600L); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); return source; } }