λ μ΄μ΄ λ¨μλ‘ λͺ¨λνν΄λ³Έ νλ‘μ νΈμμ μ΄μ μ λ§λ€μ΄λ΄€λ κΈλ‘λ² μλ΅μ²λ¦¬ κ΄λ ¨ μ½λλ₯Ό μ μ©νλλ°, μλ‘μ΄ λ¬Έμ λ₯Ό λ°κ²¬νλ€.
common λͺ¨λμ μλ΅μ²λ¦¬ μ½λλ₯Ό λͺ°μλ£μλλ°, μμ‘΄μ± import λ¬Έμ λλ¬Έμ securityμμ μ‘μμ€ ν΄λμ€(BadCredentialsException)κ° μΈμμ΄ μλλ λ¬Έμ μλ€.
μκ°ν΄λ³΄λ GlobalExceptionHandlerλ @RestControllerAdvice
λ₯Ό μ¬μ©ν΄μ λ°μ μ΅μ
μ
μ μΊμΉν΄μ£Όλλ° μνλ¦¬ν° λ°μ μλ¬λ νν° λ¨μμ μ‘νκΈ° λλ¬Έμ @ExceptionHandler
μλ μμ‘νλκ² λ§μ§ μλκ° νλ μκ°μ΄ λ²λ©μλ€.
μμ보μ
BadCredentialsException
, UsernameNotFoundException
, AccessDeniedException
λ± μμ²
β
[Security Filter Chain] // μ¬κΈ°μ μνλ¦¬ν° μμΈ λ°μ (BadCredentialsException κ°μ μ λ€)
β
DispatcherServlet
β
Controller
β
@ExceptionHandler or @ControllerAdvice
μλΈλ¦Ώ λμ€ν¨μ²λ³΄λ€ μλ¨μ μμΉν νν°μ μν΄
@ExceptionHandler
or@ControllerAdvice
μ λλ¬νμ§ μλλ€.
@Controller
λ΄λΆμμ λ°μνλ μμΈλ@RestControllerAdvice
μ μν΄ μ‘νμ§λ§,
BadCredentialsException
,UsernameNotFoundException
,AccessDeniedException
μ΄λ° μ λ€μSpring Security Filter
μμμ λ°μνκΈ° λλ¬Έμ μμΈκ° ν°μ§ μμ μ 컨νΈλ‘€λ¬κ° μμ§ νΈμΆλκΈ° μ μ΄λ@ExceptionHandler
λ‘ λͺ»μ‘λλ€
Spring Security κ΄λ ¨ μμΈ νΉλ³ν 컀μ€ν ν΄μ μ²λ¦¬νλ©΄
컀μ€ν ν νΈλ€λ¬λ€μ
securityConfig
μ λ±λ‘ν΄μ, νν°μμ 걸릴 λ μνλ νμμΌλ‘ μ‘μμ€λ³Ό μ μκ² λ€λ μμ΄λμ΄λ¨, μλ΄λ νΉλ³ν νμ λλ©μΈμ΄ μ‘΄μ¬νλ module-api μͺ½μ λ°°μΉ