-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSecurityConfig.java
More file actions
85 lines (77 loc) · 4.6 KB
/
Copy pathWebSecurityConfig.java
File metadata and controls
85 lines (77 loc) · 4.6 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
package ch.fhnw.timerecordingbackend.config;
import ch.fhnw.timerecordingbackend.security.JwtAuthenticationFilter;
import ch.fhnw.timerecordingbackend.security.UserDetailsServiceImpl;
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.security.authentication.AuthenticationManager;
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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@Configuration
@EnableWebSecurity
@EnableMethodSecurity(prePostEnabled = true) // Stellt sicher, dass @PreAuthorize funktioniert
public class WebSecurityConfig {
@Autowired
private UserDetailsServiceImpl userDetailsService;
@Bean
public JwtAuthenticationFilter jwtAuthenticationFilter() {
return new JwtAuthenticationFilter();
}
@Bean
public AuthenticationManager authenticationManager(
AuthenticationConfiguration authConfig
) throws Exception {
return authConfig.getAuthenticationManager();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.sessionManagement(sm -> sm
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth -> auth
.requestMatchers(
"/", // Root, oft die index.html
"/index.html",
"/login.html", // Falls separate Login-Seite existiert
"/dashboard.html", // Die Hauptseite nach dem Login
"/css/**", // Alle CSS-Dateien
"/js/**", // Alle JavaScript-Dateien
"/images/**", // Alle Bilder (falls vorhanden)
"/api/auth/**", // Alle Authentifizierungs-Endpunkte (Login, etc.)
"/favicon.ico",
"/api/users/request-password-reset" // NEU: Erlaube diesen Endpunkt
).permitAll()
.requestMatchers(HttpMethod.PUT, "/api/users/change-password").authenticated() // Passwort ändern nur für authentifizierte User
.requestMatchers("/api/admin/**").hasAuthority("ADMIN") // Alle /api/admin/** Endpunkte nur für Admins
// .requestMatchers("/api/users/**").hasAuthority("ADMIN") // Diese Zeile entfernen oder anpassen, da /api/users/request-password-reset öffentlich ist
.requestMatchers("/api/projects/manage/**").hasAnyAuthority("ADMIN", "MANAGER") // Beispiel für Manager-Rechte
.requestMatchers( // Diese Endpunkte für alle authentifizierten Rollen
"/api/time-entries/**",
"/api/projects/**", // Detailansicht von Projekten etc.
"/api/reports/**", // Falls vorhanden
"/api/absences/**"
).hasAnyAuthority("ADMIN", "MANAGER", "EMPLOYEE")
.anyRequest().authenticated() // Alle anderen Anfragen erfordern Authentifizierung
)
.formLogin(formLogin -> formLogin.disable()) // Standard-Form-Login deaktivieren, da JWT verwendet wird
.httpBasic(httpBasic -> httpBasic.disable()) // HTTP Basic Auth deaktivieren
.addFilterBefore( // JWT-Filter vor dem Standard-Username/Password-Filter einfügen
jwtAuthenticationFilter(),
UsernamePasswordAuthenticationFilter.class
);
return http.build();
}
}