Login์ด ์ฑ๊ณตํ์ ๋ ํ์ฒ๋ฆฌ ํ๋ ๋ฐฉ๋ฒ
Spring Security๋ฅผ ์ฌ์ฉํ๋ค๋ฉด ๋ก๊ทธ์ธ์ ์์ฝ๊ฒ ๊ตฌํํ ์ ์๋ค.
๊ทธ๋ฌ๋ ๋ก๊ทธ์ธ ์ดํ, ํ ํฐ์ ๋ฐ๊ธํ๋ค๋๊ฐ ํ๋ ์ถ๊ฐ์ ์ธ ์ํฉ์ด ํ์ํ ๊ฒฝ์ฐ ์ด๋ป๊ฒ ํด๊ฒฐํ ์ ์๋์ง ์์๋ณด๊ฒ ๋ค.
AuthenticationSuccessHandler ๊ตฌํ
ํ์ฒ๋ฆฌ ๋ฐฉ๋ฒ์ ๋งค์ฐ ๊ฐ๋จํ๋ฐ, Spring Security์ ์กด์ฌํ๋ AuthenticationSuccessHandler ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ธฐ๋ง ํ๋ฉด ๋๋ค.
AuthenticationSuccessHandler ์ ์ด 2๊ฐ์ ๋ฉ์๋๋ก ์ด๋ฃจ์ด์ ธ ์๊ณ ๊ทธ ์ค ํ๋๋ default ๋ฉ์๋์ด๋ค.
์ด ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ Handler ํด๋์ค๋ฅผ ์๋กญ๊ฒ ์์ฑํ์.
@Slf4j
@Component
public class CustomLogInSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
log.info("๋ก๊ทธ์ธ์ ์ฑ๊ณตํ์ต๋๋ค. ํ ํฐ์ ๋ฐ๊ธํฉ๋๋ค.");
}
}
์ดํ Security Config๋ฅผ ์ค์ ํด์ฃผ๋ ๊ณณ(WebSecurityConfigurerAdapter)๋ฅผ ์์๋ฐ์ ๊ณณ์์successHandler๋ฅผ ๋ฑ๋กํด์ฃผ์.
@RequiredArgsConstructor
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final CustomLogInSuccessHandler customLogInSuccessHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.successHandler(customLogInSuccessHandler);
}
}
OAuth2๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ
๋ง์ฐฌ๊ฐ์ง๋ค.
@RequiredArgsConstructor
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final CustomLogInSuccessHandler customLogInSuccessHandler;
private final CustomOAuth2UserService customOAuth2UserService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.successHandler(customLogInSuccessHandler)
.and()
.oauth2Login()
.successHandler(oAuth2LoginSuccessHandler)
.userInfoEndpoint()
.userService(customOAuth2UserService);
}
}
oauth2Login ์ดํ์ successHandler๋ฅผ ๋ฑ๋ก์์ผ ์ฃผ์.์ด๋ userInfoEndpoint().userService()๊ฐ ๋จผ์ ์คํ๋๊ณ , ๊ทธ ์ดํ์ successHandler()๊ฐ ์คํ๋๋ค๋ ๊ฒ๋ ์์๋์!
๐ Reference