[Toy Project] Swagger ๐ŸปBearer Token๐Ÿป์„ค์ •

์ตœ์ง€๋‚˜ยท2023๋…„ 11์›” 11์ผ
3
post-thumbnail

ํ”„๋กœ์ ํŠธ์—์„œ API ์š”์ฒญ์— ๋Œ€ํ•œ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•  ๋•Œ, ์œ ์ €๋งˆ๋‹ค Bearer token์„ ๋ถ€์—ฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ธ์ฆ์„ ์ง„ํ–‰ํ•˜์˜€๋‹ค. (์œ ์ €๊ฐ€ ๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ•˜๋ฉด token์„ ๋ถ€์—ฌํ•˜๋Š” ๋ฐฉ์‹) ์ด๋ฅผ Swagger์— ์ ์šฉ์‹œํ‚จ ๊ณผ์ •์„ ๊ธฐ๋กํ•˜๊ณ ์ž ํ•œ๋‹ค


Bearer Token์ด๋ž€?

  • Bearer = ์†Œ์œ ์ž
  • ํ† ํฐ์„ ์ „์†กํ•˜๋Š” ๋ฐฉ์‹ ์ค‘์— ํ•˜๋‚˜
  • HTTP ์ธ์ฆ ํ”„๋กœํ† ์ฝœ ์ค‘์— ํ•˜๋‚˜๋กœ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์˜ ์ž์›์— ์ ‘๊ทผํ•˜๊ณ ์ž ํ•  ๋•Œ ํ—ค๋”์— Bearer token์„ ํฌํ•จํ•˜์—ฌ ์š”์ฒญํ•˜์—ฌ ์ธ์ฆ์„ ๋ฐ›๋Š”๋‹ค
  • ์ผ๋ฐ˜์ ์œผ๋กœ OAuth๋‚˜ JWT์™€ ๊ฐ™์€ ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์‚ฌ์šฉ๋œ๋‹ค

JWT๋ž€?

  • ํŠน์ •ํ•œ ํ˜•์‹์˜ ํ† ํฐ์„ ์ •์˜ํ•˜๋Š” ํ‘œ์ค€

  • Json web token : Json ํฌ๋งท์„ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž์— ๋Œ€ํ•œ ์†์„ฑ์„ ์ €์žฅํ•˜๋Š” Web Token

  • Header, Payload, Signature์˜ 3 ๋ถ€๋ถ„์œผ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ ๊ฐ ๋ถ€๋ถ„์€ Base64๋กœ ์ธ์ฝ”๋”ฉ๋˜์–ด ํ‘œํ˜„๋œ๋‹ค ๊ฐ ๋ถ€๋ถ„์€ .์„ ๊ตฌ๋ถ„์ž๋กœ ์‚ฌ์šฉ

    • Header : token type, Signature ํ•ด์‹ฑ ์•Œ๊ณ ๋ฆฌ์ฆ˜
    • Payload: ๋ฐ์ดํ„ฐ
    • Signature: ํ† ํฐ์„ ์ธ์ฝ”๋”ฉ, ์œ ํšจ์„ฑ ๊ฒ€์ฆ. ํ—ค๋”์™€ ํŽ˜์ด๋กœ๋“œ๋ฅผ ํ•ด์‹ฑ, ์ธ์ฝ”๋”ฉ

์‚ฌ์šฉ ๋ฐฉ๋ฒ•
์ธ์ฆ header์— Authorization: Bearer <token> ์ถ”๊ฐ€ํ•˜์—ฌ ์š”์ฒญ

ex) Postman์„ ์‚ฌ์šฉํ•œ API ์š”์ฒญ ์‹œ ์ถ”๊ฐ€๋œ header ์—์‹œ

Swagger์—์„œ Bearer Token ์„ค์ •

์ฝ”๋“œ

JwtUtil.java

  • HTTP ์š”์ฒญ์—์„œ JWT ํ† ํฐ์„ ์ถ”์ถœ
  public String resolveToken(HttpServletRequest request) {
    ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(
      request
    );
    String token = wrappedRequest.getHeader("Authorization");
    if (token != null && token.startsWith("Bearer ")) {
      token = token.substring(7);
    }
    return token;
  }

JwtAuthenticationFilter.java

  • ์š”์ฒญ์—์„œ ์ถ”์ถœํ•œ ํ† ํฐ์„ ๊ฒ€์ฆํ•˜๊ณ , ์œ ํšจํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž ์ธ์ฆ์„ ์ˆ˜ํ–‰
