TIL [20240613] - Spring Security

์ด์œค์„ฑยท2024๋…„ 6์›” 13์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
37/51

๐Ÿ“– ์˜ค๋Š˜์˜ ํ•™์Šต ํ‚ค์›Œ๋“œ

Spring Security - Authenication & Authorization

โ“ ํ‚ค์›Œ๋“œ ์„ ํƒ ์ด์œ 

๊ฐœ์ธ ํ”„๋กœ์ ํŠธ ๋ฐ ํŒ€ ํ”„๋กœ์ ํŠธ์—์„œ ์ธ์ฆ ๋ฐ ์ธ๊ฐ€๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์— ๊ธ‰ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๋ณด๋‹ˆ ์ด๊ฒŒ ์–ด๋–ค ์›๋ฆฌ๋กœ ๋™์ž‘ํ•˜๋Š”์ง€ ์ดํ•ด๋ฅผ ์ œ๋Œ€๋กœ ๋ชปํ•œ ์ƒํƒœ๋กœ ๊ตฌํ˜„๋งŒ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— ๊ตฌํ˜„์ด๋‚˜ ๋‚จ๋“ค์—๊ฒŒ ์„ค๋ช… ํ•˜๋ฉด์„œ๋„ ๋ฏธ์ˆ™ํ•จ์ด ๋ฌป์–ด๋‚˜์˜ค๋Š” ๋“ฏ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ๋‹ค๋ฃฌ๋‹คํ•˜๊ณ  ๋ฐ”์˜๋‹ค๋Š” ํ•‘๊ณ„๋กœ ์ง€๋‚˜์ณค๋˜ ๊ฒƒ์„ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ๋ณด๋ คํ•ฉ๋‹ˆ๋‹ค.

๐Ÿง Spring Security

API์— ๊ถŒํ•œ ๊ธฐ๋Šฅ์ด ์—†์œผ๋ฉด, ์•„๋ฌด๋‚˜ ํšŒ์› ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋ฉฐ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ธ์ฆ๋œ ์œ ์ €์—๊ฒŒ๋งŒ API๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์•ผ๋˜๋Š”๋ฐ, ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๊ฐ€
Spring Security์ž…๋‹ˆ๋‹ค.

์Šคํ”„๋ง ํ”„๋ ˆ์ž„ ์›Œํฌ์—์„œ๋Š” ์ธ์ฆ ๋ฐ ๊ถŒํ•œ ๋ถ€์—ฌ๋กœ ๋ฆฌ์†Œ์Šค ์‚ฌ์šฉ์„ ์ปจํŠธ๋กค ํ•  ์ˆ˜ ์žˆ๋Š” Spring Security๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ณด์•ˆ ์ฒ˜๋ฆฌ๋ฅผ ์‰ฝ๊ฒŒ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿง Spring Security ๋™์ž‘ ์›๋ฆฌ

  1. ์‚ฌ์šฉ์ž๊ฐ€ ๋กœ๊ทธ์ธ ์ •๋ณด(username, password)๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.
  2. UserPasswordAuthenticationToken์ด ์ƒ์„ฑ๋˜์–ด AuthenicationFilter๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.
  3. AuthenicationFilter๋Š” AuthenicationManager๋กœ ์ธ์ฆ ์ฒ˜๋ฆฌ๋ฅผ ์œ„์ž„ํ•ฉ๋‹ˆ๋‹ค.
  4. AuthenicationManager๋Š” ProviderManager์—๊ฒŒ ์ธ์ฆ ์ฒ˜๋ฆฌ๋ฅผ ์œ„์ž„ํ•˜๊ณ  ProviderManager๋Š” ์ ์ ˆํ•œ AuthenicationProvider๋ฅผ ์„ ํƒํ•˜์—ฌ ์ธ์ฆ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  5. AuthenicationProvider๋Š” PasswordEncoder๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Encodeํ•ฉ๋‹ˆ๋‹ค.
  6. AuthenicationProvider๋Š” UserDetailService๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.
  7. UserDetailsService๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋˜๋Š” ๋‹ค๋ฅธ ์†Œ์Šค์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™€ UserDetails ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  8. AuthenticationProvider๋Š” ์ž…๋ ฅ๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ UserDetails์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ PasswordEncorder๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๊ตํ•˜๊ณ  ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค.
  9. AuthenticationProvider๊ฐ€ ์ธ์ฆ์„ ์ฒ˜๋ฆฌํ•œ ํ›„, ์ธ์ฆ ๊ฒฐ๊ณผ๋Š” ๋‹ค์‹œ AuthenticationManager์—๊ฒŒ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.
  10. AuthenticationManager๋Š” ์ธ์ฆ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ Authentication ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ AuthenticationFilter์—๊ฒŒ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  11. ์ธ์ฆ์— ์„ฑ๊ณตํ•œ ๊ฒฝ์šฐ : AuthenticationFilter๋Š” Authentication ๊ฐ์ฒด๋ฅผ SecurityContext์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋‹ค๋ฅธ ๋ถ€๋ถ„์—์„œ ํ˜„์žฌ ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž์˜ ์ •๋ณด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    ์ธ์ฆ์— ์‹คํŒจํ•œ ๊ฒฝ์šฐ : AuthenicationFilter๋Š” AuthenicationException์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๐Ÿง Spring Security Filter

