| 1 | package com.nikola.web3ednevnikbackend.config.security;
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 | import com.nikola.web3ednevnikbackend.service.security.impl.UserDetailsServiceImpl;
|
|---|
| 5 | import lombok.RequiredArgsConstructor;
|
|---|
| 6 | import lombok.extern.slf4j.Slf4j;
|
|---|
| 7 | import org.springframework.context.annotation.Bean;
|
|---|
| 8 | import org.springframework.context.annotation.Configuration;
|
|---|
| 9 | import org.springframework.security.authentication.AuthenticationManager;
|
|---|
| 10 |
|
|---|
| 11 | import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
|---|
| 12 |
|
|---|
| 13 | import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
|
|---|
| 14 | import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
|---|
| 15 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|---|
| 16 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|---|
| 17 | import org.springframework.security.config.http.SessionCreationPolicy;
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 | import org.springframework.security.crypto.password.PasswordEncoder;
|
|---|
| 21 | import org.springframework.security.web.SecurityFilterChain;
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 | import java.util.List;
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | @Slf4j
|
|---|
| 29 | @Configuration
|
|---|
| 30 | @EnableWebSecurity
|
|---|
| 31 | @EnableMethodSecurity
|
|---|
| 32 | @RequiredArgsConstructor
|
|---|
| 33 | public class SecurityConfig {
|
|---|
| 34 |
|
|---|
| 35 | private final UserDetailsServiceImpl userDetailsService;
|
|---|
| 36 | private final PasswordEncoder passwordEncoder;
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | @Bean
|
|---|
| 41 | public DaoAuthenticationProvider authenticationProvider() {
|
|---|
| 42 | DaoAuthenticationProvider provider = new DaoAuthenticationProvider(userDetailsService);
|
|---|
| 43 | System.out.println("AUTH PROVIDER CREATED");
|
|---|
| 44 | provider.setPasswordEncoder(passwordEncoder);
|
|---|
| 45 | return provider;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | @Bean
|
|---|
| 49 | public AuthenticationManager authenticationManager(
|
|---|
| 50 | AuthenticationConfiguration config) throws Exception {
|
|---|
| 51 | return config.getAuthenticationManager();
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | @Bean
|
|---|
| 55 | public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
|---|
| 56 |
|
|---|
| 57 | http
|
|---|
| 58 |
|
|---|
| 59 | .csrf(csrf -> csrf.disable())
|
|---|
| 60 | .cors(cors -> cors.configurationSource(request -> {
|
|---|
| 61 | var config = new org.springframework.web.cors.CorsConfiguration();
|
|---|
| 62 | config.setAllowedOrigins(List.of("http://localhost:5173"));
|
|---|
| 63 | config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
|
|---|
| 64 | config.setAllowedHeaders(List.of("*"));
|
|---|
| 65 | config.setAllowCredentials(true);
|
|---|
| 66 | return config;
|
|---|
| 67 | }))
|
|---|
| 68 | .authenticationProvider(authenticationProvider())
|
|---|
| 69 | .authorizeHttpRequests(auth -> auth
|
|---|
| 70 | .requestMatchers("/api/auth/login").permitAll()
|
|---|
| 71 | .anyRequest().authenticated()
|
|---|
| 72 | )
|
|---|
| 73 | .logout(logout->logout
|
|---|
| 74 | .logoutUrl("/api/auth/logout")
|
|---|
| 75 | .deleteCookies("JSESSIONID")
|
|---|
| 76 | .logoutSuccessHandler((request, response, authentication) -> {
|
|---|
| 77 | response.setStatus(200);
|
|---|
| 78 |
|
|---|
| 79 | })
|
|---|
| 80 | )
|
|---|
| 81 |
|
|---|
| 82 | .sessionManagement(session ->
|
|---|
| 83 | session.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
|
|---|
| 84 | );
|
|---|
| 85 |
|
|---|
| 86 | return http.build();
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|