SecurityBuilder / SecurityConfigurer ๊ทธ๋ฆฌ๊ณ  SecurityFilterChain

์ •๋ฏผ๊ตยท2024๋…„ 7์›” 11์ผ
0

spring security

๋ชฉ๋ก ๋ณด๊ธฐ
2/13
post-thumbnail

๐Ÿ“’์ฐธ๊ณ 

๋ณธ Spring Security ์‹œ๋ฆฌ์ฆˆ ๋‚ด์šฉ์€ ์ธํ”„๋Ÿฐ ์ •์ˆ˜์›๋‹˜์˜ ๊ฐ•์˜ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ์‹œ๋ฆฌ์ฆˆ์ž…๋‹ˆ๋‹ค.

์ƒ์—…์  ์ด์šฉ ๋ชฉ์ ์˜ ํฌ์ŠคํŒ… ๋ฌด๋‹จ ๋ณต์ œ ๋ฐ ์‚ฌ์šฉ์„ ๊ธˆํ•ฉ๋‹ˆ๋‹ค.

๊ฐ•์˜ ์ฃผ์†Œ: https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EC%8B%9C%ED%81%90%EB%A6%AC%ED%8B%B0-%EC%99%84%EC%A0%84%EC%A0%95%EB%B3%B5#

๐Ÿ“’SecurityBuilder์™€ SecurityCofigurer

SecurityBuilder์™€ SecurityConfigurer ๋‘˜ ๋‹ค ์ธํ„ฐํŽ˜์ด์Šค๋‹ค.

SecurityBuilder ๊ฐ€ SecurityConfigurer๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๊ณ , ์ธ์ฆ ๋ฐ ์ธ๊ฐ€์˜ ์ดˆ๊ธฐํ™” ์ž‘์—…์€ SecurityConfigurer์— ์˜ํ•ด ์ง„ํ–‰๋œ๋‹ค.

โœ”๏ธSecurityBuilder

๋นŒ๋” ํด๋ž˜์Šค๋กœ์„œ ์›น ๋ณด์•ˆ์„ ๊ตฌ์„ฑํ•˜๋Š” ๋นˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

๋Œ€ํ‘œ์ ์ธ ๊ตฌํ˜„์ฒด๋กœ WebSecurity, HttpSecurity, AuthenticationManagerBuilder๊ฐ€ ์žˆ๋‹ค.

โœ”๏ธSecurityConfigurer

Http ์š”์ฒญ์— ๋Œ€ํ•œ ๋ณด์•ˆ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ํ•„๋”๋“ค์„ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™” ์„ค์ •์— ๊ด€์—ฌํ•œ๋‹ค.

SecurityConfigurer ์ธํ„ฐํŽ˜์ด์Šค๋Š” init๊ณผ configure ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ๊ฐ ๋ฉ”์†Œ๋“œ๋Š” SecurityBuilder ํƒ€์ž…์˜ ์ธ์ˆ˜๋ฅผ ๋ฐ›๋Š”๋‹ค.

๐Ÿ“’ํ๋ฆ„

Spring Security Starter ๋ชจ๋“ˆ์„ ์˜์กด์„ฑ์— ์ถ”๊ฐ€ํ•˜๊ณ  ์‹คํ–‰ํ•˜์—ฌ ํ๋ฆ„์„ ์‚ดํŽด๋ณธ๋‹ค.

โœ”๏ธHttpSecurityConfiguration

HttpSecurityConfiguration ํด๋ž˜์Šค์— ์žˆ๋Š” httpSecurity ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด HttpSecurity ๋นˆ์„ ์ƒ์„ฑํ•œ๋‹ค.

httpSecurity ๋นˆ ์ƒ์„ฑ ๋ฉ”์†Œ๋“œ์— @Scope ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ดํŽด๋ณด๋ฉด prototype์œผ๋กœ ๋ช…์‹œ๋˜์–ด ์žˆ๋‹ค.

์ด ๋ฉ”์†Œ๋“œ๊ฐ€ ์‹คํ–‰๋  ๋•Œ๋งˆ๋‹ค HttpSecurity ๋นˆ์ด ์ƒ์„ฑ๋œ๋‹ค. ์ฆ‰ singleton์œผ๋กœ ๊ด€๋ฆฌ๋˜๋Š” ๋นˆ์ด ์•„๋‹ˆ๋‹ค.

httpSecurity ๋ฉ”์†Œ๋“œ์—์„œ AuthenticationManagerBuilder๋ฅผ ์ƒ์„ฑํ•˜๊ณ , HttpSecurity๋„ ์ƒ์„ฑํ•œ๋‹ค.

