This commit is contained in:
Wowieee4
2026-04-23 11:38:29 +07:00
parent ff76abbbf6
commit f8ca77eeb9
12 changed files with 1240 additions and 457 deletions
@@ -9,6 +9,8 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import com.walkguide.security.JwtAuthFilter;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@@ -20,27 +22,28 @@ public class SecurityConfig {
}
// Aturan jalan masuk ke API kita:
// Jangan lupa inject filter-nya di atas (tambahin parameter JwtAuthFilter jwtAuthFilter)
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
public SecurityFilterChain filterChain(HttpSecurity http, JwtAuthFilter jwtAuthFilter) throws Exception {
http
.cors(cors -> cors.configurationSource(request -> {
var corsConfiguration = new org.springframework.web.cors.CorsConfiguration();
// Izinkan semua origin localhost dan semua port (biar Chrome gak ngeblok)
corsConfiguration.setAllowedOriginPatterns(java.util.List.of(
"http://localhost:*",
"http://127.0.0.1:*"
));
corsConfiguration.setAllowedMethods(java.util.List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
corsConfiguration.setAllowedHeaders(java.util.List.of("*"));
corsConfiguration.setAllowCredentials(true);
return corsConfiguration;
var corsConfig = new org.springframework.web.cors.CorsConfiguration();
corsConfig.setAllowedOriginPatterns(java.util.List.of("http://localhost:*", "http://127.0.0.1:*"));
corsConfig.setAllowedMethods(java.util.List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
corsConfig.setAllowedHeaders(java.util.List.of("*"));
corsConfig.setAllowCredentials(true);
return corsConfig;
}))
.csrf(csrf -> csrf.disable())
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth -> auth
.requestMatchers("/api/auth/**").permitAll()
.requestMatchers("/api/auth/**", "/swagger-ui/**", "/v3/api-docs/**").permitAll() // Login & Swagger bebas
.requestMatchers("/api/guardian/**").hasRole("GUARDIAN") // Khusus Guardian
.requestMatchers("/api/user/**").hasRole("USER") // Khusus Tunanetra
.anyRequest().authenticated()
);
)
// TARUH SATPAM DI SINI
.addFilterBefore(jwtAuthFilter, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.class);
return http.build();
}