Spring Security์˜ Filter ๐Ÿ”’

Jimin Limยท2022๋…„ 6์›” 27์ผ
0

Spring

๋ชฉ๋ก ๋ณด๊ธฐ
2/18
post-thumbnail
post-custom-banner

์–ด๋– ํ•œ ์š”์ฒญ์ด ๋“ค์–ด์™”์„ ๋•Œ, ํ—ค๋”์— ๋‹ด๊ธด jwt์˜ ์œ ํšจ์„ฑ ํŒ๋ณ„์„ ํ•˜๋Š” ๋กœ์ง์ด ๊ณตํ†ต์ ์œผ๋กœ ํ•„์š”๋กœ ํ•œ๋‹ค. Spring security์˜ ๊ฒฝ์šฐ์—๋Š” Filter chain์„ ์ œ๊ณตํ•ด ์ด์™€ ๊ฐ™์€ ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
์ด๋•Œ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด์„œ ์œ ํšจ์„ฑ์„ ๊ฒ€์‚ฌํ•˜๋Š” ์œ„์น˜๊ฐ€ ์–ด๋””์ธ์ง€, ๊ตฌ์ฒด์ ์œผ๋กœ ํŒŒ์•…ํ•˜๊ณ ์ž ํ•˜์˜€๋‹ค.

โœ… DispatcherServlet

Front Controller ๋กœ์จ, ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์š”์ฒญ์ด ์˜จ๋‹ค๋ฉด ๋ชจ๋“  ์š”์ฒญ์„ ๋จผ์ € ๋ฐ›๊ณ  ๊ทธ ์š”์ฒญ๋“ค์„ ์„ธ๋ถ€ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์œ„์ž„ํ•œ๋‹ค.

์š”์ฒญ์ด ๋“ค์–ด์™”์„ ๋•Œ์˜ ์ˆœ์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  1. ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์„ ๋””์ŠคํŒจ์ฒ˜ ์„œ๋ธ”๋ฆฟ์ด ๋ฐ›๋Š”๋‹ค.
  2. Handler Mapping์ด request๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ์— ์ ์ ˆํ•œ handler๋ฅผ ์ฐพ๋Š”๋‹ค.
  3. ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์œ„์ž„ํ•  handler adapter๋ฅผ ์ฐพ์•„์„œ ์ „๋‹ฌํ•œ๋‹ค.
  4. handler adapter๊ฐ€ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์š”์ฒญ์„ ์œ„์ž„ํ•œ๋‹ค.
  5. ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง ์ฒ˜๋ฆฌํ•œ๋‹ค.
  6. ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  7. handler adapter๊ฐ€ ๋ฐ˜ํ™˜ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
  8. ์„œ๋ฒ„์˜ ์‘๋‹ต์„ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ ๋ฐ”๋กœ controller๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ , handler adapter๋ผ๋Š” ๊ฒƒ์„ ํ†ตํ•ด controller๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ๊ทธ ์ด์œ ๋Š” @RequestParam, @RequestBody ๋“ฑ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ArgumentResolver๋“ค๊ณผ, ์‘๋‹ต ์‹œ์— ResponsEntity์˜ Body๋ฅผ json์œผ๋กœ ์ง๋ ฌํ™”ํ•˜๋Š” ReturnValueHandler๋“ค์ด ์–ด๋Œ‘ํ„ฐ๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

โœ… Filter vs Interceptor

๊ณตํ†ต ๊ด€์‹ฌ์‚ฌ๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค๋Š” ์ ์—์„œ interceptor์™€ ์œ ์‚ฌํ•ด ํ•จ๊ป˜ ์ •๋ฆฌํ•ด ๋ณด์•˜๋‹ค.

ํ•„ํ„ฐ๋Š” DispatcherServlet ์ด์ „์— ์‹คํ–‰์ด ๋˜๋ฉฐ, Spring ๋ฒ”์œ„ ๋ฐ–์—์„œ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰, ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์•„๋‹Œ ์›น ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ๊ด€๋ฆฌ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

