-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSecurityConfig.java
More file actions
171 lines (157 loc) · 9.17 KB
/
Copy pathSecurityConfig.java
File metadata and controls
171 lines (157 loc) · 9.17 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
package com.dbaagent.config;
import com.dbaagent.security.CustomUserDetailsService;
import com.dbaagent.security.JwtAuthenticationFilter;
import jakarta.servlet.DispatcherType;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Configuration
@EnableWebSecurity
@EnableMethodSecurity(prePostEnabled = true)
public class SecurityConfig {
@Autowired
private JwtAuthenticationFilter jwtAuthenticationFilter;
@Autowired
private com.dbaagent.security.McpTokenAuthenticationFilter mcpTokenAuthenticationFilter;
@Autowired
private CustomUserDetailsService customUserDetailsService;
@Value("${security.auth.enabled:true}")
private boolean authEnabled;
/**
* Localhost only. Every deployment that serves a browser from anywhere else must set
* {@code CORS_ALLOWED_ORIGINS} (or {@code cors.allowed.origins}) explicitly.
*
* <p>This default used to list the operating company's production hostnames. That is a
* property default in Java, where {@code SelfHostPropertiesSafetyTest} — which reads
* only {@code application*.properties} — could not see it, and it ships to every reader
* of the public repository. {@code CorsAllowlistSafetyTest} now scans this file too.
*/
@Value("${cors.allowed.origins:http://localhost:3000,http://localhost:3001,http://localhost:3002,http://127.0.0.1:3000,http://127.0.0.1:3001,http://127.0.0.1:3002,http://127.0.0.1:*,http://localhost:*}")
private String corsAllowedOrigins;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.cors(cors -> cors.configurationSource(corsConfigurationSource()));
if (authEnabled) {
// Production mode: Require authentication
http
.authorizeHttpRequests(auth -> auth
// Allow async dispatches (SSE streaming completion) without re-auth
.dispatcherTypeMatchers(DispatcherType.ASYNC).permitAll()
.requestMatchers(
"/auth/login",
"/auth/signup",
"/auth/email/start",
"/auth/email/verify",
"/auth/google/start",
"/auth/google/callback",
"/auth/mfa/enroll/start",
"/auth/mfa/enroll/confirm",
"/auth/mfa/verify",
"/auth/refresh",
"/auth/invite/preview",
"/auth/invite/accept",
"/auth/beta-signup",
// CLI authorization: token issuance endpoints are
// unauthenticated (caller is a logged-out CLI). The
// /approve, /deny, /pending and device/{approve,deny,pending}
// endpoints require an authenticated browser session.
"/auth/cli/authorize",
"/auth/cli/exchange",
"/auth/cli/device/code",
"/auth/cli/device/token",
// Actuator: only liveness/readiness without auth. metrics/prometheus
// require an authenticated session (see application-prod.properties).
"/actuator/health",
"/actuator/health/**",
"/error",
"/admin/bootstrap/link",
"/users/admin/bootstrap",
"/users/admin/reset",
"/invite-codes/validate/**",
// Onboarding: public endpoints — status check and first-run
// initialization don't require an existing user/token
"/setup/status",
"/setup/initialize",
// Internal test-suite token issuance — validated by pre-shared
// INTERNAL_TEST_TOKEN secret, not by user credentials
"/auth/internal/token",
// Public dashboard sharing — the share token is the authorization;
// these endpoints only resolve while the dashboard is published,
// and every query is server-enforced read-only.
"/public/**"
).permitAll()
// Admin endpoints require ADMIN role
.requestMatchers("/admin/**").hasRole("ADMIN")
.requestMatchers("/users", "/users/**").hasRole("ADMIN")
// Invite code management (except validate) requires ADMIN role
.requestMatchers("/invite-codes", "/invite-codes/**")
.hasRole("ADMIN")
.anyRequest().authenticated())
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.exceptionHandling(ex -> ex
.authenticationEntryPoint((request, response, authException) -> {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setContentType("application/json");
response.getWriter().write("{\"message\":\"Unauthorized - Please login\"}");
}))
.addFilterBefore(mcpTokenAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
.addFilterAfter(jwtAuthenticationFilter, com.dbaagent.security.McpTokenAuthenticationFilter.class);
} else {
// Development mode: Permit all requests
http.authorizeHttpRequests(auth -> auth.anyRequest().permitAll());
}
return http.build();
}
@Bean
public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
AuthenticationManagerBuilder auth = http.getSharedObject(AuthenticationManagerBuilder.class);
auth.userDetailsService(customUserDetailsService)
.passwordEncoder(passwordEncoder());
return auth.build();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
List<String> allowedOriginPatterns = Arrays.stream(corsAllowedOrigins.split(","))
.map(String::trim)
.filter(origin -> !origin.isEmpty())
.collect(Collectors.toList());
configuration.setAllowedOriginPatterns(allowedOriginPatterns);
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"));
configuration.setAllowedHeaders(Arrays.asList(
"Authorization",
"Content-Type",
"X-Requested-With",
"X-Admin-Bootstrap-Secret"));
configuration.setExposedHeaders(List.of("Authorization"));
configuration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}