@Autowired
  private JwtUtil jwtUtil;

  @Override
  public void doFilter(
    ServletRequest servletRequest,
    ServletResponse servletResponse,
    FilterChain filterChain
  ) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;

    String token = jwtUtil.resolveToken(request);
    request.setAttribute("token", token);

    if (token != null && jwtUtil.validateToken(token)) {
      Authentication auth = jwtUtil.getAuthentication(token);
      SecurityContextHolder.getContext().setAuthentication(auth);
    }

    filterChain.doFilter(request, servletResponse);
  }

SwaggerConfig.java

  • Bearer token ์‚ฌ์šฉ ์„ค์ •
  @Bean
  public OpenAPI api() {
    SecurityScheme apiKey = new SecurityScheme()
      .type(SecurityScheme.Type.APIKEY)
      .in(SecurityScheme.In.HEADER)
      .name("Authorization");

    SecurityRequirement securityRequirement = new SecurityRequirement()
      .addList("Bearer Token");

    return new OpenAPI()
      .components(new Components().addSecuritySchemes("Bearer Token", apiKey))
      .addSecurityItem(securityRequirement);
  }

์ ์šฉ ๋ฐฉ๋ฒ•

  1. ๋กœ๊ทธ์ธ API ์‹คํ–‰์„ ํ†ตํ•œ ์‚ฌ์šฉ์ž token ๋ฐœ๊ธ‰

  1. Authorize ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ๋’ค Value์— ๋ฐœ๊ธ‰๋ฐ›์€ Token ๊ฐ’ ๊ทธ๋Œ€๋กœ (๋˜๋Š” Bearer + ' ' + <token>)์ž…๋ ฅ

  1. Authorize ๋ฒ„ํŠผ ํด๋ฆญ
  • API๋“ค์˜ ์šฐ์ธก์— ์ž๋ฌผ์‡  ์•„์ด์ฝ˜์ด ์ž ๊ธด ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค

  1. ๋‹ค๋ฅธ API ์‹คํ–‰
  • 401์—๋Ÿฌ(token ์ž…๋ ฅ์„ ํ†ตํ•œ ํ† ํฐ ์ธ์ฆ์„ ๋ฐ›์ง€ ์•Š์Œ) ๋˜๋Š” 403์—๋Ÿฌ(์ธ์ฆ๋ฐ›์ง€ ๋ชปํ•œ token์„ ์ž…๋ ฅํ•จ) ์—†์ด ์ •์ƒ ๋™์ž‘๋จ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค

์ด์ œ Swagger๊นŒ์ง€ ์™„์„ฑ๋˜์—ˆ์œผ๋‹ˆ, front ๋‹ด๋‹น ์นœ๊ตฌ๊ฐ€ ์š”์ฒญํ•œ ์š”๊ตฌ์‚ฌํ•ญ๋“ค(ํŠน์ • Product ๊ฐ€์ ธ์˜ค๋Š” API ์ƒ์„ฑ, ์ œํ’ˆ ๋“ฑ๋ก ์‹œ img Multipart๋กœ ๋“ฑ๋ก๋˜๋„๋ก ์ˆ˜์ •) ์š”์ฒญ์„ ํ•ด๊ฒฐํ•˜๊ณ , ๋ฉ”์ธ ๋กœ์ง์„ ๋งŒ๋“ค ์ƒ๊ฐ์ด๋‹ค !!! ๐Ÿฃ


REF

Bearer ์ธ์ฆ ๋ฐฉ์‹์— ๋Œ€ํ•ด ์ฐธ์กฐํ•œ ๊ธ€์€ ์ด๊ณณ๊ณผ ์ด๊ณณ์ด๋‹ค

profile
์˜๊ฒฌ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์„ ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค แƒš(ใƒปใƒฎใƒปแƒš)

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2024๋…„ 9์›” 20์ผ

์•ˆ๋…•ํ•˜์„ธ์š”.
SecurityScheme.Type.HTTP๋Œ€์‹  SecurityScheme.Type.APIKEY์„ ์‚ฌ์šฉํ•ด๋„ Swagger UI์ƒ์—์„œ API ์‹คํ–‰์‹œ Header์— Token์ด ์ „๋‹ฌ๋˜์…จ์„๊นŒ์š”?
์ €๋Š” ์ •์ƒ์ ์œผ๋กœ Header์— ํ† ํฐ์ด ์ „๋‹ฌ๋˜์ง€ ์•Š์•„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜์˜€์Šต๋‹ˆ๋‹ค.
๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค๋ฉด ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

    SecurityScheme securityScheme = new SecurityScheme()
            .name("Authorization")
            .type(SecurityScheme.Type.HTTP) 
            .bearerFormat("JWT")
            .scheme("bearer");
๋‹ต๊ธ€ ๋‹ฌ๊ธฐ