-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSecurityConfig.java
More file actions
133 lines (114 loc) · 6.07 KB
/
SecurityConfig.java
File metadata and controls
133 lines (114 loc) · 6.07 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package umc.codeplay.config;
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
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.AuthenticationEntryPoint;
import org.springframework.security.web.SecurityFilterChain;
import com.fasterxml.jackson.databind.ObjectMapper;
import umc.codeplay.apiPayLoad.ApiResponse;
import umc.codeplay.apiPayLoad.code.status.ErrorStatus;
import umc.codeplay.jwt.JwtAuthenticationFilter;
import umc.codeplay.jwt.JwtUtil;
@EnableWebSecurity
@Configuration
public class SecurityConfig {
private final JwtUtil jwtUtil;
private final ObjectMapper objectMapper;
public SecurityConfig(JwtUtil jwtUtil, ObjectMapper objectMapper) {
this.jwtUtil = jwtUtil;
this.objectMapper = objectMapper;
}
// AuthenticationManager 를 빈으로 등록 (스프링 시큐리티 6.x 이상)
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration)
throws Exception {
return configuration.getAuthenticationManager();
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
AuthenticationEntryPoint entryPoint = new CustomAuthenticationEntryPoint(objectMapper);
http
// 세션을 사용하지 않도록 설정
.sessionManagement(
session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.csrf(AbstractHttpConfigurer::disable) // JWT 사용 시 일반적으로 CSRF 는 disable
.exceptionHandling(exception -> exception.authenticationEntryPoint(entryPoint))
.authorizeHttpRequests(
auth ->
auth
// 로그인, 회원가입 등 토큰 없이 접근해야 하는 API 허용
.requestMatchers(
"/python-model/**",
"/oauth/**",
"/health",
"/health/s3",
"/auth/**",
"/member/**", // ??
"/v2/api-docs",
"/v3/api-docs",
"/v3/api-docs/**",
"/swagger-resources",
"/swagger-resources/**",
"/configuration/ui",
"/configuration/security",
"/swagger-ui/**",
"/webjars/**",
"/swagger-ui.html")
.permitAll()
// 그 외 나머지는 인증 필요
.anyRequest()
.authenticated())
// 폼 로그인 등 기본 기능 비활성화 (JWT 만 쓰려면)
.formLogin(Customizer.withDefaults())
// .formLogin(form -> form.disable()) // 더 엄격하게
// 폼 로그인 완전히 비활성화할 수도 있음
.logout(AbstractHttpConfigurer::disable);
// 커스텀 JWT 필터 추가
// UsernamePasswordAuthenticationFilter 이전에 동작하도록 설정
http.addFilterBefore(
new JwtAuthenticationFilter(jwtUtil),
org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
.class);
return http.build();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
public static class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
private final ObjectMapper objectMapper;
public CustomAuthenticationEntryPoint(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Override
public void commence(
HttpServletRequest request,
HttpServletResponse response,
org.springframework.security.core.AuthenticationException authException)
throws IOException, ServletException {
ApiResponse<Void> apiResponse =
ApiResponse.onFailure(
ErrorStatus.NOT_AUTHORIZED.getCode(),
ErrorStatus.NOT_AUTHORIZED.getMessage(),
null);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setContentType(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8");
response.setCharacterEncoding("UTF-8");
String jsonResponse = objectMapper.writeValueAsString(apiResponse);
response.getWriter().write(jsonResponse);
}
}
}