-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityConfig.java
More file actions
73 lines (65 loc) · 3.62 KB
/
Copy pathSecurityConfig.java
File metadata and controls
73 lines (65 loc) · 3.62 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
package com.example.team4backend.config;
import com.example.team4backend.security.CustomAccessDeniedHandler;
import com.example.team4backend.security.CustomAuthenticationEntryPoint;
import com.example.team4backend.security.CustomUserDetailsService;
import com.example.team4backend.security.jwt.JwtAuthenticationFilter;
import com.example.team4backend.security.jwt.JwtTokenProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.csrf.CsrfTokenRequestAttributeHandler;
import org.springframework.web.cors.CorsConfigurationSource;
import static com.example.team4backend.security.SecurityPaths.*;
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final JwtTokenProvider jwtTokenProvider;
private final CustomUserDetailsService customUserDetailsService;
private final CorsConfigurationSource corsConfigurationSource;
private final CustomAuthenticationEntryPoint customAuthenticationEntryPoint;
private final CustomAccessDeniedHandler customAccessDeniedHandler;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
// CookieCsrfTokenRepository csrfTokenRepository =
// CookieCsrfTokenRepository.withHttpOnlyFalse();
// csrfTokenRepository.setCookiePath("/");
// CsrfTokenRequestAttributeHandler requestHandler = new CsrfTokenRequestAttributeHandler();
//requestHandler.setCsrfRequestAttributeName(null);
http
.cors(cors -> cors.configurationSource(corsConfigurationSource))
// .csrf(csrf -> csrf
// .csrfTokenRepository(csrfTokenRepository)
// .csrfTokenRequestHandler(requestHandler)
// .ignoringRequestMatchers(CSRF_IGNORED)
// )
.csrf(AbstractHttpConfigurer::disable)
.exceptionHandling(ex -> ex
.authenticationEntryPoint(customAuthenticationEntryPoint)
.accessDeniedHandler(customAccessDeniedHandler)
)
.formLogin(AbstractHttpConfigurer::disable)
// 헤더로 아이디/비번 보내는 방식 비활성화
.httpBasic(AbstractHttpConfigurer::disable)
.sessionManagement(sm ->
sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
)
.authorizeHttpRequests(auth -> auth
.requestMatchers(PUBLIC_AUTH).permitAll()
.requestMatchers(PUBLIC_DOCS).permitAll()
.anyRequest().authenticated()
)
.userDetailsService(customUserDetailsService)
// JWT 인증 필터 추가
.addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider, customUserDetailsService),
UsernamePasswordAuthenticationFilter.class);
return http.build();
}
}