Spring Security Filter๋Š” Spring Security์˜ ํ•ต์‹ฌ ์ปดํฌ๋„ŒํŠธ ์ค‘ ํ•˜๋‚˜๋กœ, ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ณด์•ˆ์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค. 
Filter๋Š” Servlet Filter๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•˜๋ฉฐ, ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ(request)๊ณผ ๋‚˜๊ฐ€๋Š” ์‘๋‹ต(response)๋ฅผ ๊ฐ€๋กœ์ฑ„์„œ 
๋ณด์•ˆ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿง Spring Security Filter ๋™์ž‘ ์›๋ฆฌ

์‚ฌ์šฉ์ž(User)๊ฐ€ ์š”์ฒญ(Request)๋ฅผ ๋ณด๋‚ด๋ฉด, ๊ทธ ์š”์ฒญ์€ ํ•„ํ„ฐ ์ฒด์ธ์„ ํ†ต๊ณผํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•„ํ„ฐ ์ฒด์ธ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์„œ๋ธ”๋ฆฟ ํ•„ํ„ฐ(Servlet Filter)๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐ ์„œ๋ธ”๋ฆฟ ํ•„ํ„ฐ๋Š” ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑ„์„œ ์ „์ฒ˜๋ฆฌ(pre-processing)์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋•Œ, ํ•„ํ„ฐ๋Š” ์š”์ฒญ์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ํ›„ ํ•„ํ„ฐ๋Š” doFilter() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์š”์ฒญ์„ ๋‹ค์Œ ํ•„ํ„ฐ ๋˜๋Š” ์ตœ์ข… ๋ชฉ์ ์ง€์ธ ์„œ๋ธ”๋ฆฟ(Servlet)์œผ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

์š”์ฒญ์ด ๋ชจ๋“  ํ•„ํ„ฐ๋ฅผ ํ†ต๊ณผํ•˜๋ฉด, ์ตœ์ข…์ ์œผ๋กœ ์„œ๋ธ”๋ฆฟ์ด ์š”์ฒญ์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์„œ๋ธ”๋ฆฟ์€ ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต(Response)๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

์‘๋‹ต์€ ๋‹ค์‹œ ํ•„ํ„ฐ ์ฒด์ธ์„ ์—ญ์ˆœ์œผ๋กœ ํ†ต๊ณผํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ํ•„ํ„ฐ๋Š” ์‘๋‹ต์— ๋Œ€ํ•ด ํ›„์ฒ˜๋ฆฌ(post-processing)๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค. ํ•„ํ„ฐ๋Š” ์‘๋‹ต์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

public void doFilter(
			ServletRequest request,
            ServletResponse response, 
            FilterChain filterChain) throws IOException, ServletException {
		//์—ฌ๊ธฐ์— ์ „์ฒ˜๋ฆฌ
		filterChain.doFilter(request, response);
		//์—ฌ๊ธฐ์— ํ›„์ฒ˜๋ฆฌ
}

