- โ ์ฌ์ฉ์ ์ง๋ณ๋ณ ์๋ฆฌ ๋ ์ํผ ์ถ์ฒ ์น์๋น์ค
- ๐ 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
- ํ์๊ฐ์
๋ฐ ๋ก๊ทธ์ธ
- ๋ก๊ทธ์ธ, ํ์๊ฐ์
- ํ์ ํํด
- ํ์
- ํ์ ์ ๋ณด ์์ ๋ฐ ์กฐํ
- ๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ
- ์ฑ๋ด ๋ด์ฉ ์คํฌ๋ฉ ๊ธฐ๋ฅ
- ์คํฌ๋ฉ ๋ฐ ์คํฌ๋ฉ ์ญ์
- ์คํฌ๋ฉ ๋จ์ผ ์กฐํ ๋ฐ ์คํฌ๋ฉ ์ ์ฒด ๋ชฉ๋ก ์กฐํ
-
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๊ฐ์ ๋ฐ์ ์ ์๋๋ก ํฉ๋๋ค.
-
JWT ํ ํฐ ๊ด๋ จ
-
Custom Annotation
@Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) public @interface AuthUser { }
AuthUser๋ผ๋ ์ปค์คํ ์ด๋ ธํ ์ด์ ์ ์์ฑํ์ฌ ์ฌ์ฉ์๋ฅผ ์๋ณํฉ๋๋ค. -
AuthenticationSuccessHandler๋ฅผ ์์ ๋ฐ์CustomAuthenticationSuccessHandlerclass ์์ฑ@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" ๊ฐ์ ์ถ๊ฐํฉ๋๋ค.
-
SimpleUrlAuthenticationFailureHandler๋ฅผ ์์ ๋ฐ์CustomAuthenticationFailureHandlerclass ์์ฑ@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๋ฅผ ๋ฐํํฉ๋๋ค. -
UserDetails๋ฅผ ์์ ๋ฐ์AuthDetails,HandlerMethodArgumentResolver๋ฅผ ์์ ๋ฐ์AuthUserResolverclass ์์ฑ@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๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.
-