-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityConfig.java
More file actions
129 lines (116 loc) · 6.94 KB
/
Copy pathSecurityConfig.java
File metadata and controls
129 lines (116 loc) · 6.94 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
package com.codeit.team5.mopl.config;
import com.codeit.team5.mopl.auth.jwt.JwtAuthenticationFilter;
import com.codeit.team5.mopl.auth.jwt.JwtAuthenticationService;
import com.codeit.team5.mopl.auth.security.handler.signin.SignInFailureHandler;
import com.codeit.team5.mopl.auth.security.handler.signin.SignInSuccessHandler;
import com.codeit.team5.mopl.auth.security.handler.SpaCsrfTokenRequestHandler;
import com.codeit.team5.mopl.auth.security.handler.UserAccessDeniedHandler;
import com.codeit.team5.mopl.auth.security.handler.UserAuthenticationEntryPoint;
import com.codeit.team5.mopl.auth.security.handler.signout.SignOutHandler;
import com.codeit.team5.mopl.auth.security.provider.MoplAuthenticationProvider;
import jakarta.servlet.DispatcherType;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
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.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
@Configuration
@EnableWebSecurity
@EnableMethodSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final JwtAuthenticationService jwtAuthenticationService;
private final UserAuthenticationEntryPoint userAuthenticationEntryPoint;
private final UserAccessDeniedHandler userAccessDeniedHandler;
private final MoplAuthenticationProvider moplAuthenticationProvider;
private final SignInSuccessHandler signInSuccessHandler;
private final SignInFailureHandler signInFailureHandler;
private final SignOutHandler signOutHandler;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
PathPatternRequestMatcher.Builder paths =
PathPatternRequestMatcher.withDefaults();
return http
.csrf(csrf -> csrf
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.csrfTokenRequestHandler(new SpaCsrfTokenRequestHandler())
.ignoringRequestMatchers(
paths.matcher(HttpMethod.POST, "/api/auth/refresh")
)
)
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
)
.authenticationProvider(moplAuthenticationProvider)
.exceptionHandling(exception -> exception
.authenticationEntryPoint(userAuthenticationEntryPoint)
.accessDeniedHandler(userAccessDeniedHandler)
)
.authorizeHttpRequests(auth -> auth
// SSE ASYNC 재디스패치 시 JWT 필터가 건너뛰어 AuthorizationFilter에서 Access Denied가 발생하므로 ASYNC 타입은 전체 허용
.dispatcherTypeMatchers(DispatcherType.ASYNC).permitAll()
.requestMatchers("/api/auth/sign-out").permitAll()
.requestMatchers(HttpMethod.POST, "/api/users").permitAll()
.requestMatchers(HttpMethod.GET, "/api/users").hasRole("ADMIN")
.requestMatchers(HttpMethod.PATCH, "/api/users/*/role").hasRole("ADMIN")
.requestMatchers(HttpMethod.PATCH, "/api/users/*/locked").hasRole("ADMIN")
.requestMatchers(HttpMethod.GET, "/api/users/*").authenticated()
.requestMatchers(HttpMethod.PATCH, "/api/users/*").authenticated()
.requestMatchers(HttpMethod.GET, "/api/contents", "/api/contents/*").authenticated()
.requestMatchers(HttpMethod.POST, "/api/contents").hasRole("ADMIN")
.requestMatchers(HttpMethod.PATCH, "/api/contents/*").hasRole("ADMIN")
.requestMatchers(HttpMethod.DELETE, "/api/contents/*").hasRole("ADMIN")
.requestMatchers("/api/admin/**").hasRole("ADMIN")
.requestMatchers("/api/users/**").authenticated()
.requestMatchers("/api/follows/**").authenticated()
.requestMatchers("/api/conversations/**").authenticated()
.requestMatchers("/api/notifications/**").authenticated()
.requestMatchers("/api/sse/**").authenticated()
.requestMatchers("/api/reviews/**").authenticated()
.requestMatchers(HttpMethod.POST, "/api/auth/reset-password").permitAll()
.requestMatchers("/api/auth/sign-in").permitAll()
.requestMatchers("/api/auth/csrf-token").permitAll()
.requestMatchers("/api/auth/refresh").permitAll()
.requestMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll()
.anyRequest().permitAll()
)
.formLogin(login -> login
.loginProcessingUrl("/api/auth/sign-in")
.successHandler(signInSuccessHandler)
.failureHandler(signInFailureHandler)
)
.addFilterBefore(
jwtAuthenticationFilter(),
UsernamePasswordAuthenticationFilter.class
)
.logout(logout -> logout
.logoutUrl("/api/auth/sign-out")
.addLogoutHandler(signOutHandler)
.logoutSuccessHandler(
new HttpStatusReturningLogoutSuccessHandler(HttpStatus.NO_CONTENT)
)
)
.build();
}
@Bean
public JwtAuthenticationFilter jwtAuthenticationFilter() {
return new JwtAuthenticationFilter(jwtAuthenticationService);
}
@Bean
public AuthenticationManager authenticationManager(
AuthenticationConfiguration authenticationConfiguration
) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
}