Skip to content

Latest commit

ย 

History

History
97 lines (85 loc) ยท 4.31 KB

File metadata and controls

97 lines (85 loc) ยท 4.31 KB

๐Ÿ“‹ ๋‹น์‹ ์„ ์œ„ํ•œ ์š”๋ฆฌ ๋ ˆ์‹œํ”ผ

  • โ“ ์‚ฌ์šฉ์ž ์งˆ๋ณ‘๋ณ„ ์š”๋ฆฌ ๋ ˆ์‹œํ”ผ ์ถ”์ฒœ ์›น์„œ๋น„์Šค
  • ๐Ÿ“† 24/09/23 ~ 24/09/24 ๊ธฐํš, 24/09/25 ~ 24/09/30 ๊ฐœ๋ฐœ

๐Ÿ“Ž ๊ฐœ๋ฐœํ™˜๊ฒฝ & ์ ์šฉ๊ธฐ์ˆ 

  • IDE : IntelliJ
  • OS : Windows
  • ๊ฐœ๋ฐœ ์–ธ์–ด : Java
  • DBMS : MariaDB
  • BackEnd: Spring Boot, Spring Security, JWT, JPA, MVC
  • ๋ฐฐํฌ: AWS EC2, AWS S3
  • ํ˜‘์—… : Notion, Google Drive, Github Projects

โญ ๊ธฐ๋Šฅ ์†Œ๊ฐœ

  1. ํšŒ์›๊ฐ€์ž… ๋ฐ ๋กœ๊ทธ์ธ
    • ๋กœ๊ทธ์ธ, ํšŒ์›๊ฐ€์ž…
    • ํšŒ์› ํƒˆํ‡ด
  2. ํšŒ์›
    • ํšŒ์› ์ •๋ณด ์ˆ˜์ • ๋ฐ ์กฐํšŒ
    • ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ
  3. ์ฑ—๋ด‡ ๋‚ด์šฉ ์Šคํฌ๋žฉ ๊ธฐ๋Šฅ
    • ์Šคํฌ๋žฉ ๋ฐ ์Šคํฌ๋žฉ ์‚ญ์ œ
    • ์Šคํฌ๋žฉ ๋‹จ์ผ ์กฐํšŒ ๋ฐ ์Šคํฌ๋žฉ ์ „์ฒด ๋ชฉ๋ก ์กฐํšŒ

๐Ÿ’ก ์ฃผ์š” ๊ธฐ๋Šฅ

  1. CORS ์„ธํŒ…

    • SecurityFilterChain์— CORS ์„ธํŒ… ์ถ”๊ฐ€
      http.cors(corsCustomizer -> corsCustomizer.configurationSource(request -> {
         CorsConfiguration configuration = new CorsConfiguration();
         configuration.setAllowedOrigins(List.of("*"));
         configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "PATCH"));
         configuration.setAllowedHeaders(List.of("*"));
         configuration.setExposedHeaders(List.of("token", "id"));
         return configuration;
      }));
      ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ์˜ค๋Š” ๋ชจ๋“  GET, POST, DELETE, PATCH ์š”์ฒญ์„ ๋ฐ›์•„๋“ค์ด๊ณ , Response Header ๊ฐ’์œผ๋กœ token, id ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
  2. JWT ํ† ํฐ ๊ด€๋ จ

    1. Custom Annotation

      @Target(ElementType.PARAMETER)
      @Retention(RetentionPolicy.RUNTIME)
      public @interface AuthUser {
      }

      AuthUser ๋ผ๋Š” ์ปค์Šคํ…€ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ƒ์„ฑํ•˜์—ฌ ์‚ฌ์šฉ์ž๋ฅผ ์‹๋ณ„ํ•ฉ๋‹ˆ๋‹ค.

    2. AuthenticationSuccessHandler๋ฅผ ์ƒ์† ๋ฐ›์€ CustomAuthenticationSuccessHandler class ์ž‘์„ฑ

      @Override
      public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
         UserDetails userDetails = (UserDetails) authentication.getPrincipal();
         UsersEntity userEntity = userRepository.findByUserId(userDetails.getUsername());
      
         String jwtToken = jwtUtils.createAccessToken(userEntity);
      
         // ์‘๋‹ต ํ—ค๋”์— ์ƒ์„ฑํ•œ ํ† ํฐ์„ ์„ค์ •
         response.setHeader("token", jwtToken); // JWT ๊ฐ’
         response.setHeader("id", userEntity.getId().toString()); // Users: ID ๊ฐ’(PK๊ฐ’)
      }

      ๋กœ๊ทธ์ธ ์„ฑ๊ณต ์‹œ, Response Header์— "token" ๊ฐ’, "id" ๊ฐ’์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

    3. SimpleUrlAuthenticationFailureHandler๋ฅผ ์ƒ์† ๋ฐ›์€ CustomAuthenticationFailureHandler class ์ž‘์„ฑ

      @Override
      public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
         response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); //401 ์ธ์ฆ ์‹คํŒจ
         response.getWriter().write("์•„์ด๋”” ํ˜น์€ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
      }

      ๋กœ๊ทธ์ธ ์‹คํŒจ ์‹œ, Response Status 401 UNAUTHORIZED๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

    4. UserDetails๋ฅผ ์ƒ์† ๋ฐ›์€ AuthDetails, HandlerMethodArgumentResolver๋ฅผ ์ƒ์† ๋ฐ›์€ AuthUserResolver class ์ž‘์„ฑ

      @Override // JwtFilter์—์„œ ๋ชจ๋‘ ๊ฒ€์ฆํ•˜๋ฏ€๋กœ, ๊ฒ€์ฆ ๋กœ์ง์€ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์Œ
      public UsersEntity resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
         HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
         String authorizationHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
         if(authorizationHeader == null)
            return null;
         
         // "Bearer {token}"์—์„œ {token}๊ฐ’๋งŒ ์ถ”์ถœ
         String jwtToken = authorizationHeader.substring(7);
         // {token}๊ฐ’์œผ๋กœ๋ถ€ํ„ฐ UserEntity ์กฐํšŒ
         UsersEntity user = jwtUtils.getUser(jwtToken);
         
         return user;
      }

      JWT ํ† ํฐ์ด ํ•„์š”ํ•œ ๋ชจ๋“  ์š”์ฒญ์—์„œ, Request Header๋กœ๋ถ€ํ„ฐ JWT ํ† ํฐ์„ ๋ฐ›์•„ UsersEntity ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.