๐Ÿง Spring Security Filter ์ฃผ์š” ํ•„ํ„ฐ

  1. SecurityContextPersistenceFilter
    • SecurityContext๋ฅผ ๋กœ๋“œํ•˜๊ณ  ์ €์žฅํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
    • ๊ฐ ์š”์ฒญ๋งˆ๋‹ค SecurityContext๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์š”์ฒญ ์ฒ˜๋ฆฌ ํ›„ SecurityContext๋ฅผ ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  2. UsernamePasswordAuthenticationFilter
    • ์‚ฌ์šฉ์ž ์ด๋ฆ„๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฆ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•„ํ„ฐ์ž…๋‹ˆ๋‹ค.
    • ๋กœ๊ทธ์ธ ํผ์—์„œ ์ œ์ถœ๋œ ์‚ฌ์šฉ์ž์˜ ์ด๋ฆ„๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ธ์ฆ์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์ ‘๊ทผ ๊ถŒํ•œ์ด ์—†์„ ๋•Œ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ํ•„ํ„ฐ์ž…๋‹ˆ๋‹ค.
  3. AnonymousAuthenticationFilter
    • ์ธ์ฆ๋˜์ง€ ์•Š์€ ์‚ฌ์šฉ์ž๋ฅผ ์œ„ํ•œ ์ต๋ช… Authentication์„ ์ƒ์„ฑํ•˜๋Š” ํ•„ํ„ฐ์ž…๋‹ˆ๋‹ค.
    • ์ธ์ฆ๋˜์ง€ ์•Š์€ ์‚ฌ์šฉ์ž๋„ ํŠน์ • ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผ ํ•  ์ˆ˜์žˆ๋„๋ก ํ—ˆ์šฉํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  4. ExceptionTranslationFilter
    • ์ธ์ฆ๊ณผ ๊ถŒํ•œ ๋ถ€์—ฌ ๊ณผ์ •์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•„ํ„ฐ์ž…๋‹ˆ๋‹ค.
    • AccessDeniedException์ด๋‚˜ AuthenticationException ๋“ฑ์˜ ์˜ˆ์™ธ๋ฅผ ์ ์ ˆํ•œ HTTP ์‘๋‹ต์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  5. FilterSecurityInterceptor
    • ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž์˜ ๊ถŒํ•œ์„ ํ™•์ธํ•˜๊ณ  ์ ‘๊ทผ ์ œ์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•„ํ„ฐ์ž…๋‹ˆ๋‹ค.
    • ์š”์ฒญ๋œ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์ ‘๊ทผ ๊ถŒํ•œ์„ ํ™•์ธํ•˜๊ณ , ์ ‘๊ทผ ๊ถŒํ•œ์ด ์—†๋Š” ๊ฒฝ์šฐ AccessDeniedException์„ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.

๐Ÿง Spring Security Filter - OncePerRequestFilter

Spring Web์—์„œ ์ œ๊ณตํ•˜๋Š” ์ถ”์ƒ ํด๋ž˜์Šค๋กœ, ์‚ฌ์šฉ์ž ์ •์˜ ํ•„ํ„ฐ๋ฅผ ๋งŒ๋“ค ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 
์ด ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์œผ๋ฉด ๋งค ์š”์ฒญ๋งˆ๋‹ค ํ•œ ๋ฒˆ์”ฉ ํ•„ํ„ฐ๊ฐ€ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋งค ์š”์ฒญ๋งˆ๋‹ค ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
  • ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ์˜ ํ•„ํ„ฐ ์ฒด์ธ์—์„œ ์ค‘๋ณต ์‹คํ–‰์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค.
  • ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ํ•„ํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ๋ฒˆ ํ˜ธ์ถœ๋˜๋Š” ๊ฒฝ์šฐ, ์ฒซ ๋ฒˆ์งธ ํ˜ธ์ถœ์—์„œ๋งŒ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
  • shouldNotFilter๋กœ ํŠน์ • ์กฐ๊ฑด์—์„œ ํ•„ํ„ฐ๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์€ false๋กœ ๋ชจ๋“  ์š”์ฒญ์— ๋Œ€ํ•ด ํ•„ํ„ฐ๊ฐ€ ์‹คํ–‰๋จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • shouldNotFilterAsyncDispatch()์™€ shouldNotFilterErrorDispatch() ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•˜์—ฌ ๋น„๋™๊ธฐ ์š”์ฒญ๊ณผ ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ์˜ ํ•„ํ„ฐ ์‹คํ–‰ ์—ฌ๋ถ€๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿง API ์ธ์ฆ ๋ฐ ๊ถŒํ•œ ๋ถ€์—ฌ๋ฅผ ์œ„ํ•œ ์ž‘์—… ์ˆœ์„œ

