๐ŸŒธ [SPRING BOOT] | ์„ธํ๋ฆฌํ‹ฐ๊ตฌ์กฐ, SecurityConfig , ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ๋ฆฌ๋‹ค๋ ‰์…˜ ์š”์ฒญ์ด ๋„ˆ๋ฌด ๋งŽ์Šต๋‹ˆ๋‹ค ๋ฌดํ•œ๋ฃจํ”„

0
post-thumbnail

  • ์ด ๊ธ€์—์„œ csrf์— ๋Œ€ํ•œ ๋ถ€๋ถ„์€ Reiphiel๋‹˜์˜ Spring Security๋กœ CSRF ํ”„๋กœํ…์…˜ ์ ์šฉ๊ธ€์—์„œ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค.

์ง€์‹๊ณผ ๊ฟ€ํŒ์ด ๋„˜์ง€๋Š” "๋ ˆ์ดํ”ผ์—˜์˜ ๋ธ”๋กœ๊ทธ" ๋ฐฉ๋ฌธํ•˜๊ธฐ

"Spring Security๋กœ CSRF ํ”„๋กœํ…์…˜ ์ ์šฉ" ๊ธ€ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ


๐ŸŸฆ ์‹œํ๋ฆฌํ‹ฐ ์šฉ์–ด

โœ… Authentication ๋กœ๊ทธ์ธ

โœ… Authorization ๊ถŒํ•œ

๐ŸŸฆ ์‹œํ๋ฆฌํ‹ฐ ๊ตฌ์กฐ

unknown

  • 1 (Http Request) ๐Ÿ‘‰ 2 (UsernamePasswordAuthentication Token)
    : id์™€ pw๋ฅผ ๋ถ„๋ฆฌํ•˜๊ณ  ๊ทธ ๊ฐ’๋“ค์„ ๊ฐ–๊ณ  ์žˆ๋‹ค.
  • 3 (AuthenticationManager)
    :1 (Http Request)๊ณผ 4 (AuthenticationProvider)๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์ฝ˜์„ผํŠธ ์—ญํ• 
  • 3 (AuthenticationManager)์„ ์ƒ์†๋ฐ›์•„์„œ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด ProviderManager
  • 4 (AuthenticationProvider)
    : ์ธ์ฆ ์—ญํ•  ๐Ÿ‘‰ ๋กœ๊ทธ์ธ๊ณผ ๊ด€๋ จ๋จ
  • 5 (UserDetailsService)
    : ๋กœ๊ทธ์ธ์ฒ˜๋ฆฌ
  • 5 (UserDetailsService) ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ์นœ๊ตฌ๊ฐ€ ์ฒ˜๋ฆฌ
  • loaduserBy()๋ฉ”์†Œ๋“œ๊ฐ€ ๊ผญ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
  • loaduserBy()๋ฉ”์†Œ๋“œ์—๊ฒŒ id๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
  • 4 (AuthenticationProvider)๊ฐ€ loaduserBy()๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ๊ณ ๋‚˜์„œ
  • ๊ฒฐ๊ตญ ๋„˜์–ด์˜ค๋Š” ํƒ€์ž…์€ 6 (UserDetails)์ด๋‹ค.
  • loaduserBy()๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ์–ด๋–ป๊ฒŒ ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ ํ•ด์ค„์ง€ ๋กœ์ง์„ ์—ฌ๊ธฐ์— ์ ์–ด์ค€๋‹ค.
  • 6 (UserDetails)์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค๊ฐ€ ์ฒ˜๋ฆฌํ•ด์ฃผ๊ณ 
  • ๊ฒฐ๊ณผ๋ฌผ์„ 7 (AuthenticationProvider)๋ฒˆ์œผ๋กœ,
  • ๊ทธ๋ฆฌ๊ณ  8 (AuthenticationManager)์œผ๋กœ,
  • ๊ทธ๋ฆฌ๊ณ  9 (AuthenticationFilter)์œผ๋กœ,
  • ๊ทธ๋ฆฌ๊ณ  ์ตœ์ข…๊ฒฐ๊ณผ๊ฐ€ 10 (SecurityContextHolder)๋กœ ์˜จ๋‹ค.
  • ์šฐ๋ฆฌ๋Š” 6 (UserDetails)๋งŒ ์ž˜ํ•˜๋ฉด ๋œ๋‹ค.
  • 10 (SecurityContextHolder)์ด ํƒ€์ž„๋ฆฌํ”„์—์„œ #autheication์„ ๋‹ด๋‹นํ•œ๋‹ค.
  • ํƒ€์ž„๋ฆฌํ”„์—์„œ #autheication์—์„œ getPrinciple()ํ•˜๋ฉด
  • 6 (UserDetails)์˜ ๊ฐ์ฒด๊ฐ€ ์˜จ๋‹ค.
  • 10 (SecurityContextHolder)์€ ์ธ์ฆ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
    : ๋กœ๊ทธ์ธ์— ์„ฑ๊ณต ๋˜์–ด์žˆ๋ƒ์˜ ์—ฌ๋ถ€
  • ํƒ€์ž„๋ฆฌํ”„๋‚˜ ์Šคํ”„๋ง์ด 10 (SecurityContextHolder)์— ๋ฌผ์–ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.
  • ๊ทธ๋ž˜์„œ ์ตœ์ข…๊ฒฐ๊ณผ๊ฐ€ 10 (SecurityContextHolder)์œผ๋กœ ์˜ค๋Š” ๊ฒƒ์ด๋‹ค.

