๐Ÿ’ Spring

๐Ÿ’ Spring

Spring error - The dependencies of some of the beans in the application context form a cycle

์˜์กด์„ฑ ์ˆœํ™˜ ์ฐธ์กฐ ์ˆœํ™˜ ์ฐธ์กฐ ์—๋Ÿฌ๋Š” ๋‘˜ ์ด์ƒ์˜ Bean์ด ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ๋นˆ์„ ์ฃผ์ž…๋ฐ›์œผ๋ ค๊ณ  ํ•  ๋•Œ ์ƒ๊ธด๋‹ค. securityConfig์— ์žˆ๋Š” TokenProvider๊ฐ€ AccountService ๋นˆ์„ ์ฃผ์ž…๋ฐ›๊ฒŒ ๋˜๊ณ , ๋˜ AccountService์—์„œ๋Š” SecurityConfig.passwordEncoder๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ์–ด์„œ ๊ณ„์† ์ˆœํ™˜ ์ฐธ์กฐ๊ฐ€ ์ผ์–ด๋‚˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทผ๋ฐ ์ด ๊ฒฝ์šฐ์—๋Š” SecurityConfig -> TokenProvider -> AccountService ์ˆœ์œผ๋กœ ์ฐธ์กฐํ•˜์ง€๋งŒ, TokenProvider๋Š” InitializingBean์„ implements ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— AccountService๊ฐ€ ์žˆ์ง€ ์•Š์€ ์ƒํ™ฉ์—์„œ AccountService๋ฅผ ์ฃผ์ž…๋ฐ›์œผ๋ ค๊ณ  ํ•˜๋‹ค๋ณด๋‹ˆ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค...

๐Ÿ’ Spring/Spring Security

12. JWT ํ† ํฐ Authorization์„ ์œ„ํ•œ ์ปค์Šคํ…€ ํ•„ํ„ฐ ์ƒ์„ฑ

์ด ํฌ์ŠคํŠธ๋Š” ๋ฐ์–ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋‹˜์˜ ์œ ํŠœ๋ธŒ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ๋‚˜์„œ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ๋กœ๊ทธ์ธ ์š”์ฒญ์ด ์˜ค๋ฉด ์‚ฌ์šฉ์ž ์ธ์ฆ ํ›„ JWT ํ† ํฐ์„ ์ƒ์„ฑํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ตํ•ด์คฌ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์‚ฌ์šฉ์ž๋Š” ๋งค๋ฒˆ ๋กœ๊ทธ์ธ์„ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ด์ „์— ๋ฐœ๊ธ‰ ๋ฐ›์€ JWT ํ† ํฐ์„ ๋“ค๊ณ  ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ํ•˜๋ฉด ์„œ๋ฒ„๋Š” ํ•ด๋‹น JWT ํ† ํฐ์„ ๊ฒ€์ฆํ•˜์—ฌ ์œ ํšจํ•œ ํ† ํฐ์ธ์ง€ ํ™•์ธ ํ›„ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” JWT ํ† ํฐ์„ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ๋Š” Filter๋ฅผ ์ƒ์„ฑํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. BasicAuthenticationFilter ์ƒ์† ํ—ค๋”์— Authorization : Basic *** ๋ฐฉ์‹์œผ๋กœ ์ธ์ฆ์„ ์‹œ๋„ํ•˜๋ฉด BasicAuthenticationFilter์—์„œ ํ•ด๋‹น ํ† ํฐ์„ ๊ฒ€์ฆํ•˜์—ฌ ์ธ์ฆ์„ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ, ์šฐ๋ฆฌ๋Š” Basic ๋ฐฉ์‹์ด ์•„๋‹Œ, JWT..

๐Ÿ’ Spring/Spring Security

11. JWT ๋กœ๊ทธ์ธ์„ ์œ„ํ•œ UsernamePasswordAuthenticationFilter ์ƒ์†

