Skip to content

Commit c0bc119

Browse files
authored
Merge pull request #61 from CommitField/feature/#6
style : ์ฃผ์„ ์‚ญ์ œ ๋ฐ ๊ถŒํ•œ ์„ค์ • ์ฝ”๋“œ ์ถ”๊ฐ€
2 parents 1da2445 + 1656037 commit c0bc119

File tree

1 file changed

+80
-67
lines changed

1 file changed

+80
-67
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,89 @@
11
package cmf.commitField.global.security;
22

3+
import cmf.commitField.domain.user.entity.CustomOAuth2User;
4+
import cmf.commitField.domain.user.service.CustomOAuth2UserService;
5+
import org.springframework.context.annotation.Bean;
36
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
48
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9+
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
10+
import org.springframework.security.config.http.SessionCreationPolicy;
11+
import org.springframework.security.core.context.SecurityContextHolder;
12+
import org.springframework.security.oauth2.core.user.OAuth2User;
13+
import org.springframework.security.web.SecurityFilterChain;
14+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
15+
16+
import static org.springframework.security.config.Customizer.withDefaults;
517

618
@Configuration
719
@EnableWebSecurity
820
public class SecurityConfig {
9-
// private final CustomOAuth2UserService customOAuth2UserService;
10-
//
11-
// public SecurityConfig(CustomOAuth2UserService customOAuth2UserService) {
12-
// this.customOAuth2UserService = customOAuth2UserService;
13-
// }
14-
//
15-
// @Bean
16-
// protected SecurityFilterChain config(HttpSecurity http) throws Exception {
17-
// // ๊ถŒํ•œ ์„ค์ •
18-
// http
19-
// .authorizeHttpRequests(auth -> auth
20-
// .requestMatchers("/actuator/**").permitAll() // actuator ์—”๋“œํฌ์ธํŠธ ํ—ˆ์šฉ
21-
// .anyRequest().authenticated() // ๊ทธ ์™ธ ๋ชจ๋“  ์š”์ฒญ์€ ์ธ์ฆ ํ•„์š”
22-
// );
23-
//
24-
// //๋กœ๊ทธ์ธ ๊ด€๋ จ ์„ค์ •
25-
// http
26-
// .oauth2Login(oauth2 -> oauth2
27-
// .loginPage("/login") // ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ์ง€์ •
28-
// .successHandler((request, response, authentication) -> {
29-
// // ์ธ์ฆ ์ •๋ณด๊ฐ€ SecurityContext์— ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์„ ๋ณด์žฅ
30-
// SecurityContextHolder.getContext().setAuthentication(authentication);
31-
//
32-
// CustomOAuth2User customUser = (CustomOAuth2User) authentication.getPrincipal();
33-
//
34-
// // ๋””๋ฒ„๊น…: authentication ์ •๋ณด ํ™•์ธ
35-
// System.out.println("Authentication: " + authentication);
36-
// System.out.println("Principal: " + authentication.getPrincipal());
37-
//
38-
// if (authentication != null && authentication.getPrincipal() != null) {
39-
// //์ธ๊ฐ€๊ฐ€ ์žˆ์œผ๋ฉด ์œ ์ € ์ •๋ณด๋ฅผ ์ €์žฅ
40-
// OAuth2User principal = (OAuth2User) authentication.getPrincipal();
41-
// String username = principal.getAttribute("login");
42-
//
43-
// // ์„ธ์…˜์— ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์ถ”๊ฐ€
44-
// request.getSession().setAttribute("user", username);
45-
//
46-
// response.sendRedirect("/"); // ๋กœ๊ทธ์ธ ์„ฑ๊ณต ํ›„ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ
47-
// } else {
48-
// // ์ธ์ฆ ์‹คํŒจ ์‹œ ์ฒ˜๋ฆฌ
49-
// response.sendRedirect("/login?error=authenticationFailed");
50-
// }
51-
// })
52-
// )
53-
// .sessionManagement(session -> session
54-
// .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) // ์„ธ์…˜ ์ •์ฑ… ์„ค์ •
55-
// .invalidSessionUrl("/login?error=invalidSession") // ์„ธ์…˜์ด ์œ ํšจํ•˜์ง€ ์•Š์œผ๋ฉด ์ด๋™ํ•  URL
56-
// .maximumSessions(1) // ํ•˜๋‚˜์˜ ๊ณ„์ •์œผ๋กœ ํ•œ ๋ฒˆ์— ๋กœ๊ทธ์ธํ•  ์ˆ˜ ์žˆ๋„๋ก ์ œํ•œ
57-
// .expiredUrl("/login?error=sessionExpired") // ์„ธ์…˜ ๋งŒ๋ฃŒ ํ›„ ์ด๋™ํ•  URL ์„ค์ •
58-
// );
59-
//
60-
// //๋กœ๊ทธ์•„์›ƒ ๊ด€๋ จ ์„ค์ •
61-
// http
62-
// .logout(logout -> logout
63-
// .logoutUrl("/logout") // ๋กœ๊ทธ์•„์›ƒ URL ์„ค์ •
64-
// .logoutSuccessUrl("/") // ๋กœ๊ทธ์•„์›ƒ ์„ฑ๊ณต ํ›„ ์ด๋™ํ•  URL
65-
// .invalidateHttpSession(true) // ๋กœ๊ทธ์•„์›ƒ ์‹œ ์„ธ์…˜ ๋ฌดํšจํ™”
66-
// .clearAuthentication(true) // ์ธ์ฆ ์ •๋ณด ์ง€์šฐ๊ธฐ
67-
// .deleteCookies("JSESSIONID") // ์„ธ์…˜ ์ฟ ํ‚ค ์‚ญ์ œ
68-
// );
69-
// http
70-
// .csrf(
71-
// AbstractHttpConfigurer::disable // CSRF ๋ณดํ˜ธ ๋น„ํ™œ์„ฑํ™”
72-
// );
73-
//
74-
// return http.build();
75-
// }
21+
private final CustomOAuth2UserService customOAuth2UserService;
22+
23+
public SecurityConfig(CustomOAuth2UserService customOAuth2UserService) {
24+
this.customOAuth2UserService = customOAuth2UserService;
25+
}
26+
27+
@Bean
28+
protected SecurityFilterChain config(HttpSecurity http) throws Exception {
29+
// ๊ถŒํ•œ ์„ค์ •
30+
http
31+
.cors(withDefaults()) // CORS ์„ค์ • ํ™œ์„ฑํ™”
32+
.csrf(csrf -> csrf.disable())
33+
.authorizeHttpRequests((authorizeHttpRequests) -> authorizeHttpRequests
34+
.requestMatchers(new AntPathRequestMatcher("/**")).permitAll())
35+
;
36+
37+
//๋กœ๊ทธ์ธ ๊ด€๋ จ ์„ค์ •
38+
http
39+
.oauth2Login(oauth2 -> oauth2
40+
.loginPage("/login") // ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ์ง€์ •
41+
.successHandler((request, response, authentication) -> {
42+
// ์ธ์ฆ ์ •๋ณด๊ฐ€ SecurityContext์— ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์„ ๋ณด์žฅ
43+
SecurityContextHolder.getContext().setAuthentication(authentication);
44+
45+
CustomOAuth2User customUser = (CustomOAuth2User) authentication.getPrincipal();
46+
47+
// ๋””๋ฒ„๊น…: authentication ์ •๋ณด ํ™•์ธ
48+
System.out.println("Authentication: " + authentication);
49+
System.out.println("Principal: " + authentication.getPrincipal());
50+
51+
if (authentication != null && authentication.getPrincipal() != null) {
52+
//์ธ๊ฐ€๊ฐ€ ์žˆ์œผ๋ฉด ์œ ์ € ์ •๋ณด๋ฅผ ์ €์žฅ
53+
OAuth2User principal = (OAuth2User) authentication.getPrincipal();
54+
String username = principal.getAttribute("login");
55+
56+
// ์„ธ์…˜์— ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์ถ”๊ฐ€
57+
request.getSession().setAttribute("user", username);
58+
59+
response.sendRedirect("/"); // ๋กœ๊ทธ์ธ ์„ฑ๊ณต ํ›„ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ
60+
} else {
61+
// ์ธ์ฆ ์‹คํŒจ ์‹œ ์ฒ˜๋ฆฌ
62+
response.sendRedirect("/login?error=authenticationFailed");
63+
}
64+
})
65+
)
66+
.sessionManagement(session -> session
67+
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) // ์„ธ์…˜ ์ •์ฑ… ์„ค์ •
68+
.invalidSessionUrl("/login?error=invalidSession") // ์„ธ์…˜์ด ์œ ํšจํ•˜์ง€ ์•Š์œผ๋ฉด ์ด๋™ํ•  URL
69+
.maximumSessions(1) // ํ•˜๋‚˜์˜ ๊ณ„์ •์œผ๋กœ ํ•œ ๋ฒˆ์— ๋กœ๊ทธ์ธํ•  ์ˆ˜ ์žˆ๋„๋ก ์ œํ•œ
70+
.expiredUrl("/login?error=sessionExpired") // ์„ธ์…˜ ๋งŒ๋ฃŒ ํ›„ ์ด๋™ํ•  URL ์„ค์ •
71+
);
72+
73+
//๋กœ๊ทธ์•„์›ƒ ๊ด€๋ จ ์„ค์ •
74+
http
75+
.logout(logout -> logout
76+
.logoutUrl("/logout") // ๋กœ๊ทธ์•„์›ƒ URL ์„ค์ •
77+
.logoutSuccessUrl("/") // ๋กœ๊ทธ์•„์›ƒ ์„ฑ๊ณต ํ›„ ์ด๋™ํ•  URL
78+
.invalidateHttpSession(true) // ๋กœ๊ทธ์•„์›ƒ ์‹œ ์„ธ์…˜ ๋ฌดํšจํ™”
79+
.clearAuthentication(true) // ์ธ์ฆ ์ •๋ณด ์ง€์šฐ๊ธฐ
80+
.deleteCookies("JSESSIONID") // ์„ธ์…˜ ์ฟ ํ‚ค ์‚ญ์ œ
81+
);
82+
http
83+
.csrf(
84+
AbstractHttpConfigurer::disable // CSRF ๋ณดํ˜ธ ๋น„ํ™œ์„ฑํ™”
85+
);
86+
87+
return http.build();
88+
}
7689
}

0 commit comments

Comments
ย (0)