-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityConfig.java
More file actions
87 lines (77 loc) · 4.4 KB
/
Copy pathSecurityConfig.java
File metadata and controls
87 lines (77 loc) · 4.4 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
package com.codeit.team5.mopl.config;
import com.codeit.team5.mopl.auth.filter.JwtAuthenticationFilter;
import com.codeit.team5.mopl.auth.handler.SpaCsrfTokenRequestHandler;
import com.codeit.team5.mopl.auth.handler.UserAccessDeniedHandler;
import com.codeit.team5.mopl.auth.handler.UserAuthenticationEntryPoint;
import com.codeit.team5.mopl.auth.security.provider.MoplAuthenticationProvider;
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.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.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final JwtAuthenticationFilter jwtAuthenticationFilter;
private final UserAuthenticationEntryPoint userAuthenticationEntryPoint;
private final UserAccessDeniedHandler userAccessDeniedHandler;
private final MoplAuthenticationProvider moplAuthenticationProvider;
@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
.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("/api/users/**").authenticated()
.requestMatchers("/api/follows/**").authenticated()
.requestMatchers("/api/notifications/**").authenticated()
.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()
)
.addFilterBefore(
jwtAuthenticationFilter,
UsernamePasswordAuthenticationFilter.class
)
.build();
}
@Bean
public AuthenticationManager authenticationManager(
AuthenticationConfiguration authenticationConfiguration
) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
}