๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

Spring Security์—์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๊ฐ€ ControllerAdvice์—์„œ ํ•ธ๋“ค๋ง๋˜๋Š” ๋ฌธ์ œ

by young-ji 2024. 4. 25.

Spring Security์—์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๊ฐ€ ControllerAdvice์—์„œ ํ•ธ๋“ค๋ง๋˜๋Š” ๋ฌธ์ œ

 

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฐœ์ƒํ•œ Exception์ด ControllerAdvice์— ํ•ธ๋“ค๋ง๋˜๋Š” ๊ฒƒ์ด ๋ญ๊ฐ€ ๋ฌธ์ œ์ผ๊นŒ์š”?

 

 

์ตœ๊ทผ์— Client์—๊ฒŒ ์ „๋‹ฌํ•˜๋Š” error ๋ฉ”์„ธ์ง€๋ฅผ ๊ณตํ†ตํ™”ํ•˜๊ธฐ ์œ„ํ•ด Rest Controller Advice์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

	... 
	
	@ExceptionHandler(Exception.class)
	public ResponseEntity<ErrorResponse> handleException(HttpServletRequest request, Exception exception)
		return ResponseEntity.internalServerError()
			.body(ErrorResponse.badRequest(exception.getMessage(), request.getRequestURI()));
	}
	
}

์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์˜ˆ์™ธ ๋ฐœ์ƒ์œผ๋กœ ํ•ด๋‹น Exception๊ณผ ์ผ์น˜ํ•˜๋Š” ์‹œ๊ทธ๋‹ˆ์ฒ˜๋กœ ์ •์˜๋œ ๋ฉ”์„œ๋“œ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ, ๊ณตํ†ต์ ์œผ๋กœ ํ˜ธ์ถœ๋˜๊ฒŒ๋  ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

 

 

๋ชฉํ‘œํ•˜๋˜ error ๋ฉ”์„ธ์ง€๋Š” ๊ณตํ†ตํ™” ํ•  ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ ๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. Security์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ธ์ฆ, ์ธ๊ฐ€ ์˜ค๋ฅ˜๊ฐ€ ํ•ด๋‹น ๋ฉ”์„œ๋“œ์— ์บ์น˜๋˜์–ด 500 ์—๋Ÿฌ๋ฅผ ์ „๋‹ฌํ•œ ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

status code

๋ชจ๋‘๊ฐ€ ์•„์‹œ๋‹ค์‹œํ”ผ ์ธ์ฆ์— ์‹คํŒจํ–ˆ์„ ๊ฒฝ์šฐ์—๋Š” 401 ์—๋Ÿฌ์ฝ”๋“œ๋ฅผ, ์ธ๊ฐ€์— ์‹คํŒจํ–ˆ์„ ๋•Œ๋Š” 403 ์—๋Ÿฌ์ฝ”๋“œ๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

 

Spring Security๋Š” Filter ๋‹จ์—์„œ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋”๋ผ๋„ Spring Context ๋‚ด๋ถ€์— ์žˆ๋Š” ControllerAdvice๊นŒ์ง€ ์ „ํŒŒ๋˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์–ด์งธ์„œ ํ•ด๋‹น ๋ฉ”์„œ๋“œ์— ์บ์น˜๋˜์–ด ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๊ฑธ๊นŒ์š”?

 

Spring Security์˜ ๋™์ž‘ ์œ„์น˜

๐Ÿ’ก ํ•ด๋‹น ๊ฒŒ์‹œ๋ฌผ์—์„œ๋Š” Spring Security์— ๊ด€ํ•œ ์ž์„ธํ•œ ๋™์ž‘์›๋ฆฌ๋ฅผ ์„ค๋ช…ํ•˜๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค! ๐Ÿ™…‍โ™‚๏ธ

 

spring security architecture

 

์œ„๋Š” Spring Security Architecture ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค. ์„œ๋ฒ„๋กœ ๋“ค์–ด์˜จ Http Request๊ฐ€ Controller์— ๋„๋‹ฌํ•˜๊ธฐ ์ „ AuthenticationFilter๊ฐ€ ํ•ด๋‹น ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑ„ ์ธ์ฆ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผSecurityContextHolder์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

