2026-04-24 10:42:11 +08:00
|
|
|
package com.xuqm.update.config;
|
|
|
|
|
|
2026-04-29 16:07:22 +08:00
|
|
|
import com.xuqm.common.security.JwtAuthFilter;
|
|
|
|
|
import com.xuqm.common.security.JwtUtil;
|
2026-04-24 10:42:11 +08:00
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
2026-04-29 12:33:25 +08:00
|
|
|
import org.springframework.http.HttpMethod;
|
2026-04-24 10:42:11 +08:00
|
|
|
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;
|
2026-05-18 13:31:24 +08:00
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
2026-04-24 10:42:11 +08:00
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
2026-04-29 16:07:22 +08:00
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
2026-04-29 12:33:25 +08:00
|
|
|
import org.springframework.web.cors.CorsConfiguration;
|
|
|
|
|
import org.springframework.web.cors.CorsConfigurationSource;
|
|
|
|
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
2026-04-24 10:42:11 +08:00
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
@EnableWebSecurity
|
|
|
|
|
public class SecurityConfig {
|
|
|
|
|
|
2026-04-29 16:07:22 +08:00
|
|
|
private final JwtUtil jwtUtil;
|
|
|
|
|
|
|
|
|
|
public SecurityConfig(JwtUtil jwtUtil) {
|
|
|
|
|
this.jwtUtil = jwtUtil;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 10:42:11 +08:00
|
|
|
@Bean
|
|
|
|
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
|
|
|
|
http
|
|
|
|
|
.csrf(AbstractHttpConfigurer::disable)
|
2026-04-29 12:33:25 +08:00
|
|
|
.cors(cors -> {})
|
2026-04-24 10:42:11 +08:00
|
|
|
.sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
|
|
|
|
.authorizeHttpRequests(auth -> auth
|
2026-04-29 12:33:25 +08:00
|
|
|
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
|
2026-04-24 10:42:11 +08:00
|
|
|
.requestMatchers(
|
|
|
|
|
"/actuator/**",
|
|
|
|
|
"/api/v1/updates/app/check",
|
2026-04-29 17:35:52 +08:00
|
|
|
"/api/v1/updates/app/inspect",
|
2026-04-24 10:42:11 +08:00
|
|
|
"/api/v1/rn/update/check",
|
2026-04-29 17:35:52 +08:00
|
|
|
"/api/v1/rn/inspect",
|
2026-04-24 10:42:11 +08:00
|
|
|
"/api/v1/rn/files/**",
|
|
|
|
|
"/files/apk/**"
|
|
|
|
|
).permitAll()
|
|
|
|
|
.anyRequest().authenticated()
|
|
|
|
|
)
|
2026-05-18 13:31:24 +08:00
|
|
|
.exceptionHandling(ex -> ex
|
|
|
|
|
.authenticationEntryPoint((req, res, e) -> res.sendError(HttpServletResponse.SC_UNAUTHORIZED))
|
|
|
|
|
)
|
2026-04-29 16:07:22 +08:00
|
|
|
.addFilterBefore(new JwtAuthFilter(jwtUtil), UsernamePasswordAuthenticationFilter.class)
|
2026-04-24 10:42:11 +08:00
|
|
|
.httpBasic(AbstractHttpConfigurer::disable)
|
|
|
|
|
.formLogin(AbstractHttpConfigurer::disable);
|
|
|
|
|
return http.build();
|
|
|
|
|
}
|
2026-04-29 12:33:25 +08:00
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
public CorsConfigurationSource corsConfigurationSource() {
|
|
|
|
|
CorsConfiguration config = new CorsConfiguration();
|
|
|
|
|
config.setAllowCredentials(true);
|
|
|
|
|
config.setAllowedOriginPatterns(List.of(
|
|
|
|
|
"http://localhost:*",
|
|
|
|
|
"http://127.0.0.1:*",
|
|
|
|
|
"http://*.xuqinmin.com",
|
|
|
|
|
"https://*.xuqinmin.com"
|
|
|
|
|
));
|
|
|
|
|
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
|
|
|
|
|
config.setAllowedHeaders(List.of("*"));
|
|
|
|
|
config.setExposedHeaders(List.of("Location"));
|
|
|
|
|
config.setMaxAge(3600L);
|
|
|
|
|
|
|
|
|
|
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
|
|
|
|
source.registerCorsConfiguration("/**", config);
|
|
|
|
|
return source;
|
|
|
|
|
}
|
2026-04-24 10:42:11 +08:00
|
|
|
}
|