-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSecurityConfig.java
More file actions
161 lines (141 loc) · 6.24 KB
/
SecurityConfig.java
File metadata and controls
161 lines (141 loc) · 6.24 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.sprint.mission.discodeit.security;
import static com.sprint.mission.discodeit.domain.enums.Role.*;
import static com.sprint.mission.discodeit.exception.ErrorCode.*;
import static org.springframework.http.HttpStatus.*;
import static org.springframework.security.config.http.SessionCreationPolicy.*;
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.access.expression.method.DefaultMethodSecurityExpressionHandler;
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
import org.springframework.security.access.hierarchicalroles.RoleHierarchyAuthoritiesMapper;
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
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.session.HttpSessionEventPublisher;
import org.springframework.web.cors.CorsConfiguration;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sprint.mission.discodeit.exception.ErrorResponse;
import com.sprint.mission.discodeit.security.jwt.JwtAuthenticationFilter;
import com.sprint.mission.discodeit.security.jwt.JwtLoginSuccessHandler;
import com.sprint.mission.discodeit.security.jwt.JwtLogoutHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Configuration
@EnableMethodSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final ObjectMapper objectMapper;
@Bean
public SecurityFilterChain filterChain(
HttpSecurity http,
JwtLoginSuccessHandler jwtLoginSuccessHandler,
JwtLogoutHandler jwtLogoutHandler,
LoginFailureHandler loginFailureHandler,
JwtAuthenticationFilter jwtAuthenticationFilter
) throws Exception {
http
.authorizeHttpRequests(auth -> auth
// permitAll 경로 설정
.requestMatchers(SecurityWhitelist.WHITE_LIST.toArray(String[]::new)).permitAll()
.requestMatchers(HttpMethod.POST, "/api/users").permitAll() // 회원 가입
.anyRequest().authenticated()
)
.formLogin(login -> login
.loginProcessingUrl("/api/auth/login")
.successHandler(jwtLoginSuccessHandler)
.failureHandler(loginFailureHandler)
)
.logout(logout -> logout
.logoutUrl("/api/auth/logout")
.addLogoutHandler(jwtLogoutHandler)
.logoutSuccessHandler(
new HttpStatusReturningLogoutSuccessHandler(HttpStatus.NO_CONTENT))
)
.csrf(csrf -> csrf
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.csrfTokenRequestHandler(new SpaCsrfTokenRequestHandler())
.ignoringRequestMatchers("/api/auth/logout")
)
.exceptionHandling(ex -> ex
.accessDeniedHandler((request, response, accessDeniedException) -> {
response.setStatus(FORBIDDEN.value());
response.setContentType("application/json");
ErrorResponse body = ErrorResponse.of(NOT_AUTHORIZED, FORBIDDEN.value(), accessDeniedException);
response.getWriter().write(objectMapper.writeValueAsString(body));
})
.authenticationEntryPoint((request, response, authException) -> {
response.setStatus(FORBIDDEN.value());
response.setContentType("application/json");
ErrorResponse body = ErrorResponse.of(NOT_AUTHORIZED, FORBIDDEN.value(), authException);
response.getWriter().write(objectMapper.writeValueAsString(body));
})
)
.sessionManagement(session -> session.sessionCreationPolicy(STATELESS))
.addFilterBefore(
jwtAuthenticationFilter,
UsernamePasswordAuthenticationFilter.class
)
// .rememberMe(Customizer.withDefaults())
;
// 8) cors 설정 추가
http.cors(cors -> cors.configurationSource(request -> {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOriginPattern("*"); // 모든 Origin 허용
config.addAllowedHeader("*"); // 모든 Header 허용
config.addAllowedMethod("*"); // 모든 Method 허용
config.setAllowCredentials(true); // 쿠키/인증정보 허용 (필요할 때만)
return config;
}));
return http.build();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public RoleHierarchy roleHierarchy() {
return RoleHierarchyImpl.withDefaultRolePrefix()
.role(ADMIN.name()).implies(CHANNEL_MANAGER.name())
.role(CHANNEL_MANAGER.name()).implies(USER.name())
.build();
}
@Bean
public MethodSecurityExpressionHandler methodSecurityExpressionHandler(RoleHierarchy roleHierarchy) {
DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
expressionHandler.setRoleHierarchy(roleHierarchy);
return expressionHandler;
}
@Bean
public SessionRegistry sessionRegistry() {
return new SessionRegistryImpl();
}
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
@Bean
public DaoAuthenticationProvider daoAuthenticationProvider(
UserDetailsService userDetailsService,
PasswordEncoder passwordEncoder,
RoleHierarchy roleHierarchy
) {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(userDetailsService);
provider.setPasswordEncoder(passwordEncoder);
provider.setAuthoritiesMapper(new RoleHierarchyAuthoritiesMapper(roleHierarchy));
return provider;
}
}