๊ทธ ํ›„ ์š”์ฒญ์ด ์ปจํŠธ๋กค๋Ÿฌ์— ๋„๋‹ฌํ•˜๊ธฐ ์ „ SecurityInterceptor๊ฐ€ ์‹คํ–‰๋˜์–ด ๋ณด์•ˆ ๊ทœ์น™์— ๋”ฐ๋ผ ์š”์ฒญ์„ ๊ฒ€์‚ฌํ•˜๊ณ  ์‚ฌ์šฉ์ž์˜ ๊ถŒํ•œ์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ธ๊ฐ€ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฑฐ์ฃ .

 

 

Filter์˜ ์œ„์น˜

 

๊ทธ๋ ‡๋‹ค๋ฉด ์ธ์ฆ, ์ธ๊ฐ€ ๋ชจ๋‘ Controller ๋ฐ–์—์„œ ์‹คํ–‰๋˜๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ControllerAdvice์— ์บ์น˜๊ฐ€ ๋œ ๊ฑธ๊นŒ์š”?

 

๊ทธ ์ด์œ ๋Š” SecurityInterceptor ๊ตฌํ˜„์ฒด์— ์žˆ์Šต๋‹ˆ๋‹ค.

SecurityInterceptor์˜ ๊ตฌํ˜„์ฒด๋Š” FilterSecurityInterceptor ์™€ MethodSecurityInterceptor๋กœ ๋‚˜๋‰˜๋Š”๋ฐ, FilterSecurityInterceptor๋Š” Servlet ํ•„ํ„ฐ์˜ ํ˜•ํƒœ๋กœ ๋™์ž‘ํ•˜์ง€๋งŒ MethodSecurityInterceptor๋Š” Spring์˜ AOP๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์Šคํ”„๋ง ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์•ˆ์—์„œ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

  • FilterSecurityInterceptor
    • HTTP ์š”์ฒญ์— ๋Œ€ํ•œ ์ ‘๊ทผ ์ œ์–ด๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ
    • ๋ณดํ†ต Spring Security์˜ ๋ณด์•ˆ ํ•„ํ„ฐ ์ฒด์ธ์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • URL ํŒจํ„ด๊ณผ ๊ถŒํ•œ ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ์ธํ„ฐ์…‰ํŠธํ•˜๊ณ  ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•˜๊ฑฐ๋‚˜ ๊ฑฐ๋ถ€ํ•ฉ๋‹ˆ๋‹ค. antMatchers() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ URL ํŒจํ„ด๊ณผ ๊ถŒํ•œ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ธํ„ฐ์…‰ํ„ฐ๋Š” ๋ณดํ†ต Spring Security์˜ XML ๋˜๋Š” Java Config๋ฅผ ํ†ตํ•ด ์„ค์ •๋ฉ๋‹ˆ๋‹ค.
  • MethodSecurityInterceptor
    • ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ์— ๋Œ€ํ•œ ์ ‘๊ทผ ์ œ์–ด๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • ์ฃผ๋กœ ๋ฉ”์†Œ๋“œ ์ˆ˜์ค€์˜ ๋ณด์•ˆ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
    • @PreAuthorize, @PostAuthorize, @Secured ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”์†Œ๋“œ ์ˆ˜์ค€์˜ ๋ณด์•ˆ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

 

์ฆ‰, ์šฐ๋ฆฌ๊ฐ€ ํ”ํžˆ ์‚ฌ์šฉํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•œ ๊ถŒํ•œ ์„ ์–ธ์€ Filter๊ฐ€ ์•„๋‹Œ Spring ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด์—์„œ ์ฒ˜๋ฆฌ๋˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

 

 

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

๋‹ค์‹œ ๋ฌธ์ œ๋กœ ๋Œ์•„์™€ Spring Security Exception์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋ ๊นŒ์š”?

 