1. ํšŒ์› ๊ฐ€์ž…, ๋กœ๊ทธ์ธ API ๊ตฌํ˜„
2. ๋ฆฌ์†Œ์Šค ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ROLE_USER ๊ถŒํ•œ์„ ๊ฐ€์ž… ํšŒ์›์—๊ฒŒ ๋ถ€์—ฌ
3. Spring Security ์„ค์ •์—์„œ ROLE_USER ๊ถŒํ•œ์„ ๊ฐ€์ง€๋ฉด ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋„๋ก ์„ธํŒ…
4. ๊ถŒํ•œ์ด ์žˆ๋Š” ํšŒ์›์ด ๋กœ๊ทธ์ธ ์„ฑ๊ณตํ•˜๋ฉด ๋ฆฌ์†Œ์Šค ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ JWT ํ† ํฐ ๋ฐœ๊ธ‰
5. ํ•ด๋‹น ํšŒ์›์€ ๊ถŒํ•œ์ด ํ•„์š”ํ•œ API ์ ‘๊ทผ ์‹œ JWT ๋ณด์•ˆ ํ† ํฐ์„ ์‚ฌ์šฉ

์ด์ฒ˜๋Ÿผ ์ ‘๊ทผ ์ œํ•œ์ด ํ•„์š”ํ•œ API์—๋Š” ๋ณด์•ˆ ํ† ํฐ์„ ํ†ตํ•ด์„œ ์ด ์œ ์ €๊ฐ€ ๊ถŒํ•œ์ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ Spring Security๋ฅผ ํ†ตํ•ด ํ™•์ธํ•˜๊ณ  ๋ฆฌ์†Œ์Šค๋ฅผ ์š”์ฒญํ•  ์ˆ˜์žˆ๋„๋ก ๊ตฌ์„ฑ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿง Spring Security Configuration

Spring Security 5.7.0-M2 ๋ณด๋‹ค ๋†’์€ ๋ฒ„์ „์€ WebSecurityConfigurerAdapter๊ฐ€ Deprecated๋˜์—ˆ๊ธฐ์— ์‚ฌ์šฉํ•˜์ง€์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ SecurityFilterChain Bean์„ ์ง์ ‘ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ „ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

@Configuration
public class SecurityConfiguration {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests((authz) -> authz
                .anyRequest().authenticated()
            )
            .httpBasic(withDefaults());
        return http.build();
    }
}

์ €๋Š” ์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค.

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

    private final UserDetailsServiceImpl userDetailsService;


    public WebSecurityConfig(UserDetailsServiceImpl userDetailsService){
        this.userDetailsService = userDetailsService;
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception{
        return configuration.getAuthenticationManager();
    }

    @Bean
    public JwtAuthenticationFilter jwtAuthenticationFilter() throws Exception{
        return new JwtAuthenticationFilter(userDetailsService);
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{

        http.csrf((csrf) -> csrf.disable());

        http.sessionManagement((sessionManagement) ->
                sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS));

        http.authorizeHttpRequests((authorizeHttpRequests) ->
                authorizeHttpRequests
                        .anyRequest().authenticated()); // ๋ชจ๋“  ์š”์ฒญ ์ธ์ฆ ํ•„์š”

        http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);

        return http.build();
    }
}

๐Ÿ“š ์˜ค๋Š˜์˜ ํšŒ๊ณ 

์•ž์œผ๋กœ ๊ธฐ์ˆ  ๋ฉด์ ‘ ์ค€๋น„๋„ ํ•ด์•ผํ•˜๋Š”๋ฐ ์ค‘์š”ํ•œ ์‹œ๊ฐ„์„ ๊ตฌํ˜„ ๋จผ์ €ํ•˜๋ฉด ์ต์ˆ™ํ•ด์ง€๊ฒ ์ง€๋ผ๋Š” ์•ˆ์ผํ•œ ๋งˆ์Œ์œผ๋กœ ์ œ๋Œ€๋กœ ๋ณด์ง€์•Š์•˜๋˜ ๊ฒƒ๋“ค์ด ๋งŽ์Šต๋‹ˆ๋‹ค. ์ง€๋‚˜์ณค๋˜ ๊ฒƒ๋“ค์„ ๋งŽ์ด ๋˜๋Œ์•„๋ณด๋ฉฐ ์ •๋ฆฌํ•˜๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

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