thals0 / TIL

๐Ÿ“š ํ•˜๋ฃจ๋™์•ˆ ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ๊ธฐ๋กํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.
0 stars 0 forks source link

[Spring] Spring Security #9

Closed thals0 closed 1 year ago

thals0 commented 1 year ago

Spring Security ๋ž€?

๐ŸŒฑย Spring Security ์ฃผ์š” ์ปดํฌ๋„ŒํŠธ ํ™•์ธ

Spring Security ์™€ Filter

SecurityFilterChain

image

AbstractAuthenticationProcessingFilter

UsernamePasswordAuthenticationFilter

SecurityContextHolder

image

// ์˜ˆ์‹œ์ฝ”๋“œ
SecurityContext context = SecurityContextHolder.createEmptyContext();
Authentication authentication = new UsernamePasswordAuthenticationToken(principal, credentials, authorities);
context.setAuthentication(authentication);

SecurityContextHolder.setContext(context);

Authentication

<UserDetails>
        @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        UserRoleEnum role = user.getRole();
        String authority = role.getAuthority();
        System.out.println("authority = " + authority);

        SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(authority);
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(simpleGrantedAuthority);

        return authorities;
    }

Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());

UserDetailsService

UserDetailsService๋Š” username/password ์ธ์ฆ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•  ๋•Œ ์‚ฌ์šฉ์ž๋ฅผ ์กฐํšŒํ•˜๊ณ  ๊ฒ€์ฆํ•œ ํ›„ UserDetails๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. Customํ•˜์—ฌ Bean์œผ๋กœ ๋“ฑ๋ก ํ›„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

UserDetails

๊ฒ€์ฆ๋œ UserDetails๋Š” UsernamePasswordAuthenticationToken ํƒ€์ž…์˜ Authentication๋ฅผ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ ํ•ด๋‹น ์ธ์ฆ๊ฐ์ฒด๋Š” SecurityContextHolder์— ์„ธํŒ…๋œ๋‹ค.
Customํ•˜์—ฌ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

thals0 commented 1 year ago

๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™”

Spring Security ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ ์‘ํ˜• ๋‹จ๋ฐฉํ–ฅ ํ•จ์ˆ˜์ธ bCrypt๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์•”ํ˜ธํ™”

โ—๏ธ์ ์‘ํ˜• ๋‹จ๋ฐฉํ–ฅ ํ•จ์ˆ˜๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฆฌ์†Œ์Šค์˜ ๋‚ญ๋น„๊ฐ€ ๋งค์šฐ ์‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— API ์š”์ฒญ ๋งˆ๋‹ค ์‚ฌ์šฉ์ž์˜ ์ด๋ฆ„๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๊ฒ€์ฆํ•˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ฑ๋Šฅ์ด ํฌ๊ฒŒ ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์„ธ์…˜, ํ† ํฐ ๊ณผ ๊ฐ™์€ ์ธ์ฆ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ฆํ•˜๋Š” ๊ฒƒ์ด ์†๋„ ๋ฐ ๋ณด์•ˆ ์ธก๋ฉด์— ์œ ๋ฆฌํ•˜๋‹ค.

โ—๏ธ์šฐ๋ฆฌ๊ฐ€ ์•ž์œผ๋กœ ํ”„๋กœ์ ํŠธ์— ์ ์šฉํ•  ์‚ฌ์šฉ์ž ๊ฒ€์ฆ ํ๋ฆ„ ์งš๊ณ  ๋„˜์–ด๊ฐ€๊ธฐ!

  1. ์‚ฌ์šฉ์ž๋Š” ํšŒ์›๊ฐ€์ž…์„ ์ง„ํ–‰ํ•œ๋‹ค.
  2. ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•  ๋•Œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์•”ํ˜ธํ™”ํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค.
  3. ์‚ฌ์šฉ์ž๋Š” ๋กœ๊ทธ์ธ์„ ์ง„ํ–‰ํ•œ๋‹ค.
  4. ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ •๋ณด๋ฅผ ํ†ตํ•ด ์ €์žฅ๋œ ์•”ํ˜ธํ™”๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์•”ํ˜ธ์™€ ๋น„๊ตํ•œ๋‹ค.
  5. ์‚ฌ์šฉ์ž ์ธ์ฆ์ด ์„ฑ๊ณตํ•˜๋ฉด ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ JWT ํ† ํฐ์„ ์ƒ์„ฑํ•˜์—ฌ Header์— ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๊ณ  Client ๋Š” ์ด๋ฅผ ์ฟ ํ‚ค์ €์žฅ์†Œ์— ์ €์žฅํ•œ๋‹ค.
  6. ์‚ฌ์šฉ์ž๋Š” ๊ฒŒ์‹œ๊ธ€ ์ž‘์„ฑ๊ณผ ๊ฐ™์€ ์š”์ฒญ์„ ์ง„ํ–‰ํ•  ๋•Œ ๋ฐœ๊ธ‰๋ฐ›์€ JWT ํ† ํฐ์„ ๊ฐ™์ด ๋ณด๋‚ด๊ณ  ์„œ๋ฒ„๋Š” ์ด๋ฅผ ๋น ๋ฅด๊ฒŒ ์ธ์ฆ ํ•˜๊ณ  ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

์–‘๋ฐฉํ–ฅ โ†”ย ๋‹จ๋ฐฉํ–ฅ

Password Matching

Spring Security ์—์„œ๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์•”ํ˜ธํ™”ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ €์žฅ๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ๋น„๊ตํ•˜์—ฌ ์ผ์น˜์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋„ ์ œ๊ณต

// ์‚ฌ์šฉ์˜ˆ์‹œ
// ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ
if(!passwordEncoder.matches("์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ", "์ €์žฅ๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ")) {
           throw new IllegalAccessError("๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
 }

์•”ํ˜ธํ™” ๊ธฐ๋Šฅ ์ถ”๊ฐ€ ๋ฐฉ๋ฒ•

// WebSecurityConfig
@Bean // ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ๊ธฐ๋Šฅ ๋“ฑ๋ก
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }