|
|
@@ -0,0 +1,80 @@
|
|
|
+package pl.dmcs.eldarmuk.backend_springboot.security;
|
|
|
+
|
|
|
+import jakarta.servlet.DispatcherType;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
|
|
+import org.springframework.security.config.Customizer;
|
|
|
+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.annotation.web.configurers.AbstractHttpConfigurer;
|
|
|
+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 pl.dmcs.eldarmuk.backend_springboot.security.jwt.JwtAuthEntryPoint;
|
|
|
+import pl.dmcs.eldarmuk.backend_springboot.security.jwt.JwtAuthTokenFilter;
|
|
|
+import pl.dmcs.eldarmuk.backend_springboot.security.services.UserDetailsServiceImpl;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@EnableWebSecurity
|
|
|
+@EnableMethodSecurity
|
|
|
+public class WebSecurityConfig {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ UserDetailsServiceImpl userDetailsService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private JwtAuthEntryPoint unauthorizedHandler;
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public JwtAuthTokenFilter authenticationJwtTokenFilter() {
|
|
|
+ return new JwtAuthTokenFilter();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ DaoAuthenticationProvider authProvider(){
|
|
|
+ DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
|
|
|
+ authProvider.setUserDetailsService(userDetailsService);
|
|
|
+ authProvider.setPasswordEncoder(passwordEncoder());
|
|
|
+ return authProvider;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public PasswordEncoder passwordEncoder() {
|
|
|
+ return new BCryptPasswordEncoder();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
|
|
+
|
|
|
+ http
|
|
|
+ .cors(Customizer.withDefaults())
|
|
|
+ .csrf(AbstractHttpConfigurer::disable)
|
|
|
+ .authorizeHttpRequests((auth) -> auth
|
|
|
+ .requestMatchers("/auth/**").permitAll()
|
|
|
+ .requestMatchers("/students/**").permitAll()
|
|
|
+ .requestMatchers("/teachers/**").permitAll()
|
|
|
+ .requestMatchers("/subjects/**").permitAll()
|
|
|
+ .requestMatchers("/grades/**").permitAll()
|
|
|
+ .requestMatchers("/error").permitAll() // this enables the body in the exception responses
|
|
|
+ .anyRequest().authenticated()
|
|
|
+ )
|
|
|
+ .exceptionHandling(unauthorized -> unauthorized
|
|
|
+ .authenticationEntryPoint(unauthorizedHandler)
|
|
|
+ )
|
|
|
+ .sessionManagement(session -> session
|
|
|
+ .sessionCreationPolicy(SessionCreationPolicy.STATELESS));
|
|
|
+
|
|
|
+ http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
|
|
|
+ return http.build();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|