ControllerAdvice์—์„œ ์บ์น˜๊ฐ€ ๋œ๋‹ค๋ฉด ๋‹ค๋ฅธ ์˜ˆ์™ธ๋“ค๊ณผ ๋™์ผํ•˜๊ฒŒ Exception๊ณผ ์‹œ๊ทธ๋‹ˆ์ฒ˜๊ฐ€ ๋™์ผํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๊ฒ ๋‹ค ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ Spring Context ๋‚ด์—์„œ ๋ฐœ์ƒํ•œ Exception(AccessDeniedException)์€ ์•„๋ž˜ ์ด๋ฏธ์ง€์™€ ๊ฐ™์ด ExceptionTranslationFilter์— ์˜ํ•ด์„œ ์ธ์ฆ ์˜ˆ์™ธ(AuthenticationException)์™€ ์ธ๊ฐ€ ์˜ˆ์™ธ(AccessDeniedException)๋กœ ๋ถ„๊ธฐ๋˜๊ธฐ ๋•Œ๋ฌธ์— Filter๋ฅผ ํƒ€๊ธฐ์ „์— Exception์„ ์ฒ˜๋ฆฌํ•  ๊ฒฝ์šฐ ์›ํ•˜๋Š” ์—๋Ÿฌ ์ƒํƒœ์ฝ”๋“œ์„ ๋ฐ›์ง€ ๋ชป ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

์ œ๊ฐ€ ํ•ด๊ฒฐํ•œ ๋ฐฉ๋ฒ•์€ ํ•ด๋‹น Exception์ด ๊ธฐ์กด ๋กœ์ง๋Œ€๋กœ Filter์—์„œ ๋ถ„๊ธฐ๋  ์ˆ˜ ์žˆ๋„๋ก ๋˜์ง€๋Š” ๊ฒƒ(throw)์ž…๋‹ˆ๋‹ค.

@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<ErrorResponse> handleException(AccessDeniedException exception) {
    throw exception;
}

 

 

spring-projects Github์— ๋™์ผํ•œ ์ด์Šˆ๋ฅผ ์ฐพ์•˜๋Š”๋ฐ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ rethrowํ•˜๊ธฐ๋ฅผ ๊ถŒ์žฅํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

https://github.com/spring-projects/spring-security/issues/6908

 

AccessDeniedHandler and AuthenticationEntryPoint does not work Because of the global exception handler · Issue #6908 · spring-

Summary AccessDeniedHandler and AuthenticationEntryPoint do not work because the global exception handler is defined Actual Behavior When the request has an AuthenticationException or an AccessDeni...

github.com

 

 

 

๋งˆ๋ฌด๋ฆฌ

๋„ˆ๋ฌด ํ—ˆ๋ฌดํ•œ ๊ฒฐ๋ก ์ผ๊นŒ์š”?ใ…Žใ…Ž

์˜ˆ์™ธ๋ฅผ ๊ทธ๋ƒฅ ๋˜์ ธ์•ผํ•˜๋Š”๊ฒŒ ์ฐ์ฐํ•  ์ˆ˜๋Š” ์žˆ์ง€๋งŒ Security์˜ ๊ธฐ๋ณธ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•œ ๋’ค์— custom filter๋ฅผ ๋“ฑ๋กํ•˜์—ฌ ์‘๋‹ต ๋ฉ”์„ธ์ง€๋ฅผ ๊ณตํ†ตํ™”ํ•˜๋Š”๊ฒŒ ์ข‹๊ฒ ๋‹ค๋Š” ๊ฒฐ๋ก ์„ ๋‚ด๋ ธ์Šต๋‹ˆ๋‹ค.

 

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€ ๋‹จ์ˆœ ํ–ˆ์ง€๋งŒ Spring Security๊ฐ€ Filter์—์„œ๋งŒ ๋™์ž‘ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋” ์ข‹์€ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์„ ์•Œ๊ณ  ๊ณ„์‹œ๊ฑฐ๋‚˜ ์ž˜๋ชป๋œ ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์„ธ์š”. ๋๊นŒ์ง€ ๋ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)

 

 

 

Spring MVC ํŒจํ„ด๊ณผ Filter์— ๋Œ€ํ•œ ์„ค๋ช…์ด ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ์•„๋ž˜ ๊ฒŒ์‹œ๊ธ€์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”. 
[Spring] Spring MVC : https://clipcode.tistory.com/62   
[Spring] Filter์™€ Interceptor ์ฐจ์ด : https://clipcode.tistory.com/65

 

 

 

Reference.

https://ugo04.tistory.com/169

 

 

๋Œ“๊ธ€