๐Ÿ’ Spring/Spring Security

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

iseunghan 2021. 11. 4. 14:37
๋ฐ˜์‘ํ˜•

์ด ํฌ์ŠคํŠธ๋Š” ๋ฐ์–ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋‹˜์˜ ์œ ํŠœ๋ธŒ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ๋‚˜์„œ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

 

๋กœ๊ทธ์ธ ์š”์ฒญ์ด ์˜ค๋ฉด ์‚ฌ์šฉ์ž ์ธ์ฆ ํ›„ JWT ํ† ํฐ์„ ์ƒ์„ฑํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ตํ•ด์คฌ์Šต๋‹ˆ๋‹ค.

์ด์ œ ์‚ฌ์šฉ์ž๋Š” ๋งค๋ฒˆ ๋กœ๊ทธ์ธ์„ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ด์ „์— ๋ฐœ๊ธ‰ ๋ฐ›์€ JWT ํ† ํฐ์„ ๋“ค๊ณ  ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ํ•˜๋ฉด 

์„œ๋ฒ„๋Š” ํ•ด๋‹น JWT ํ† ํฐ์„ ๊ฒ€์ฆํ•˜์—ฌ ์œ ํšจํ•œ ํ† ํฐ์ธ์ง€ ํ™•์ธ ํ›„ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” JWT ํ† ํฐ์„ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ๋Š” Filter๋ฅผ ์ƒ์„ฑํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

BasicAuthenticationFilter ์ƒ์†

BasicAuthenticationFilter.doFilterInternal

ํ—ค๋”์— Authorization : Basic *** ๋ฐฉ์‹์œผ๋กœ ์ธ์ฆ์„ ์‹œ๋„ํ•˜๋ฉด BasicAuthenticationFilter์—์„œ ํ•ด๋‹น ํ† ํฐ์„ ๊ฒ€์ฆํ•˜์—ฌ ์ธ์ฆ์„ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ,

์šฐ๋ฆฌ๋Š” Basic ๋ฐฉ์‹์ด ์•„๋‹Œ, JWT๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•ด์ฃผ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

doFilterInternal

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {

    String jwt_Header = request.getHeader("Authorization");

    // ํ† ํฐ ๊ฐ’์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋“ค์–ด์žˆ๋Š”์ง€ ์ฒดํฌ!
    if(jwt_Header != null && jwt_Header.startsWith("Bearer")) {

        // JWT ์ถ”์ถœ
        String jwtToken = jwt_Header.replace("Bearer", "").trim();

        // JWT Verify (๊ฒ€์ฆ) -> ๊ฒ€์ฆ ์‹คํŒจ ์‹œ exception ๋ฐœ์ƒ
        String username = JWT.require(Algorithm.HMAC256("secret")).build().verify(jwtToken).getClaim("username").asString();

        // Verify ํ†ต๊ณผ? -> ์„œ๋ช…์ด ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋Š” ๋œป.
        // username์ด ๋น„์–ด์žˆ์ง„ ์•Š์€์ง€ ์ฒดํฌ
        if (username != null && !username.equals("")) {
            UserDetails userDetails = accountService.loadUserByUsername(username);

            // AuthenticationManager๋กœ ์ธ์ฆ์„ ํ•˜๋ฉด ์‹ค์ œ ๋กœ๊ทธ์ธ์„ ํ• ๋•Œ์— ํ•„์š”ํ•œ ์ž‘์—…์ด๋‚˜,
            // Authentication authenticate = getAuthenticationManager().authenticate(new UsernamePasswordAuthenticationToken(userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities()));

            // ํ˜„์žฌ ์šฐ๋ฆฌ๋Š” Token ์„œ๋ช…์œผ๋กœ ๋ฌด๊ฒฐ์„ฑ์„ ๊ฒ€์ฆํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— username์„ ๊ฐ€์ง€๊ณ  ๊ฐ•์ œ๋กœ Authentication ์„ ๋งŒ๋“ค์–ด securityContextHolder์— ๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
            Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities());
            // ์„ธ์…˜ ์ €์žฅ (๊ถŒํ•œ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ)
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
    }

    doFilter(request, response, chain);

}
  • ๋จผ์ € ํ—ค๋”์— Authorization ๊ฐ’์„ ๋ฝ‘์•„ ์˜ต๋‹ˆ๋‹ค.
  • ํ•ด๋‹น ๊ฐ’์ด ์žˆ๋Š”์ง€, Bearer ๋ฐฉ์‹์˜ ํ† ํฐ์ธ์ง€ ํ™•์ธ์„ ํ•ฉ๋‹ˆ๋‹ค.
  • JWT ํ† ํฐ ๊ฐ’์„ ์ถ”์ถœํ•˜์—ฌ verify๋ฅผ ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค.
  • ํ† ํฐ ๊ฒ€์ฆ์— ์„ฑ๊ณตํ•˜์˜€๋‹ค๋ฉด, user ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  Authentication ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • SecurityContextHolder์— ๋‹ด์Šต๋‹ˆ๋‹ค. (์ด๋•Œ, ๊ถŒํ•œ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด ์„ธ์…˜์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.)
๋ฐ˜์‘ํ˜•