Post

[Spring Security] httpBasic, HTTP Basic Authentication

[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 ์€ ๊ทธ๋ ‡์ง€ ์•Š๋‹ค.

This post is licensed under CC BY 4.0 by the author.