์ธํ„ฐ์…‰ํ„ฐ๋Š” Spring์ด ์ œ๊ณตํ•˜๋Š” ๊ธฐ์ˆ ๋กœ, Dispatcher Servlet์ด ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์ „๊ณผ ํ›„์— ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ„์—์„œ Dicpoatcher Servlet์€ ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘์„ ํ†ตํ•ด ์ ์ ˆํ•œ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ฐพ๋„๋ก ์š”์ฒญํ•œ๋‹ค๊ณ  ํ•˜์˜€๋Š”๋ฐ, ์ด๋•Œ ์ฐพ์€ ํ›„ ์‹คํ–‰ ์ฒด์ธ์ด๋ผ๋Š” ๊ฒƒ์„ ๋Œ๋ ค์ค€๋‹ค. ์ด ์‹คํ–‰ ์ฒด์ธ์€ 1๊ฐœ ์ด์ƒ์˜ ์ธํ„ฐ์…‰ํ„ฐ๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ๋‹ค๋ฉด ์ˆœ์ฐจ์ ์œผ๋กœ ์ธํ„ฐ์…‰ํ„ฐ๋“ค์„ ๊ฑฐ์ณ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ํ•˜๊ณ , ์ธํ„ฐ์…‰ํ„ฐ๊ฐ€ ์—†๋‹ค๋ฉด ๋ฐ”๋กœ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ํ•œ๋‹ค.

๐Ÿ”— ์‚ฌ์šฉ ์šฉ๋„

ํ•„ํ„ฐ๋Š” ์Šคํ”„๋ง๊ณผ ๋ฌด๊ด€ํ•˜๊ฒŒ ์ „์—ญ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ์ž‘์—…๋“ค์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉ ํ•œ๋‹ค. ์ธํ„ฐ์…‰ํ„ฐ๋ณด๋‹ค ์•ž๋‹จ์—์„œ ๋™์ž‘ํ•˜๋ฏ€๋กœ ์ „์—ญ์ ์œผ๋กœ ํ•ด์•ผํ•˜๋Š” ๋ณด์•ˆ ๊ฒ€์‚ฌ๋ฅผ ํ•ด, ์˜ฌ๋ฐ”๋ฅธ ์š”์ฒญ์ด ์•„๋‹ ๊ฒฝ์šฐ ์ฐจ๋‹จ์„ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ด๋Š” ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊นŒ์ง€ ์š”์ฒญ์ด ์ „๋‹ฌ๋˜์ง€ ๋ชปํ•˜๊ณ  ์ฐจ๋‹จ๋˜์–ด ์•ˆ์ •์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

์ธํ„ฐ์…‰ํ„ฐ๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „, ๊ฒ€์‚ฌํ•ด์•ผํ•˜๋Š” ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ์— ์ ํ•ฉํ•˜๋‹ค. ๋˜ํ•œ ์Šคํ”„๋ง๊ณผ ์—ฐ๊ด€์ด ๋งŽ์€ ์ž‘์—…์„ ํ•˜๊ธฐ ์‰ฌ์šฐ๋ฉฐ ์ฃผ๋กœ ๋นˆ ํ˜ธ์ถœ์— ๋Œ€ํ•œ ๋กœ๊น…์ด ์žˆ๋‹ค.

โœ… Spring Security์—์„œ์˜ Filter

์œ„์˜ ๋‚ด์šฉ์„ ์š”์•ฝํ•˜์ž๋ฉด dispatcher servlet์— ๋„๋‹ฌํ•˜๊ธฐ ์ „, ํ•„ํ„ฐ๋ฅผ ๊ฑฐ์ณ์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.
spring security๋Š” ํ•„ํ„ฐ๋“ค์„ ์ฒด์ธ์œผ๋กœ ์—ฎ๋Š” ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๋ฉฐ, DelegatingFilterProxy๋ผ๋Š” ํ•„ํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด ๋ฉ”์ธ Filter Chain์— ๋ผ์›Œ ๋„ฃ๊ณ  ๊ทธ ์•„๋ž˜ SecurityFilterChain ๊ทธ๋ฃน์„ ๋“ฑ๋กํ•˜๋„๋ก ํ•œ๋‹ค.

WebSecurityConfigurerAdapter ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ Filter Chain์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, configure(HttpSecurity http)๋ฅผ overrideํ•ด ํŠน์ • url ๋งคํ•‘์„ ํ†ตํ•œ ๊ถŒํ•œ ์„ค์ •, filter ์„ธํŒ… ๋“ฑ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฐธ๊ณ  ์ž๋ฃŒ

https://mangkyu.tistory.com/18
https://mangkyu.tistory.com/173
https://velog.io/@seongwon97/Spring-Security-Filter%EB%9E%80

profile
๐Ÿ’ป โ˜•๏ธ ๐Ÿ ๐Ÿ‘ ๐Ÿน ๐ŸŠโ€โ™€๏ธ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€