๊ทธ ๋ฐ‘์˜ http(HttpSecurity)๋ฅผ ํ†ตํ•ด์„œ ๊ธฐ๋ณธ ๋ณด์•ˆ ์„ค์ • ์ดˆ๊ธฐํ™” ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ๋‹ค.

http.csrf(withDefaults)

withDefaults ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ csrf ๋ณด์•ˆ ์ดˆ๊ธฐํ™” ์ž‘์—…์„ ์ง„ํ–‰ํ•œ๋‹ค.

withDefaults ๋ฉ”์†Œ๋“œ๋Š” ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค์ธ Customizer์˜ static ๋ฉ”์†Œ๋“œ์ด๋‹ค.

withDefaults ๋ฉ”์†Œ๋“œ๋Š” ์ œ๋„ค๋ฆญ ๋ฉ”์†Œ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ช…์‹œ์ ์œผ๋กœ ์ œ๋„ค๋ฆญ ํƒ€์ž…์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด HttpSecurity์˜ csrf ๋ฉ”์†Œ๋“œ ์ธ์ž๊ฐ’์„ ํ†ตํ•ด ์ถ”๋ก ํ•œ๋‹ค.

์ฆ‰, ์—ฌ๊ธฐ์„œ ์ œ๋„ค๋ฆญ ํƒ€์ž…์€ csrf ๋ฉ”์†Œ๋“œ์˜ ์ธ์ž๊ฐ’์„ ์ถ”๋ก ํ•˜์—ฌ Customizer<CsrfConfigurer<HttpSecurity> ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

CsrfConfigurer ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  getOrApply ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•œ CsrfConfigurer๋ฅผ ์ ์šฉํ•œ๋‹ค. CsrfConfigurer๋Š” SecurityConfigurer๋ฅผ ๊ตฌํ˜„ํ•œ ๊ตฌํ˜„์ฒด๋‹ค.

์ด๋Ÿฐ์‹์œผ๋กœ ๋‚˜๋จธ์ง€๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ƒ์„ฑํ•œ configurer๋“ค์„ ๋‹ค ์ ์šฉํ•œ ํ›„์— HttpSecurity๋ฅผ ๊ทธ๋Œ€๋กœ returnํ•˜๋ฉด์„œ HttpSecurity ๋นˆ์„ ๋“ฑ๋กํ•œ๋‹ค.

โœ”๏ธSpringBootWebSecurityConfiguration

๊ทธ ์ดํ›„์— ์ €๋ฒˆ์— ์‚ดํŽด๋ดค๋˜ SpringBootWebSecurityCongiruation ํด๋ž˜์Šค์˜ defaultSecurityFilterChain ๋ฉ”์†Œ๋“œ๋กœ ๋„˜์–ด์˜จ๋‹ค.

์—ฌ๊ธฐ์„œ HttpSecurity ๋นˆ์„ ์ฃผ์ž…๋ฐ›๊ณ , ์ถ”๊ฐ€์ ์œผ๋กœ Configurer๋“ค์„ ์ ์šฉํ•œ ํ›„ build ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  return ํ•˜์—ฌ SecurityFilterChain ๋นˆ์„ ๋“ฑ๋กํ•œ๋‹ค.

http.build()

HttpSecurity์˜ build ๋ฉ”์†Œ๋“œ๋Š” ์œ„์™€ ๊ฐ™์ด doBuild ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

doBuild ๋ฉ”์†Œ๋“œ๋Š” init๊ณผ configure ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  performBuild ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ๋ฅผ return ํ•˜๊ณ  ์žˆ๋‹ค.

init๊ณผ configure ๋ฉ”์†Œ๋“œ์˜ ๊ตฌํ˜„์€ ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ฒผ๋‹ค.

SecurityConfigurer๋“ค์„ ๊บผ๋‚ด์„œ SecurityConfigurer์˜ init์„ ํ•œ ๋ฒˆ์”ฉ ํ˜ธ์ถœํ•˜๊ณ , configure๋ฅผ ํ•œ ๋ฒˆ์”ฉ ํ˜ธ์ถœํ•˜์—ฌ ๊ฐ SecurityConfigurer์— ํ•ด๋‹นํ•˜๋Š” Filter๋“ค์„ ์ƒ์„ฑํ•œ๋‹ค.

performBuild ๋ฉ”์†Œ๋“œ๋Š” ์ตœ์ข…์ ์œผ๋กœ DefualtSecurityFilterChain์„ ์ƒ์„ฑํ•ด์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ ์ตœ์ข…์ ์œผ๋กœ SecurityFilterChain์ด ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋œ๋‹ค.

profile
๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž

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