๐ Spring
์์กด์ฑ ์ํ ์ฐธ์กฐ ์ํ ์ฐธ์กฐ ์๋ฌ๋ ๋ ์ด์์ Bean์ด ์์ฑ์๋ฅผ ํตํด ๋น์ ์ฃผ์
๋ฐ์ผ๋ ค๊ณ ํ ๋ ์๊ธด๋ค. securityConfig์ ์๋ TokenProvider๊ฐ AccountService ๋น์ ์ฃผ์
๋ฐ๊ฒ ๋๊ณ , ๋ AccountService์์๋ SecurityConfig.passwordEncoder๋ฅผ ์ฐธ์กฐํ๊ณ ์์ด์ ๊ณ์ ์ํ ์ฐธ์กฐ๊ฐ ์ผ์ด๋๊ฒ ๋๋ ๊ฒ์ด๋ค. ๊ทผ๋ฐ ์ด ๊ฒฝ์ฐ์๋ SecurityConfig -> TokenProvider -> AccountService ์์ผ๋ก ์ฐธ์กฐํ์ง๋ง, TokenProvider๋ InitializingBean์ implements ํ๊ณ ์๊ธฐ ๋๋ฌธ์ AccountService๊ฐ ์์ง ์์ ์ํฉ์์ AccountService๋ฅผ ์ฃผ์
๋ฐ์ผ๋ ค๊ณ ํ๋ค๋ณด๋ ์ค๋ฅ๊ฐ ์๊ธฐ๋ ๊ฒ ๊ฐ์๋ค...
๐ Spring/Spring Security
์ด ํฌ์คํธ๋ ๋ฐ์ด ํ๋ก๊ทธ๋๋ฐ๋์ ์ ํ๋ธ ๊ฐ์๋ฅผ ๋ฃ๊ณ ๋์ ์ ๋ฆฌํ ๊ธ์
๋๋ค. ๋ก๊ทธ์ธ ์์ฒญ์ด ์ค๋ฉด ์ฌ์ฉ์ ์ธ์ฆ ํ JWT ํ ํฐ์ ์์ฑํ์ฌ ํด๋ผ์ด์ธํธ์๊ฒ ์๋ตํด์คฌ์ต๋๋ค. ์ด์ ์ฌ์ฉ์๋ ๋งค๋ฒ ๋ก๊ทธ์ธ์ ํ๋ ๊ฒ์ด ์๋๋ผ, ์ด์ ์ ๋ฐ๊ธ ๋ฐ์ JWT ํ ํฐ์ ๋ค๊ณ ์๋ฒ๋ก ์์ฒญ์ ํ๋ฉด ์๋ฒ๋ ํด๋น JWT ํ ํฐ์ ๊ฒ์ฆํ์ฌ ์ ํจํ ํ ํฐ์ธ์ง ํ์ธ ํ ํด๋ผ์ด์ธํธ ์์ฒญ์ ์ฒ๋ฆฌํด์ฃผ๋ฉด ๋ฉ๋๋ค. ์ด๋ฒ ์๊ฐ์๋ JWT ํ ํฐ์ ๊ฒ์ฆํ ์ ์๋ Filter๋ฅผ ์์ฑํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. BasicAuthenticationFilter ์์ ํค๋์ Authorization : Basic *** ๋ฐฉ์์ผ๋ก ์ธ์ฆ์ ์๋ํ๋ฉด BasicAuthenticationFilter์์ ํด๋น ํ ํฐ์ ๊ฒ์ฆํ์ฌ ์ธ์ฆ์ ์ฒ๋ฆฌํ๋๋ฐ, ์ฐ๋ฆฌ๋ Basic ๋ฐฉ์์ด ์๋, JWT..
๐ Spring/Spring Security
์ด ํฌ์คํธ๋ ๋ฐ์ด ํ๋ก๊ทธ๋๋ฐ๋์ ์ ํ๋ธ ๊ฐ์๋ฅผ ๋ฃ๊ณ ๋์ ์ ๋ฆฌํ ๊ธ์
๋๋ค. UsernamePasswordAuthenticationFilter์ ๋ํด์๋ ์๋ ํฌ์คํ
์ ์ฐธ์กฐํ์๊ธธ ๋ฐ๋๋๋ค. UsernamePasswordAuthenticationFilter ๋์ ๋ฐฉ์์ ๋ํด์ SpringSecurity Filterchain ์๋ ์ฌ๋ฌ ์ข
๋ฅ์ ํํฐ๊ฐ ์กด์ฌํ์ง๋ง, ์ด๋ฒ ์๊ฐ์๋ ๋ก๊ทธ์ธ ์ธ์ฆ์ ์ฒ๋ฆฌํ๋ UsernamePasswordAuthenticationFilter์ ๋ํด์ ์์๋ณด๊ฒ ์ต๋๋ค. Login ์ธ์ฆ ๋ก์ง flow POST "/login".. iseunghan.tistory.com ๋ก๊ทธ์ธ ์์ฒญ์ ์ฒ๋ฆฌํ๊ธฐ ์ํด์๋ SpringSecurityFilterChain ์ค์ ๋ก๊ทธ์ธ ์ธ์ฆ์ ์ฒ๋ฆฌํ๋ ํํฐ์ธ Userna..
๐ Spring/Spring Security
SpringSecurity Filterchain ์๋ ์ฌ๋ฌ ์ข
๋ฅ์ ํํฐ๊ฐ ์กด์ฌํ์ง๋ง, ์ด๋ฒ ์๊ฐ์๋ ๋ก๊ทธ์ธ ์ธ์ฆ์ ์ฒ๋ฆฌํ๋ UsernamePasswordAuthenticationFilter์ ๋ํด์ ์์๋ณด๊ฒ ์ต๋๋ค. Login ์ธ์ฆ ๋ก์ง flow POST "/login" ์ผ๋ก ์์ฒญ์ ๋ณด๋ด๋ฉด? Postman์ผ๋ก body์ username, password๋ฅผ ๋ฃ๊ณ "/login"์ผ๋ก ์์ฒญ์ ๋ณด๋ด๋ฉด, UsernamePasswordAuthenticationFilter๊ฐ ์์ฒญ์ ๋์์ฑ๊ณ username, password๋ฅผ ๊ฒ์ฆํฉ๋๋ค. ๊ทธ ๊ณผ์ ์์ ์ฐ๋ฆฌ๊ฐ ์ค๋ฒ๋ผ์ด๋ฉํ UsernamePasswordAuthenticationFilter.attemptAuthentication ๋ฉ์๋๊ฐ ํธ์ถ์ด ๋ฉ๋๋ค. POST๋ก ์์ฒญ์ด ์..
๐ Spring/Spring Security
์ด ํฌ์คํธ๋ ๋ฐ์ด ํ๋ก๊ทธ๋๋ฐ๋์ ์ ํ๋ธ ๊ฐ์๋ฅผ ๋ฃ๊ณ ๋์ ์ ๋ฆฌํ ๊ธ์
๋๋ค. 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
์ด ํฌ์คํธ๋ ๋ฐ์ด ํ๋ก๊ทธ๋๋ฐ๋์ ์ ํ๋ธ ๊ฐ์๋ฅผ ๋ฃ๊ณ ๋์ ์ ๋ฆฌํ ๊ธ์
๋๋ค. ํ ํฐ์ ๊ฒ์ฆ ํ ํํฐ ์์ฑ ํํฐ๋ฅผ ์ด์ฉํ์ฌ ํ ํฐ ๊ฒ์ฆ์ ํ์ ๋, ์ ํจํ์ง ์๋ค๋ฉด ํด๋น ์ปจํธ๋กค๋ฌ๋ก ๋ ์ด์ ์ ๊ทผํ์ง ๋ชปํ๋๋ก ํ ์ ์์ต๋๋ค. public class MyFilter3 implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) servletRequest; HttpServletR..