2025-04-30
Spring Security์์ ์ธ์ฆ(Authentication)๊ณผ ๊ถํ(Authorization) ์ฒ๋ฆฌ ๊ณผ์ ์์ ๋ฐ์ํ ์ ์๋ ์์ธ ์ํฉ์ ํธ๋ค๋ฌ๋ก ์ธ๋ถํํ์ฌ ์ฒ๋ฆฌํ๋ค.
์ด 6๊ฐ์ ์ปค์คํ ํธ๋ค๋ฌ๋ฅผ ๊ตฌํํ์ฌ ์ธ์ฆ, ์ธ๊ฐ, ๋ก๊ทธ์ธ, ๋ก๊ทธ์์ ์ ๋ฐ์ ํ๋ฆ์ ์ ์ดํ์๋ค.
CustomAuthenticationEntryPoint
๋ฏธ์ธ์ฆ ์ฌ์ฉ์๊ฐ ๋ณดํธ๋ ์์์ ์ ๊ทผํ ๋ ํธ์ถ (401 Unauthorized
)
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException {
log.error("CustomAuthenticationEntryPoint's commence invoke...");
response.sendRedirect(request.getContextPath() + "/login?error=true");
}
}
CustomAccessDeniedHandler
์ธ์ฆ์ ๋์์ง๋ง ๊ถํ์ด ๋ถ์กฑํ ๊ฒฝ์ฐ ํธ์ถ (403 Forbidden
)
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
AccessDeniedException accessDeniedException) throws IOException {
log.error("CustomAccessDeniedHandler's handle invoke...");
response.sendRedirect(request.getContextPath() + "/login?error=true");
}
}
CustomSuccessHandler
๋ก๊ทธ์ธ ์ฑ๊ณต ์ ๋ฉ์ธ ํ์ด์ง๋ก ์ด๋
public class CustomSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException {
log.info("CustomSuccessHandler's onAuthenticationSuccess invoke...");
response.sendRedirect(request.getContextPath() + "/");
}
}
CustomLoginFailureHandler
๋ก๊ทธ์ธ ์คํจ ์ ๋ก๊ทธ์ธ ํ์ด์ง๋ก ๋ฆฌ๋ค์ด๋ ํธ (๋ณด์์ ๋ฉ์์ง ๋ ธ์ถ X)
public class CustomLoginFailureHandler implements AuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException {
log.error("CustomLoginFailureHandler's onAuthenticationFailure invoke...");
response.sendRedirect(request.getContextPath() + "/login?error=true");
}
}
CustomLogoutHandler
๋ก๊ทธ์์ ์์ฒญ ์ ์ธ์ ๋ฌดํจํ ๋ฑ ์ปค์คํ ์์ ์ฒ๋ฆฌ
public class CustomLogoutHandler implements LogoutHandler {
@Override
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
log.info("CustomLogoutHandler's logout invoke...");
HttpSession session = request.getSession(false);
if (session != null) session.invalidate();
}
}
CustomLogoutSuccessHandler
๋ก๊ทธ์์ ์๋ฃ ํ ๋ฉ์ธ ํ์ด์ง๋ก ๋ฆฌ๋ค์ด๋ ํธ
public class CustomLogoutSuccessHandler implements LogoutSuccessHandler {
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException {
log.info("CustomLogoutSuccessHandler's onLogoutSuccess invoke...");
response.sendRedirect(request.getContextPath() + "/");
}
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// CSRF ๋นํ์ฑํ (ํ
์คํธ ์ฉ๋, ์ด์ํ๊ฒฝ์์๋ ๊ผญ ํ์ฑํ!)
http.csrf().disable();
// ๊ถํ ์ฒดํฌ
http.authorizeRequests()
.antMatchers("/", "/join", "/login").permitAll()
.antMatchers("/user").hasRole("USER")
.antMatchers("/manager").hasRole("MANAGER")
.antMatchers("/admin").hasRole("ADMIN")
.anyRequest().authenticated();
// ๋ก๊ทธ์ธ ์ค์
http.formLogin()
.loginPage("/login")
.permitAll()
.successHandler(new CustomSuccessHandler())
.failureHandler(new CustomLoginFailureHandler());
// ๋ก๊ทธ์์ ์ค์
http.logout()
.permitAll()
.addLogoutHandler(new CustomLogoutHandler())
.logoutSuccessHandler(new CustomLogoutSuccessHandler());
// ์์ธ ์ฒ๋ฆฌ ์ค์
http.exceptionHandling()
.authenticationEntryPoint(new CustomAuthenticationEntryPoint())
.accessDeniedHandler(new CustomAccessDeniedHandler());
}
์ฒ๋ฆฌ ์ํฉ | ํธ๋ค๋ฌ | ์ค๋ช |
---|---|---|
์ธ์ฆ๋์ง ์์ ์ฌ์ฉ์ ์ ๊ทผ | CustomAuthenticationEntryPoint | /login?error=true ๋ก ๋ฆฌ๋ค์ด๋ ํธ |
๊ถํ ๋ถ์กฑ ์ ๊ทผ | CustomAccessDeniedHandler | /login?error=true ๋ก ๋ฆฌ๋ค์ด๋ ํธ |
๋ก๊ทธ์ธ ์ฑ๊ณต | CustomSuccessHandler | / ๋ฉ์ธ ํ์ด์ง๋ก ์ด๋ |
๋ก๊ทธ์ธ ์คํจ | CustomLoginFailureHandler | ์๋ฌ ๋ฉ์์ง ์์ด /login?error=true ๋ก ์ด๋ |
๋ก๊ทธ์์ ์์ฒญ ์ฒ๋ฆฌ | CustomLogoutHandler | ์ธ์ ๋ฌดํจํ ๋ฑ ์ง์ ์์ ๊ฐ๋ฅ |
๋ก๊ทธ์์ ์๋ฃ ์ฒ๋ฆฌ | CustomLogoutSuccessHandler | / ๋ฉ์ธ์ผ๋ก ์ด๋ |