-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSecurityConfig.java
More file actions
113 lines (102 loc) · 4.67 KB
/
SecurityConfig.java
File metadata and controls
113 lines (102 loc) · 4.67 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
package doldol_server.doldol.common.config;
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.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.authentication.logout.LogoutFilter;
import com.fasterxml.jackson.databind.ObjectMapper;
import doldol_server.doldol.auth.filter.CustomLogoutFilter;
import doldol_server.doldol.auth.filter.CustomUserLoginFilter;
import doldol_server.doldol.auth.filter.JwtAuthenticationFilter;
import doldol_server.doldol.auth.handler.CustomAccessDeniedHandler;
import doldol_server.doldol.auth.handler.CustomAuthenticationEntryPoint;
import doldol_server.doldol.auth.handler.CustomOAuth2FailureHandler;
import doldol_server.doldol.auth.handler.CustomOAuth2SuccessHandler;
import doldol_server.doldol.auth.jwt.TokenProvider;
import doldol_server.doldol.auth.resolver.CustomOAuth2ParameterResolver;
import doldol_server.doldol.auth.service.CustomOAuth2UserService;
import doldol_server.doldol.user.entity.Role;
import lombok.RequiredArgsConstructor;
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private static final String[] WHITELIST = {
"/auth/check-email",
"/auth/check-password-info",
"/auth/check-id",
"/auth/check-register-info",
"/auth/email/send-code",
"/auth/email/verify-code",
"/auth/register",
"/auth/oauth/register",
"/auth/reissue",
"/auth/validate/user/info",
"/auth/find/id",
"/auth/reset/password",
"/swagger-ui/**",
"/v3/api-docs/**",
"/swagger-resources/**",
"/webjars/**",
"/actuator/**",
"/papers/invite",
"/invites/**/comments",
};
private static final String[] BLACKLIST = {
"/auth/logout"
};
private final TokenProvider tokenProvider;
private final ObjectMapper objectMapper;
private final CustomOAuth2SuccessHandler customOAuth2SuccessHandler;
private final CustomOAuth2FailureHandler customOAuth2FailureHandler;
private final CustomOAuth2UserService customOAuth2UserService;
private final CustomOAuth2ParameterResolver customOAuth2ParameterResolver;
@Bean
public SecurityFilterChain filterChain(HttpSecurity http, AuthenticationManager authenticationManager)
throws Exception {
http
.cors(AbstractHttpConfigurer::disable)
.csrf(AbstractHttpConfigurer::disable)
.formLogin(AbstractHttpConfigurer::disable)
.httpBasic(AbstractHttpConfigurer::disable)
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.oauth2Login(oauth2 -> oauth2
.authorizationEndpoint(authorization -> authorization
.authorizationRequestResolver(customOAuth2ParameterResolver)
)
.userInfoEndpoint(userInfo -> userInfo
.userService(customOAuth2UserService)
)
.successHandler(customOAuth2SuccessHandler)
.failureHandler(customOAuth2FailureHandler)
)
.authorizeHttpRequests(auth -> auth
.requestMatchers("/auth/withdraw").hasAuthority(Role.ADMIN.getRole())
.requestMatchers("/admin/reports/**").hasAuthority(Role.ADMIN.getRole())
.requestMatchers(WHITELIST).permitAll()
.requestMatchers(HttpMethod.POST, "/reports").hasAuthority(Role.USER.getRole())
.requestMatchers(HttpMethod.GET, "/invites/**").permitAll()
.requestMatchers(BLACKLIST).authenticated()
.anyRequest().authenticated())
.addFilterAt(new CustomUserLoginFilter(authenticationManager, tokenProvider, objectMapper),
UsernamePasswordAuthenticationFilter.class)
.addFilterAfter(new JwtAuthenticationFilter(tokenProvider, WHITELIST, BLACKLIST, objectMapper),
CustomUserLoginFilter.class)
.addFilterBefore(new CustomLogoutFilter(tokenProvider, objectMapper), LogoutFilter.class)
.exceptionHandling(exceptions -> exceptions
.authenticationEntryPoint(new CustomAuthenticationEntryPoint(objectMapper))
.accessDeniedHandler(new CustomAccessDeniedHandler(objectMapper)));
return http.build();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception {
return configuration.getAuthenticationManager();
}
}