-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityConfig.java
More file actions
72 lines (63 loc) · 3.6 KB
/
SecurityConfig.java
File metadata and controls
72 lines (63 loc) · 3.6 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.FixLog.config;
import com.example.FixLog.repository.MemberRepository;
import com.example.FixLog.util.JwtUtil;
import jakarta.servlet.Filter;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.Customizer;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@RequiredArgsConstructor
public class SecurityConfig {
private final JwtUtil jwtUtil;
private final MemberRepository memberRepository;
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.cors(Customizer.withDefaults()) // CORS 설정 추가 (WebConfig와 연결됨)
.authorizeHttpRequests(auth -> auth
.requestMatchers(HttpMethod.GET, "/", "/main", "/main/**").permitAll()
.requestMatchers(HttpMethod.GET, "/auth/**").permitAll()
.requestMatchers(HttpMethod.POST, "/auth/**").permitAll()
.requestMatchers(HttpMethod.POST, "/members/signup").permitAll()
.requestMatchers(HttpMethod.GET, "/members/check-email").permitAll()
.requestMatchers(HttpMethod.GET, "/members/check-nickname").permitAll()
.requestMatchers(HttpMethod.GET, "/", "/main", "/main/**").permitAll()
.requestMatchers(HttpMethod.GET, "/posts/**").permitAll()
// h2-console (로컬 테스트용)
.requestMatchers(HttpMethod.GET, "/h2-console/**").permitAll()
// 배포 확인용 임시 허용
.requestMatchers(HttpMethod.GET, "/test", "/test/**").permitAll()
// Swagger 허용
.requestMatchers(HttpMethod.GET,"/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html").permitAll()
.requestMatchers(HttpMethod.POST,"/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html").permitAll()
.requestMatchers(HttpMethod.PATCH,"/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html").permitAll()
// 그 외 모든 요청은 인증 필요
.anyRequest().authenticated()
)
.headers(headers -> headers.frameOptions(frame -> frame.disable())) // H2 콘솔
.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
return http.build();
}
@Bean
public Filter jwtAuthenticationFilter() {
return new com.example.FixLog.config.JwtAuthenticationFilter(jwtUtil, memberRepository);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
return config.getAuthenticationManager();
}
}