forked from InhaBas/Inhabas.com-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthSecurityConfig.java
More file actions
72 lines (64 loc) · 3.29 KB
/
AuthSecurityConfig.java
File metadata and controls
72 lines (64 loc) · 3.29 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
package com.inhabas.api.auth.config;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
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.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.cors.CorsUtils;
import com.inhabas.api.auth.domain.oauth2.CustomOAuth2UserService;
import com.inhabas.api.auth.domain.oauth2.cookie.HttpCookieOAuth2AuthorizationRequestRepository;
import com.inhabas.api.auth.domain.oauth2.handler.Oauth2AuthenticationFailureHandler;
import com.inhabas.api.auth.domain.oauth2.handler.Oauth2AuthenticationSuccessHandler;
@Order(0) // 인증 관련 security filter chain 은 우선순위가 가장 높아야 함.
@EnableWebSecurity
@Configuration
@RequiredArgsConstructor
@Profile({"dev1", "dev2", "local", "prod1", "prod2"}) // 테스트에는 포함시키지 않음.
public class AuthSecurityConfig {
private final CustomOAuth2UserService customOAuth2UserService;
private final OAuth2AuthorizedClientService authorizedClientService;
private final Oauth2AuthenticationSuccessHandler oauth2AuthenticationSuccessHandler;
private final Oauth2AuthenticationFailureHandler oauth2AuthenticationFailureHandler;
private final HttpCookieOAuth2AuthorizationRequestRepository
httpCookieOAuth2AuthorizationRequestRepository;
@Bean
@Order(0)
public SecurityFilterChain authSecurityFilterChain(HttpSecurity http) throws Exception {
http
// /login/** 경로에만 이 보안 체인 적용
.securityMatcher("/login/**")
// 세션 생성 금지
.sessionManagement(
session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.cors(cors -> {})
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(
authorize ->
authorize
.requestMatchers(request -> CorsUtils.isPreFlightRequest(request))
.permitAll()
.anyRequest()
.permitAll())
// Oauth 로그인 설정
.oauth2Login(
oauth2 ->
oauth2
.authorizedClientService(authorizedClientService)
.authorizationEndpoint(
authorization ->
authorization
.baseUri("/login/oauth2/authorization")
.authorizationRequestRepository(
httpCookieOAuth2AuthorizationRequestRepository))
.userInfoEndpoint(userInfo -> userInfo.userService(customOAuth2UserService))
.failureHandler(oauth2AuthenticationFailureHandler)
.successHandler(oauth2AuthenticationSuccessHandler));
return http.build();
}
}