implemented friendship system

This commit is contained in:
2025-12-16 17:47:13 +01:00
parent c05420099a
commit 38ce235522
19 changed files with 542 additions and 48 deletions
@@ -27,12 +27,9 @@ import org.springframework.security.oauth2.jwt.NimbusJwtEncoder;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.header.writers.XXssProtectionHeaderWriter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
@@ -43,19 +40,26 @@ import static org.springframework.security.config.Customizer.withDefaults;
@EnableWebSecurity
public class SecurityConfiguration {
private static final String[] AUTH_WHITELIST = {
"/api/v1/auth/**",
"/v3/api-docs/**",
"/v3/api-docs.yaml",
"/swagger-ui/**",
"/swagger-ui.html",
};
private final RSAKeyProperties keys;
public SecurityConfiguration(RSAKeyProperties keys){
public SecurityConfiguration(RSAKeyProperties keys) {
this.keys = keys;
}
@Bean
public PasswordEncoder passwordEncoder(){
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public AuthenticationManager authenticationManager(UserDetailsService detailsService){
public AuthenticationManager authenticationManager(UserDetailsService detailsService) {
DaoAuthenticationProvider daoProvider = new DaoAuthenticationProvider();
daoProvider.setUserDetailsService(detailsService);
daoProvider.setPasswordEncoder(passwordEncoder());
@@ -78,7 +82,7 @@ public class SecurityConfiguration {
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.cors(withDefaults())
@@ -88,7 +92,8 @@ public class SecurityConfiguration {
auth.requestMatchers("/auth/user/**").permitAll();
auth.requestMatchers("/admin/**").hasRole("ADMIN"); //looks for "ROLE_ADMIN" //alternative: hasAuthoritiy("ADMIN")
auth.requestMatchers("/user/**").hasAnyRole("ADMIN", "USER");
auth.requestMatchers("/trip/**").hasAnyRole( "USER", "ADMIN");
auth.requestMatchers("/friendship/**").hasAnyRole("ADMIN", "USER");
auth.requestMatchers("/trip/**").hasAnyRole("USER", "ADMIN");
auth.requestMatchers(AUTH_WHITELIST).permitAll();
//auth.requestMatchers("/error").permitAll();
auth.anyRequest().authenticated();
@@ -112,21 +117,13 @@ public class SecurityConfiguration {
return http.build();
}
private static final String[] AUTH_WHITELIST = {
"/api/v1/auth/**",
"/v3/api-docs/**",
"/v3/api-docs.yaml",
"/swagger-ui/**",
"/swagger-ui.html",
};
@Bean
public JwtDecoder jwtDecoder(){
public JwtDecoder jwtDecoder() {
return NimbusJwtDecoder.withPublicKey(keys.getPublicKey()).build();
}
@Bean
public JwtEncoder jwtEncoder(){
public JwtEncoder jwtEncoder() {
JWK jwk = new RSAKey.Builder(keys.getPublicKey()).privateKey(keys.getPrivateKey()).build();
JWKSource<SecurityContext> jwks = new ImmutableJWKSet<>(new JWKSet(jwk));
@@ -135,7 +132,7 @@ public class SecurityConfiguration {
@Bean
// for spring security, so it can correctly identifiy Roles (identifies roles via "ROLE_[role]")
public JwtAuthenticationConverter jwtAuthenticationConverter(){
public JwtAuthenticationConverter jwtAuthenticationConverter() {
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");