๐ŸŸฆ HttpSecurity csrf

โœ… ์‚ฌ์ดํŠธ ๊ฐ„ ์š”์ฒญ ์œ„์กฐ

  • ์ตœ๊ทผ์˜ Java๊ธฐ๋ฐ˜์˜ ์›น ํ”„๋กœ์ ํŠธ๋Š” ๋Œ€๋ถ€๋ถ„ Spring(์Šคํ”„๋ง) ํ”„๋ ˆ์ž„์›Œํฌ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„๋˜๊ธฐ ๋•Œ๋ฌธ์—
  • ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ Spring Security๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ณด์•ˆ๊ด€๋ จ ๊ธฐ๋Šฅ๋“ค์„ ๊ตฌํ˜„ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  • ๊ทธ์ค‘ CSRF(Cross Site Request Forgery : ์‚ฌ์ดํŠธ ๊ฐ„ ์š”์ฒญ ์œ„์กฐ)๋Š” ๊ฐœ๋ฐœ์ค‘์—๋Š” ๋งค์šฐ ๊ท€์ฐฎ์€ ์กด์žฌ์ž…๋‹ˆ๋‹ค.
  • ์›น์—์„œ CSRF ํ”„๋กœํ…์…˜์€ ๋ณด์•ˆ ๋Œ€์ฑ…์œผ๋กœ ๊ฑฐ์˜ ํ•„์ˆ˜์ ์œผ๋กœ ์š”๊ตฌ๋˜์ง€๋งŒ
  • ๊ฐœ๋ฐœ ํŽธ์˜์„ฑ ๋ฌธ์ œ๋กœ ๊ฐœ๋ฐœ์ค‘์—๋Š” ์ „ํ˜€ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š๊ฑฐ๋‚˜ ๋น„ํ™œ์„ฑํ™”ํ•ด๋‘๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ฒฐ๊ตญ์—๋Š” ๋ณด์•ˆ์ ๊ฒ€์„ ํ†ตํ•ด ์ง€์ ๋ฐ›๊ณ  ์ผ๊ด„์ ์œผ๋กœ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
  • ํ•˜์ง€๋งŒ ๊ธ‰ํ•˜๊ฒŒ ๊ณ ์นœ ์ฝ”๋“œ๋Š” ํ•ญ์ƒ(?) ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚ต๋‹ˆ๋‹ค.
  • ๋˜ํ•œ ์šด์˜์ค‘์—๋„ ์ž˜๋ชป๋œ ์ˆ˜์ •์œผ๋กœ ๋ฐฐํฌํ›„์— ์˜ˆ์™ธ๋ฅผ ๋งŒ๋‚˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๐ŸŸฆ AntPathRequestMatcher

  • AntPathRequestMatcher ๋Š”
  • AntPathRequestMatcher ์— ์„ค์ •ํ•œ ์ž์›์ •๋ณด์™€
  • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญํ•œ ์ž์›์ •๋ณด๋ฅผ
  • ์„œ๋กœ ๋น„๊ตํ•ด์„œ ์ฐธ์ธ์ง€ ๊ฑฐ์ง“์ธ์ง€ ์•Œ๋ ค์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํด๋ž˜์Šค

