[Spring Security] httpBasic, HTTP Basic Authentication
๐ ์ธ์ฆ ๋ฉ์ปค๋์ฆ ์๋ฆฌ
HTTP Basic Authentication
์ ํน์ ๋ฆฌ์์ค์ ๋ํ ์ ๊ทผ์ ์์ฒญํ ๋ ์ฌ์ฉ์์๊ฒ username
๊ณผ password
๋ฅผ ํ์ธํ์ฌ ์ธ๊ฐ๋ฅผ ์ํํ๋ ๋ฐฉ๋ฒ์ด๋ค.
ํด๋ผ์ด์ธํธ๊ฐ ๋ณดํธ๋ ๋ฆฌ์์ค์ ์ ๊ทผํ๋ฉด ์๋ฒ๋ Authorization
ํค๋์ ์กด์ฌ ์ ๋ฌด๋ฅผ ํ์ธํ๊ณ , ์๋ต์ WWW-Authenticate
ํค๋์ Basic
์คํด๊ณผ realm
๋งค๊ฐ๋ณ์๋ฅผ ํฌํจํ์ฌ ๋ค์ ํด๋ผ์ด์ธํธ๋ก ์ ๋ฌํ๋ค.
ํด๋ผ์ด์ธํธ๋ ์ฌ์ฉ์์ ์๊ฒฉ ์ฆ๋ช
์ username:password
ํํ์ ๋ฌธ์์ด๋ก ๋ง๋ ํ ์ด๋ฅผ Base64
๋ก ์ธ์ฝ๋ฉํ์ฌ Authrization
ํค๋์ ๋ด์ ์๋ฒ์ ์ ์กํ๋ค.
Spring Boot์์ HTTP Basic Authentication ๊ตฌํ
๋จผ์ ์ฌ์ฉ์๊ฐ ๋ณดํธ๋ ๋ฆฌ์์ค์ ์ฒ์ ์ ๊ทผ์ ์๋ํ๋ฉด ์๋ฒ๋ SecurityFilterChain
์์ BasicAuthenticationFilter
๊ฐ ์์ฒญ์ ๋ฐ๋๋ค. BasicAuthenticationFilter
๋ OncePerRequestFilter
๋ฅผ ์์๋ฐ์ ๊ตฌํ๋๋๋ฐ, ์์ฒญ ๋น ํ ๋ฒ์ ์คํ์ ๋ณด์ฅํ ์ ์๋๋ก ํ๋ค.
์ด๋ `HTTP Basic Authentication์ด statelessํ ํน์ฑ์ ๊ฐ์ง๋ค๋ ๊ฒ์ ์๋ฏธํ๋ค.
BasicAuthenticationFilter
๋ form ๊ธฐ๋ฐ ์ธ์ฆ๊ณผ ๋ฌ๋ฆฌ ๋งค ์์ฒญ๋ง๋ค Authorization
ํค๋์ ์กด์ฌ ์ฌ๋ถ๋ฅผ ํ์ธ ๋ฐ ์ธ์ฆ์ ์๋ํ๋ค. ํค๋๊ฐ ์๋ค๋ฉด ์ธ์ฆ๋์ง ์์ ์์ฒญ์ผ๋ก ๊ฐ์ฃผํ๊ณ , 401 Unauthorized
์ ํจ๊ป ์๋ต์ ํด๋ผ์ด์ธํธ์ ์ ๋ฌํ๋ค. ํค๋๊ฐ ์กด์ฌํ๋ฉด Base64
๋ก ์ธ์ฝ๋ฉ๋ ํ ํฐ์ ์ถ์ถํ๊ณ ๋์ฝ๋ฉํ์ฌ username
๊ณผ password
๋ฅผ ๋ถ๋ฆฌํ์ฌ UsernamePasswordAuthenticationToken
์ ์์ฑํ ํ, AuthenticationManager
์๊ฒ ์ธ์ฆ์ ์์ํ๋ค. ์ธ์ฆ์ด ์ฑ๊ณตํ๋ฉด SecurityContextHolder
์ ์ธ์ฆ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ค.
์ ๋ฌ๋๋ ์๋ต์ WWW-Authenticate
ํค๋์๋ Basic
์คํด์ ์ง์ ํ๊ณ , realm
๋งค๊ฐ๋ณ์์ โ๋ณดํธ ์์ญโ์ ์ง์ ํ๋ค. ํด๋ผ์ด์ธํธ๋ Basic
์คํด์ ํ์ธํ๊ณ , ์๊ฒฉ ์ฆ๋ช
์ Base64
๋ก ์ธ์ฝ๋ฉํ๋ค. ์ฌ๊ธฐ์ โ๋ณดํธ ์์ญโ์ด๋ ์ฌ์ฉ์๊ฐ ์ด๋ค ์์คํ
์ ๋ก๊ทธ์ธํ๋ ค๋์ง๋ฅผ ์๋ ค์ค๋ค.
๐ HTTP Basic Authentication์ ์ฅ/๋จ์
1
2
3
4
5
6
7
8
9
const username = 'admin';
const password = 'secret123';
const credentials = btoa(`${username}:${password}`);
fetch('/api/data', {
headers: {
'Authorization': `Basic ${credentials}`
}
});
1
2
3
4
5
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.httpBasic(withDefaults());
return http.build();
}
HTTP Basic Authentication
์ ๊ฐ์ฅ ํฐ ์ฅ์ ์ ๋จ์ํ ๊ตฌํ์ด๋ค. ๋์์ ์์ด ํ ํฐ๋ง ์ฌ์ฉ๋๊ณ , ์ฟ ํค, ์ธ์
๋ฑ์ ํ์ํ์ง ์๋ค. ๋ํ HTTP ํ์ค ํ๋กํ ์ฝ์ ์ ์๋ ๋ฐฉ์์ด๋ฏ๋ก ๋๋ถ๋ถ์ ํด๋ผ์ด์ธํธ ๋ฐ ์๋ฒ์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ง์๋๋ค.
๋ค๋ง ์ฌ์ฉ์์ ์๊ฒฉ ์ฆ๋ช
์ด Base64
์ธ์ฝ๋ฉ์ด ๋์ด ์๋ฒ๋ก ์ ๋ฌ๋๋๋ฐ, ์ธ์ฝ๋ฉ์ ์ํธํ๊ฐ ์๋๋ค. ์ฆ, ์ธ์ฝ๋ฉ๋ ์๊ฒฉ ์ฆ๋ช
์ด ํ์ทจ๋๋ฉด ๋์ฝ๋ฉ์ ํตํด ์๊ฒฉ ์ฆ๋ช
์ ์ฝ๊ฒ ์ป์ ์ ์๋ค.
HTTPS ํต์ ์ ์ฌ์ฉํ๋ค๋ฉด Man-in-the-Middle
๊ณต๊ฒฉ์ ๋ํ ๋
ธ์ถ ์ํ์ ํฌ๊ฒ ์ค์ผ ์ ์๊ธด ํ๋ค.
๐ HTTP Basic Authentication vs. formLogin
formLogin
์ ์ธ์
๋ฐฉ์์ด๊ณ , HTTP Basic Authentication
์ stateless ๋ฐฉ์์ด๋ค. ๋ฐ๋ผ์ formLogin
์ ์ต์ด ๋ก๊ทธ์ธ ํ ์๋ฒ์์ ์ธ์
์ํ๋ฅผ ์ ์งํ์ฌ ๊ฐ์ ์ฌ์ฉ์์ ๋ํ ์ธ์ฆ์ ์๋ตํ ์ ์์ผ๋, httpBasic
์ ๊ทธ๋ ์ง ์๋ค.