๐ ์์ธ๋ฅผ ํธ๋ค๋ฌ๋ก ์ฒ๋ฆฌํ๊ธฐ (@ExceptionHandler)
๐พ Form ์ผ๋ก ๋ก๊ทธ์ธ ์ ๋ณด ๊ฐ์ฒด๋ก ๋ฐ์์ค๊ธฐ
โซ๏ธ AuthFailureHandler.java
โถ๏ธ ์ธ์ฆ ์คํจ error (๋ก๊ทธ์ธ ์คํจ) ์ฒ๋ฆฌ
public class AuthFailureHandler implements AuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
if(exception.getClass() == UsernameNotFoundException.class || exception.getClass() == BadCredentialsException.class) {
response.sendRedirect("/account/login?error=auth");
}else {
response.sendRedirect("/account/login?error");
}
}
}
๐ฆ AuthenticationFailureHandler๋ Spring Security ์์ฒด ์ธํฐํ์ด์ค๋ก, ๊ตฌํํ์ฌ( โ AuthFilureHandler) SecurityConfig์์ ์ค์ ํด์ฃผ์ด์ ์ฌ์ฉํ๋ค.
โป SecurityConfig ์์ โ failureHandler(new AuthFailureHandler()) ์ค์ ๋ฃ์ด์ค.
์ ๋ ์๋ฌ ๋ฐ์ ์ auth error ์์ ์๋ ค์ค.
โซ๏ธ CustomValidationException.java
โถ๏ธ ์ ํจ์ฑ ๊ฒ์ฌ ์คํจ ์ ๋์์ค ์๋ฌ
@Getter
public class CustomValidationException extends RuntimeException {
private Map<String, String> errorMap;
public CustomValidationException(String message, Map<String, String> errorMap) {
super(message);
this.errorMap = errorMap;
}
}
โซ๏ธ CustomInternalServerErrorException.java
โถ๏ธ ์๋ฒ ์๋ฌ
public class CustomInternalServerErrorException extends Exception {
public CustomInternalServerErrorException(String message) {
super(message);
}
}
โซ๏ธ RestControllerExceptionHandler.java
@RestController
@RestControllerAdvice // โก๏ธ โ๏ธ๋ชจ๋ Controller ์์ ๋ฐ์ํ๋ ์์ธ๋ฅผ ํด๋น ํธ๋ค๋ฌ๋ฅผ ํตํด ์ฒ๋ฆฌํ ์ ์๋๋ก ํจ
public class RestControllerExceptionHandler {
@ExceptionHandler(CustomValidationException.class) // โก๏ธ ํด๋น ์์ธ๊ฐ ๋ฐ์ํ์์ ๋, ํด๋น ๋ฉ์๋๋ก ์ฒ๋ฆฌํจ
public ResponseEntity<?> validationErrorException(CustomValidationException e) {
return ResponseEntity
.badRequest()
.body(new CMRespDto<>(-1, e.getMessage(), e.getErrorMap()));
}
@ExceptionHandler(CustomInternalServerErrorException.class)
public ResponseEntity<?> internalServerErrorException(CustomInternalServerErrorException e) {
return ResponseEntity
.internalServerError()
.body(new CMRespDto<>(-1, e.getMessage(), null));
}
}
Advice ๋ JoinPoint ์์ ์คํํ๋ ๋ก์ง์ผ๋ก, ์ค๊ฐ์ ๋ผ์๋ฃ์ด ์ฌ์ฉํ ๋ถ๊ฐ ๊ธฐ๋ฅ์ ๋งํจ.
@RestControllerAdvice ๋ @ControllerAdvice + @ResponseBody ๊ธฐ๋ฅ
โก๏ธ Controller ์์ ๋ฐ์ํ๋ ์์ธ๋ฅผ ํด๋น ํธ๋ค๋ฌ๊ฐ ๋์์ฑ์ ์ฒ๋ฆฌํ๊ฒ ๋จ.
โ๏ธ ๊ฐ๋ณ Controller ์ ๋ฉ์๋๋ฅผ ์ ์ธํ์ฌ , ํด๋น Controller ์์ ๊ฐ์ ์์ธ๋ฅผ ์ฒ๋ฆฌํ ์๋ ์์.
โซ๏ธ login.js
const registerButton = document.querySelectorAll(".login-button")[0]; // โ ์ ๊ท๊ฐ์
const loginButton = document.querySelectorAll(".login-button")[1]; // โ ๋ก๊ทธ์ธ
registerGoButton.onclick = () => {
location.href = "/account/register";
}
loginButton.onclick = () => {
const loginForm = document.querySelector("form");
// form <email & password>
// ๏ผbutton type="submit" . . .
}
์ฐธ๊ณ ๐ง
custom exception์ ์ธ์ ์จ์ผ ํ ๊น?
@RestControllerAdvice๋ฅผ ์ฌ์ฉํด๋ณด์
@ExceptionHandler๋ฅผ ํตํ ์์ธ์ฒ๋ฆฌ
controller-advice
๐ข ์๊ฐ ๐ฝ
์ค๋์ ์นญ๊ตฌ์ ํจ๊ป ์์ฑ ํํฐ ํ๊ธฐ๋ก ํจ
์น๊ตฌ๊ฐ ์ด์ฌํ ๊ณจ๋ผ์จ ์ํ๋ ๋ฐ๋ก . . .
<์์์ฌ๋ฆฐ ๋ถ๊ฝ, ๋ฐ์์ ๋ณผ๊น? ์์์ ๋ณผ๊น?> ์ 1993๋
๋ฒ์ . .
์ด๋ฐ ๊ฑธ ์ด๋์ ์ฐพ์์จ ๊ฑธ๊น? ๊ฝค๋ ์๊ธด
์ง์ค๋ ฅ ๋ธ๋ฆฌ๋ ์ธ๊ฐ๋ค์ด๊ธฐ ๋๋ฌธ์ ์งง์ ์ํ๋ก ๊ณจ๋ผ์๋ค๊ณ ํ๋ค
๋๋ฏธ๋
ธ ํผ์ 1+1 ๋ฐ์ด๋ผ์ ํผ์ ์ฌ์์ ๋จน์ผ๋ฉด์ ๋ด์ผ์ง (โพโกโ)