-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityConfig.java
More file actions
213 lines (190 loc) · 12 KB
/
Copy pathSecurityConfig.java
File metadata and controls
213 lines (190 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package com.crimeLink.analyzer.config;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
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.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.core.userdetails.UserDetailsService;
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.HttpStatusEntryPoint;
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;
@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfig {
@Autowired
private JwtAuthenticationFilter jwtAuthFilter;
@Autowired
private UserDetailsService userDetailsService;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
// CRITICAL FIX: Enable CORS using the bean configuration
.cors(cors -> cors.configurationSource(corsConfigurationSource()))
.authorizeHttpRequests(auth -> auth
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.requestMatchers("/api/auth/**").permitAll()
.requestMatchers("/api/health").permitAll()
.requestMatchers("/api/admin/health").permitAll()
.requestMatchers("/api/facial/health").permitAll() // ML service health check
.requestMatchers("/api/call-analysis/health").permitAll() // ML service health check
// ML Service endpoints
.requestMatchers("/api/call-analysis/**").hasRole("Investigator")
.requestMatchers("/api/facial/register").hasAnyRole("Investigator", "OIC")
.requestMatchers("/api/facial/criminals").hasAnyRole("Investigator", "OIC")
.requestMatchers("/api/facial/**").hasRole("Investigator")
// Criminal CRUD (direct DB, no Python)
.requestMatchers("/api/criminals/**").hasAnyRole("Investigator", "OIC")
.requestMatchers("/api/criminals").hasAnyRole("Investigator", "OIC")
.requestMatchers("/api/database/**").permitAll()
.requestMatchers("/api/test").permitAll()
.requestMatchers("/api/debug/**").permitAll() // 🔍 Debug endpoints
.requestMatchers("/error").permitAll() // Allow error page without auth
// Public endpoints
.requestMatchers("/api/vehicles/**").permitAll()
.requestMatchers("/api/mobile/auth/**").permitAll()
.requestMatchers("/api/duties/**").permitAll()
.requestMatchers("/api/crime-reports/map").permitAll()
.requestMatchers(HttpMethod.GET, "/api/crime-reports").permitAll()
.requestMatchers("/api/crime-reports/upload-evidence").authenticated()
.requestMatchers("/api/crime-reports/**").hasAnyRole("OIC", "Admin")
// Field Officer routes
.requestMatchers("/api/officers/me/**").hasRole("FieldOfficer")
.requestMatchers("/api/mobile/**").hasRole("FieldOfficer")
.requestMatchers("/api/leaves/**").permitAll()
// OIC-only routes
.requestMatchers("/api/duty-schedules/**").hasRole("OIC")
.requestMatchers("/api/weapon/**").hasRole("OIC")
.requestMatchers("/api/weapon-issue/**").hasRole("OIC")
// Admin/OIC/Investigator routes (officer data, locations, users)
.requestMatchers("/api/users/field-officers").hasAnyRole("Admin", "OIC", "Investigator")
.requestMatchers("/api/admin/officers/*/locations/**").hasAnyRole("Admin", "OIC", "Investigator")
.requestMatchers("/api/admin/**").hasAnyRole("OIC", "Admin")
.anyRequest().authenticated())
.exceptionHandling(exception -> exception
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
.accessDeniedHandler((request, response, accessDeniedException) -> {
response.setStatus(HttpStatus.FORBIDDEN.value());
response.setContentType("application/json");
response.getWriter().write("{\"message\":\"Access denied\"}");
}))
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
)
.authenticationProvider(authenticationProvider())
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);
.csrf(csrf -> csrf.disable())
// CRITICAL FIX: Enable CORS using the bean configuration
.cors(cors -> cors.configurationSource(corsConfigurationSource()))
.authorizeHttpRequests(auth -> auth
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.requestMatchers("/api/auth/**").permitAll()
.requestMatchers("/api/health").permitAll()
.requestMatchers("/api/admin/health").permitAll()
.requestMatchers("/api/facial/health").permitAll() // ML service health check
.requestMatchers("/api/call-analysis/health").permitAll() // ML service health check
// ML Service endpoints
.requestMatchers("/api/call-analysis/**").hasRole("Investigator")
.requestMatchers("/api/facial/register").hasAnyRole("Investigator", "OIC")
.requestMatchers("/api/facial/criminals").hasAnyRole("Investigator", "OIC")
.requestMatchers("/api/facial/**").hasRole("Investigator")
// Criminal CRUD (direct DB, no Python)
.requestMatchers("/api/criminals/**").hasAnyRole("Investigator", "OIC")
.requestMatchers("/api/criminals").hasAnyRole("Investigator", "OIC")
.requestMatchers("/api/database/**").permitAll()
.requestMatchers("/api/test").permitAll()
.requestMatchers("/api/debug/**").permitAll() // 🔍 Debug endpoints
.requestMatchers("/error").permitAll() // Allow error page without auth
// Public endpoints
.requestMatchers("/api/vehicle**").permitAll()
.requestMatchers("/api/mobile/auth/**").permitAll()
.requestMatchers("/api/duties/**").permitAll()
.requestMatchers("/api/crime-reports/map").permitAll()
.requestMatchers(HttpMethod.GET, "/api/crime-reports").permitAll()
.requestMatchers("/api/crime-reports/upload-evidence").authenticated()
.requestMatchers("/api/crime-reports/**").hasAnyRole("OIC", "Admin")
// Field Officer routes
.requestMatchers("/api/officers/me/**").hasRole("FieldOfficer")
.requestMatchers("/api/mobile/**").hasRole("FieldOfficer")
.requestMatchers("/api/leaves/**").permitAll()
// OIC-only routes
.requestMatchers("/api/duty-schedules/**").hasRole("OIC")
.requestMatchers("/api/weapon/**").hasRole("OIC")
.requestMatchers("/api/weapon-issue/**").hasRole("OIC")
// Admin/OIC/Investigator routes (officer data, locations, users)
.requestMatchers("/api/users/field-officers").hasAnyRole("Admin", "OIC", "Investigator")
.requestMatchers("/api/admin/officers/*/locations/**").hasAnyRole("Admin", "OIC", "Investigator")
// Admin-only: backup, restore, settings (must come before general /api/admin/**)
.requestMatchers("/api/admin/backup").hasRole("Admin")
.requestMatchers("/api/admin/restore").hasRole("Admin")
.requestMatchers("/api/admin/backups").hasRole("Admin")
.requestMatchers("/api/admin/settings").hasRole("Admin")
.requestMatchers("/api/admin/**").hasAnyRole("OIC", "Admin")
.anyRequest().authenticated())
.exceptionHandling(exception -> exception
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
.accessDeniedHandler((request, response, accessDeniedException) -> {
response.setStatus(HttpStatus.FORBIDDEN.value());
response.setContentType("application/json");
response.getWriter().write("{\"message\":\"Access denied\"}");
}))
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authenticationProvider(authenticationProvider())
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
@Bean
public AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(userDetailsService);
authProvider.setPasswordEncoder(passwordEncoder());
return authProvider;
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
return config.getAuthenticationManager();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
// Use allowedOriginPatterns for wildcard support with credentials
// For production, replace with specific origins
configuration.setAllowedOriginPatterns(List.of("*"));
// Or use specific origins (recommended for production):
// configuration.setAllowedOrigins(Arrays.asList(
// "http://localhost:5173",
// "http://localhost:3000",
// "https://yourdomain.com"
// ));
configuration.setAllowedMethods(Arrays.asList(
"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"));
configuration.setAllowedHeaders(List.of("*"));
configuration.setExposedHeaders(List.of("Authorization"));
configuration.setAllowCredentials(true);
configuration.setMaxAge(3600L);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}