๐ŸŸฆ configure(AuthenticationManagerBuilder auth)

โœ… configure(AuthenticationManagerBuilder auth)

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetails).passwordEncoder(passwordEncoder());
    }
  • ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’๊ณผ DB์˜ ๊ฐ’์„ ๋น„๊ตํ•ด์„œ ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค
  • POST๋กœ ๋“ค์–ด์˜จ ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ๋ˆ„๊ฐ€ ์ฒ˜๋ฆฌํ• ๊นŒ์š”?
  • UserDetailsService์™€ PasswordEncoder๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์ •ํ•ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค!
  1. DB์—์„œ ์•„์ด๋””๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์กฐํšŒํ•œ๋‹ค (=๋น„๋ฐ€๋ฒˆํ˜ธ ํฌํ•จ)
  2. ์ž…๋ ฅ๋ฐ›์€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ธ์ฝ”๋”ฉํ•œ๋‹ค
  3. ์ธ์ฝ”๋”ฉํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ์‚ฌ์šฉ์ž ์ •๋ณด์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋น„๊ตํ•œ๋‹ค

๐ŸŸฆ configure(WebSecurity web)

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/css/**", "/js/**", "/img/**", "/error", "favicon.ico", "/resources/**");
}
  • /error : ์‚ฌ์šฉ์ž๋งˆ๋‹ค ์ฒ˜์Œ ๋กœ๊ทธ์ธํ•˜๊ณ  ๋‚˜๋ฉด /error์š”์ฒญ์ด ์–ด๋”˜๊ฐ€์—์„œ ์ƒ๊ฒจ์„œ ์ด๊ฒƒ์„ ๋ฌด์‹œํ•˜๋ผ๊ณ  ํ•ด์„œ ๊ทธ๋ƒฅ ์“ฐ๋Š” ๊ฑฐ๋‹ค
  • ์ •ํ™•ํ•˜๊ฒŒ๋Š” ์ž˜ ๋ชจ๋ฅธ๋‹ค.
  • ๊ทธ๋ƒฅ ์ด๋ ‡๊ฒŒ ์“ฐ๋ฉด ์—๋Ÿฌ ์•ˆ๋œฌ๋Œ€์„œ ์“ด๋‹ค๊ณ  ํ•œ๋‹ค
  • favicon.ico : ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ €๋Š” title์— ์žˆ๋Š” ํ™ˆํŽ˜์ด์ง€ ๊ณ ์œ ์˜ ์ด๋ชจ๋””์ฝ˜์„ ๊ผญ ๋‹ฌ๋ผ๊ณ  ํ•ด์„œ ์ค€๊ฒƒ์ด๋‹ค
  • resources๋Š” ๋‚˜์ค‘์—๋Š” ๋บ๋‹ค ์ด ์š”์ฒญ์€ ์•ˆ ์˜ฌ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ํ•จ

๐ŸŸฆ ๋ฆฌ๋‹ค๋ ‰์…˜ ์š”์ฒญ์ด ๋„ˆ๋ฌด ๋งŽ์Šต๋‹ˆ๋‹ค

๐Ÿ”น ์—๋Ÿฌ๋ฉ”์‹œ์ง€

๋‹ค์šด๋กœ๋“œ

๐Ÿ”น ์›์ธ

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/css/**", "/js/**", "/img/**", "/error", "favicon.ico", "/resources/**");
    }
  • "/resources/**"์—ฌ๊ธฐ์„œ ์˜คํƒ€๋ฅผ ๋ƒˆ๊ธฐ ๋–„๋ฌธ์ด๋‹ค.
profile
๋ช‡ ๋ฒˆ์„ ๋„˜์–ด์ ธ๋„ ์•ž์œผ๋กœ ๊ณ„์† ๋‚˜์•„๊ฐ€์ž

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