| [700e2f9] | 1 | package com.finki.icare.config;
|
|---|
| 2 |
|
|---|
| 3 | import lombok.RequiredArgsConstructor;
|
|---|
| 4 | import org.springframework.context.annotation.Bean;
|
|---|
| 5 | import org.springframework.context.annotation.Configuration;
|
|---|
| 6 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|---|
| 7 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|---|
| 8 | import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
|---|
| 9 | import org.springframework.security.config.http.SessionCreationPolicy;
|
|---|
| 10 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|---|
| 11 | import org.springframework.security.crypto.password.PasswordEncoder;
|
|---|
| 12 | import org.springframework.security.web.SecurityFilterChain;
|
|---|
| 13 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|---|
| 14 |
|
|---|
| 15 | @Configuration
|
|---|
| 16 | @EnableWebSecurity
|
|---|
| 17 | @RequiredArgsConstructor
|
|---|
| 18 | public class SecurityConfig {
|
|---|
| 19 |
|
|---|
| 20 | private final JwtAuthenticationFilter jwtAuthenticationFilter;
|
|---|
| 21 |
|
|---|
| 22 | @Bean
|
|---|
| 23 | public PasswordEncoder passwordEncoder() {
|
|---|
| 24 | return new BCryptPasswordEncoder();
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | @Bean
|
|---|
| 28 | public SecurityFilterChain securityFilterChain(HttpSecurity http) {
|
|---|
| 29 | http
|
|---|
| 30 | .csrf(AbstractHttpConfigurer::disable)
|
|---|
| 31 | .cors(cors -> cors.configure(http))
|
|---|
| 32 | .authorizeHttpRequests(auth -> auth
|
|---|
| 33 | .requestMatchers("/api/auth/**")
|
|---|
| 34 | .permitAll()
|
|---|
| 35 | .requestMatchers("/api/blogs/**")
|
|---|
| 36 | .authenticated()
|
|---|
| 37 | .anyRequest()
|
|---|
| 38 | .authenticated()
|
|---|
| 39 | )
|
|---|
| 40 | .sessionManagement(session -> session
|
|---|
| 41 | .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
|---|
| 42 | )
|
|---|
| 43 | .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
|
|---|
| 44 |
|
|---|
| 45 | return http.build();
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|