์ด ํฌ์ŠคํŠธ๋Š” ๋ฐ์–ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋‹˜์˜ ์œ ํŠœ๋ธŒ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ๋‚˜์„œ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. UsernamePasswordAuthenticationFilter์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ž˜ ํฌ์ŠคํŒ…์„ ์ฐธ์กฐํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. UsernamePasswordAuthenticationFilter ๋™์ž‘ ๋ฐฉ์‹์— ๋Œ€ํ•ด์„œ SpringSecurity Filterchain ์—๋Š” ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์— ํ•„ํ„ฐ๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ, ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๋กœ๊ทธ์ธ ์ธ์ฆ์„ ์ฒ˜๋ฆฌํ•˜๋Š” UsernamePasswordAuthenticationFilter์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Login ์ธ์ฆ ๋กœ์ง flow POST "/login".. iseunghan.tistory.com ๋กœ๊ทธ์ธ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” SpringSecurityFilterChain ์ค‘์— ๋กœ๊ทธ์ธ ์ธ์ฆ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•„ํ„ฐ์ธ Userna..

๐Ÿ’ Spring/Spring Security

UsernamePasswordAuthenticationFilter ๋™์ž‘ ๋ฐฉ์‹์— ๋Œ€ํ•ด์„œ

SpringSecurity Filterchain ์—๋Š” ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์— ํ•„ํ„ฐ๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ, ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๋กœ๊ทธ์ธ ์ธ์ฆ์„ ์ฒ˜๋ฆฌํ•˜๋Š” UsernamePasswordAuthenticationFilter์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Login ์ธ์ฆ ๋กœ์ง flow POST "/login" ์œผ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด? Postman์œผ๋กœ body์— username, password๋ฅผ ๋„ฃ๊ณ  "/login"์œผ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด, UsernamePasswordAuthenticationFilter๊ฐ€ ์š”์ฒญ์„ ๋‚š์•„์ฑ„๊ณ  username, password๋ฅผ ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ณผ์ •์—์„œ ์šฐ๋ฆฌ๊ฐ€ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•œ UsernamePasswordAuthenticationFilter.attemptAuthentication ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ์ด ๋ฉ๋‹ˆ๋‹ค. POST๋กœ ์š”์ฒญ์ด ์™”..

๐Ÿ’ Spring/Spring Security

10. JWT ๋กœ๊ทธ์ธ์„ ์œ„ํ•œ UserDetails, UserDetailsService ๊ตฌํ˜„ํ•˜๊ธฐ

์ด ํฌ์ŠคํŠธ๋Š” ๋ฐ์–ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋‹˜์˜ ์œ ํŠœ๋ธŒ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ๋‚˜์„œ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. Adapter ํŒจํ„ด์„ ์ด์šฉํ•˜์—ฌ UserDetails ๊ตฌํ˜„ User ํด๋ž˜์Šค๋Š” UserDetails๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” Adapter ํŒจํ„ด์„ ์ด์šฉํ•˜์—ฌ User ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. public class AccountAdapter extends User { private Account account; public AccountAdapter(Account account) { super(account.getUsername(), account.getPassword(), getAuthorities(account.getRoles())); this.account = account; } public static Collection

๐Ÿ’ Spring/Spring Security

9. JWT ์ž„์‹œ ํ† ํฐ์œผ๋กœ ํ•„ํ„ฐ ํ…Œ์ŠคํŠธ

์ด ํฌ์ŠคํŠธ๋Š” ๋ฐ์–ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋‹˜์˜ ์œ ํŠœ๋ธŒ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ๋‚˜์„œ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ํ† ํฐ์„ ๊ฒ€์ฆ ํ•  ํ•„ํ„ฐ ์ƒ์„ฑ ํ•„ํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ† ํฐ ๊ฒ€์ฆ์„ ํ–ˆ์„ ๋•Œ, ์œ ํšจํ•˜์ง€ ์•Š๋‹ค๋ฉด ํ•ด๋‹น ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋” ์ด์ƒ ์ ‘๊ทผํ•˜์ง€ ๋ชปํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. public class MyFilter3 implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) servletRequest; HttpServletR..

iseunghan
'๐Ÿ’ Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (3 Page)