From d8e256f7da667da3eccd5cffd56bdf8eb367eaf6 Mon Sep 17 00:00:00 2001 From: Jeongho Date: Tue, 7 May 2024 11:45:05 +0900 Subject: [PATCH] delete file --- .../backend/domain/auth/jwt/JWTFilter.java | 81 ------------------- .../auth/oauth/dto/CustomOAuth2User.java | 45 ----------- .../domain/auth/oauth/dto/KakaoResponse.java | 37 --------- .../domain/auth/oauth/dto/OAuth2Response.java | 15 ---- .../oauth/handler/CustomSuccessHandler.java | 62 -------------- .../service/CustomOAuth2UserService.java | 71 ---------------- 6 files changed, 311 deletions(-) delete mode 100644 src/main/java/com/meetup/teame/backend/domain/auth/jwt/JWTFilter.java delete mode 100644 src/main/java/com/meetup/teame/backend/domain/auth/oauth/dto/CustomOAuth2User.java delete mode 100644 src/main/java/com/meetup/teame/backend/domain/auth/oauth/dto/KakaoResponse.java delete mode 100644 src/main/java/com/meetup/teame/backend/domain/auth/oauth/dto/OAuth2Response.java delete mode 100644 src/main/java/com/meetup/teame/backend/domain/auth/oauth/handler/CustomSuccessHandler.java delete mode 100644 src/main/java/com/meetup/teame/backend/domain/auth/oauth/service/CustomOAuth2UserService.java diff --git a/src/main/java/com/meetup/teame/backend/domain/auth/jwt/JWTFilter.java b/src/main/java/com/meetup/teame/backend/domain/auth/jwt/JWTFilter.java deleted file mode 100644 index 0c7711b..0000000 --- a/src/main/java/com/meetup/teame/backend/domain/auth/jwt/JWTFilter.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.meetup.teame.backend.domain.auth.jwt; - -import com.meetup.teame.backend.domain.auth.oauth.dto.CustomOAuth2User; -import com.meetup.teame.backend.domain.user.dto.oauth.KakaoUserDto; -import jakarta.servlet.FilterChain; -import jakarta.servlet.ServletException; -import jakarta.servlet.http.Cookie; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import lombok.RequiredArgsConstructor; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.web.filter.OncePerRequestFilter; - -import java.io.IOException; - -@RequiredArgsConstructor -public class JWTFilter extends OncePerRequestFilter { - - private final JWTUtil jwtUtil; - - @Override - protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { - - //cookie들을 불러온 뒤 Authorization Key에 담긴 쿠키를 찾음 - String authorization = null; - Cookie[] cookies = request.getCookies(); - if (cookies != null) { - for (Cookie cookie : cookies) { - - System.out.println(cookie.getName()); - if (cookie.getName().equals("Authorization")) { - - authorization = cookie.getValue(); - } - } - } - - //Authorization 헤더 검증 - if (authorization == null) { - - System.out.println("token null"); - filterChain.doFilter(request, response); - - //조건이 해당되면 메소드 종료 (필수) - return; - } - - //토큰 - String token = authorization; - - //토큰 소멸 시간 검증 - if (jwtUtil.isExpired(token)) { - - System.out.println("token expired"); - filterChain.doFilter(request, response); - - //조건이 해당되면 메소드 종료 (필수) - return; - } - - //토큰에서 username과 role 획득 - //String username = jwtUtil.getUsername(token); - Long id = jwtUtil.getId(token); - String role = jwtUtil.getRole(token); - - //userDTO를 생성하여 값 set - KakaoUserDto kakaoUserDto = new KakaoUserDto(id, role); - - //UserDetails에 회원 정보 객체 담기 - CustomOAuth2User customOAuth2User = new CustomOAuth2User(kakaoUserDto); - - //스프링 시큐리티 인증 토큰 생성 - Authentication authToken = new UsernamePasswordAuthenticationToken(customOAuth2User, null, customOAuth2User.getAuthorities()); - //세션에 사용자 등록 - SecurityContextHolder.getContext().setAuthentication(authToken); - - filterChain.doFilter(request, response); - } -} diff --git a/src/main/java/com/meetup/teame/backend/domain/auth/oauth/dto/CustomOAuth2User.java b/src/main/java/com/meetup/teame/backend/domain/auth/oauth/dto/CustomOAuth2User.java deleted file mode 100644 index 07fb3f3..0000000 --- a/src/main/java/com/meetup/teame/backend/domain/auth/oauth/dto/CustomOAuth2User.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.meetup.teame.backend.domain.auth.oauth.dto; -import com.meetup.teame.backend.domain.user.dto.oauth.KakaoUserDto; -import lombok.RequiredArgsConstructor; -import org.springframework.security.core.GrantedAuthority; -import org.springframework.security.oauth2.core.user.OAuth2User; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Map; - -@RequiredArgsConstructor -public class CustomOAuth2User implements OAuth2User { - - private final KakaoUserDto kakaoUserDto; - - //원래는 이 메서드로 받은 데이터 값을 리턴할 수 있지만 구글,네이버,카카오 등에 따라 attribute가 달라서 안쓸거 - @Override - public Map getAttributes() { - return null; - } - - @Override - public Collection getAuthorities() { - - Collection collection = new ArrayList<>(); - - collection.add(new GrantedAuthority() { - @Override - public String getAuthority() { - return kakaoUserDto.getRole(); - } - }); - return collection; - } - - @Override - public String getName() { - return kakaoUserDto.getName(); - } - - public Long getId() { - return kakaoUserDto.getId(); - } - -} diff --git a/src/main/java/com/meetup/teame/backend/domain/auth/oauth/dto/KakaoResponse.java b/src/main/java/com/meetup/teame/backend/domain/auth/oauth/dto/KakaoResponse.java deleted file mode 100644 index e3f3443..0000000 --- a/src/main/java/com/meetup/teame/backend/domain/auth/oauth/dto/KakaoResponse.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.meetup.teame.backend.domain.auth.oauth.dto; - -import lombok.RequiredArgsConstructor; - -import java.util.Map; - -@RequiredArgsConstructor -public class KakaoResponse implements OAuth2Response { - - private final Map attribute; - - @Override - public String getProvider() { - return "kakao"; - } - - @Override - public String getProviderId() { - return attribute.get("id").toString(); - } - - @Override - public String getName() { - // kakao_account라는 Map에서 추출 - return (String) ((Map) attribute.get("properties")).get("nickname"); - } - - @Override - public String getGender() { - return (String) ((Map) attribute.get("kakao_account")).get("gender"); - } - - @Override - public String getBirthyear() { - return (String) ((Map) attribute.get("kakao_account")).get("birthyear"); - } -} diff --git a/src/main/java/com/meetup/teame/backend/domain/auth/oauth/dto/OAuth2Response.java b/src/main/java/com/meetup/teame/backend/domain/auth/oauth/dto/OAuth2Response.java deleted file mode 100644 index 742f54c..0000000 --- a/src/main/java/com/meetup/teame/backend/domain/auth/oauth/dto/OAuth2Response.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.meetup.teame.backend.domain.auth.oauth.dto; - -public interface OAuth2Response { - - //제공자 (Ex. naver, google, ...) - String getProvider(); - //제공자에서 발급해주는 아이디(번호) - String getProviderId(); - //사용자 실명 (설정한 이름) - String getName(); - - String getGender(); - - String getBirthyear(); -} diff --git a/src/main/java/com/meetup/teame/backend/domain/auth/oauth/handler/CustomSuccessHandler.java b/src/main/java/com/meetup/teame/backend/domain/auth/oauth/handler/CustomSuccessHandler.java deleted file mode 100644 index 27afbea..0000000 --- a/src/main/java/com/meetup/teame/backend/domain/auth/oauth/handler/CustomSuccessHandler.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.meetup.teame.backend.domain.auth.oauth.handler; - - -import com.meetup.teame.backend.domain.auth.jwt.JWTUtil; -import com.meetup.teame.backend.domain.auth.oauth.dto.CustomOAuth2User; -import jakarta.servlet.ServletException; -import jakarta.servlet.http.Cookie; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import lombok.RequiredArgsConstructor; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.GrantedAuthority; -import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; -import org.springframework.stereotype.Component; - -import java.io.IOException; -import java.util.Collection; -import java.util.Iterator; - -@RequiredArgsConstructor -@Component -public class CustomSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { - - private final JWTUtil jwtUtil; - - @Override - public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { - - //OAuth2User - CustomOAuth2User customUserDetails = (CustomOAuth2User) authentication.getPrincipal(); - - //String username = customUserDetails.getKakaoId(); - Long id = customUserDetails.getId(); - - Collection authorities = authentication.getAuthorities(); - Iterator iterator = authorities.iterator(); - GrantedAuthority auth = iterator.next(); - String role = auth.getAuthority(); - - //String token = jwtUtil.createJwt(username, role, 60*60*60L); - String token = jwtUtil.createJwt(id, role, 60*60*60L); - - /*response.addCookie(createCookie("Authorization", token)); - response.sendRedirect("http://localhost:3000");*/ - - // HTTP 응답에 토큰 값 추가 - response.setHeader("Authorization", "Bearer " + token); - response.setStatus(HttpServletResponse.SC_OK); - response.getWriter().flush(); - } - - private Cookie createCookie(String key, String value) { - - Cookie cookie = new Cookie(key, value); - cookie.setMaxAge(60*60*60); - //cookie.setSecure(true); - cookie.setPath("/"); - cookie.setHttpOnly(true); - - return cookie; - } -} diff --git a/src/main/java/com/meetup/teame/backend/domain/auth/oauth/service/CustomOAuth2UserService.java b/src/main/java/com/meetup/teame/backend/domain/auth/oauth/service/CustomOAuth2UserService.java deleted file mode 100644 index 4039083..0000000 --- a/src/main/java/com/meetup/teame/backend/domain/auth/oauth/service/CustomOAuth2UserService.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.meetup.teame.backend.domain.auth.oauth.service; - -import com.meetup.teame.backend.domain.auth.oauth.dto.CustomOAuth2User; -import com.meetup.teame.backend.domain.auth.oauth.dto.KakaoResponse; -import com.meetup.teame.backend.domain.auth.oauth.dto.OAuth2Response; -import com.meetup.teame.backend.domain.user.dto.oauth.KakaoUserDto; -import com.meetup.teame.backend.domain.user.entity.Gender; -import com.meetup.teame.backend.domain.user.entity.User; -import com.meetup.teame.backend.domain.user.repository.UserRepository; -import lombok.RequiredArgsConstructor; -import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; -import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; -import org.springframework.security.oauth2.core.OAuth2AuthenticationException; -import org.springframework.security.oauth2.core.user.OAuth2User; -import org.springframework.stereotype.Service; - -@RequiredArgsConstructor -@Service -public class CustomOAuth2UserService extends DefaultOAuth2UserService { - - private final UserRepository userRepository; - - @Override - public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { - //OAuth2LoginAuthenticationProvider로부터 받은 사용자 정보를 얻기위한 메서드 - OAuth2User oAuth2User = super.loadUser(userRequest); - //여기에 사용자 정보 담겨있음 - System.out.println(oAuth2User); - - //카카오인지 네이버인지 어디에서 온 요청인지 알기 위한 id 획득 - String registrationId = userRequest.getClientRegistration().getRegistrationId(); - OAuth2Response oAuth2Response = null; - if (registrationId.equals("kakao")) { - oAuth2Response = new KakaoResponse(oAuth2User.getAttributes()); - } - else { - - return null; - } - - //리소스 서버에서 발급 받은 정보로 사용자를 특정할 아이디값을 만듬 - String kakaoId = oAuth2Response.getProvider()+" "+oAuth2Response.getProviderId(); - User existData = userRepository.findByKakaoId(kakaoId); - - String genderStr = oAuth2Response.getGender(); - Gender gender = null; - if (genderStr != null) { - if (genderStr.equalsIgnoreCase("male")) { - gender = Gender.MALE; - } else if (genderStr.equalsIgnoreCase("female")) { - gender = Gender.FEMALE; - } - } - - if (existData == null) { - User user = User.ofKakao(kakaoId, oAuth2Response.getName(), gender, oAuth2Response.getBirthyear(), "ROLE_USER"); - userRepository.save(user); - KakaoUserDto kakaoUserDto = new KakaoUserDto(user.getId(), kakaoId, oAuth2Response.getName(), oAuth2Response.getGender(), oAuth2Response.getBirthyear()); - - return new CustomOAuth2User(kakaoUserDto); - } - else { - existData.setName(oAuth2Response.getName()); - userRepository.save(existData); - KakaoUserDto kakaoUserDto = new KakaoUserDto(existData.getId(), kakaoId, oAuth2Response.getName(), oAuth2Response.getGender(), oAuth2Response.getBirthyear()); - - return new CustomOAuth2User(kakaoUserDto); - } - } - -}