2026-04-28 09:45:20 +08:00
|
|
|
package com.xuqm.push.config;
|
|
|
|
|
|
|
|
|
|
import com.xuqm.common.security.JwtAuthFilter;
|
|
|
|
|
import com.xuqm.common.security.JwtUtil;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
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-28 09:45:20 +08:00
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
|
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
@EnableWebSecurity
|
|
|
|
|
public class SecurityConfig {
|
|
|
|
|
|
|
|
|
|
private final JwtUtil jwtUtil;
|
|
|
|
|
|
|
|
|
|
public SecurityConfig(JwtUtil jwtUtil) {
|
|
|
|
|
this.jwtUtil = jwtUtil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
|
|
|
|
http
|
|
|
|
|
.csrf(AbstractHttpConfigurer::disable)
|
|
|
|
|
.sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
|
|
|
|
.authorizeHttpRequests(auth -> auth
|
2026-05-14 23:40:35 +08:00
|
|
|
.requestMatchers("/api/push/internal/**", "/api/push/auth/**", "/actuator/health", "/actuator/info").permitAll()
|
2026-04-28 09:45:20 +08:00
|
|
|
.anyRequest().authenticated()
|
|
|
|
|
)
|
2026-05-18 13:31:24 +08:00
|
|
|
.exceptionHandling(ex -> ex
|
|
|
|
|
.authenticationEntryPoint((req, res, e) -> res.sendError(HttpServletResponse.SC_UNAUTHORIZED))
|
|
|
|
|
)
|
2026-04-28 09:45:20 +08:00
|
|
|
.addFilterBefore(new JwtAuthFilter(jwtUtil), UsernamePasswordAuthenticationFilter.class);
|
|
|
|
|
return http.build();
|
|
|
|
|
}
|
|
|
|
|
}
|