diff --git a/build/classes/java/main/com/sunjoo/auth/domain/UserRepository.class b/build/classes/java/main/com/sunjoo/auth/domain/UserRepository.class index cae41ec..b7853ba 100644 Binary files a/build/classes/java/main/com/sunjoo/auth/domain/UserRepository.class and b/build/classes/java/main/com/sunjoo/auth/domain/UserRepository.class differ diff --git a/build/classes/java/main/com/sunjoo/auth/domain/controller/UserController.class b/build/classes/java/main/com/sunjoo/auth/domain/controller/UserController.class index 2f09d8f..84fce54 100644 Binary files a/build/classes/java/main/com/sunjoo/auth/domain/controller/UserController.class and b/build/classes/java/main/com/sunjoo/auth/domain/controller/UserController.class differ diff --git a/build/classes/java/main/com/sunjoo/auth/domain/service/JwtServiceImpl.class b/build/classes/java/main/com/sunjoo/auth/domain/service/JwtServiceImpl.class index fc98fea..539c196 100644 Binary files a/build/classes/java/main/com/sunjoo/auth/domain/service/JwtServiceImpl.class and b/build/classes/java/main/com/sunjoo/auth/domain/service/JwtServiceImpl.class differ diff --git a/build/classes/java/main/com/sunjoo/auth/domain/service/UserService.class b/build/classes/java/main/com/sunjoo/auth/domain/service/UserService.class index 6cf7685..4635cf0 100644 Binary files a/build/classes/java/main/com/sunjoo/auth/domain/service/UserService.class and b/build/classes/java/main/com/sunjoo/auth/domain/service/UserService.class differ diff --git a/build/classes/java/main/com/sunjoo/auth/domain/service/UserServiceImpl.class b/build/classes/java/main/com/sunjoo/auth/domain/service/UserServiceImpl.class index 441bc1a..df82846 100644 Binary files a/build/classes/java/main/com/sunjoo/auth/domain/service/UserServiceImpl.class and b/build/classes/java/main/com/sunjoo/auth/domain/service/UserServiceImpl.class differ diff --git a/build/classes/java/main/com/sunjoo/auth/global/config/SecurityConfig.class b/build/classes/java/main/com/sunjoo/auth/global/config/SecurityConfig.class index 6a183ed..35e5dd1 100644 Binary files a/build/classes/java/main/com/sunjoo/auth/global/config/SecurityConfig.class and b/build/classes/java/main/com/sunjoo/auth/global/config/SecurityConfig.class differ diff --git a/build/classes/java/main/com/sunjoo/auth/global/config/WebConfig.class b/build/classes/java/main/com/sunjoo/auth/global/config/WebConfig.class index cc27de5..039c9b3 100644 Binary files a/build/classes/java/main/com/sunjoo/auth/global/config/WebConfig.class and b/build/classes/java/main/com/sunjoo/auth/global/config/WebConfig.class differ diff --git a/build/resources/main/application.yml b/build/resources/main/application.yml index 8959960..1882def 100644 --- a/build/resources/main/application.yml +++ b/build/resources/main/application.yml @@ -35,4 +35,4 @@ eureka: register-with-eureka: true fetch-registry: true service-url: - defaultZone: http://sunjoo-server-eureka-eureka-1:8761/eureka/ \ No newline at end of file + defaultZone: http://localhost:8761/ \ No newline at end of file diff --git a/src/main/java/com/sunjoo/auth/domain/UserRepository.java b/src/main/java/com/sunjoo/auth/domain/UserRepository.java index 98794ed..e8378fc 100644 --- a/src/main/java/com/sunjoo/auth/domain/UserRepository.java +++ b/src/main/java/com/sunjoo/auth/domain/UserRepository.java @@ -9,4 +9,5 @@ public interface UserRepository extends JpaRepository { Optional findById(String id); Optional findByUserNo(long userNo); + Optional findByName(String name); } diff --git a/src/main/java/com/sunjoo/auth/domain/controller/UserController.java b/src/main/java/com/sunjoo/auth/domain/controller/UserController.java index f9022d5..ee42530 100644 --- a/src/main/java/com/sunjoo/auth/domain/controller/UserController.java +++ b/src/main/java/com/sunjoo/auth/domain/controller/UserController.java @@ -1,26 +1,32 @@ package com.sunjoo.auth.domain.controller; +import com.sunjoo.auth.domain.dto.KakaoLoginRequestDto; +import com.sunjoo.auth.domain.dto.KakaoLoginResponseDto; import com.sunjoo.auth.domain.dto.UserRegisterRequestDto; import com.sunjoo.auth.domain.dto.UserRegisterResponseDto; +import com.sunjoo.auth.domain.service.JwtService; +import com.sunjoo.auth.domain.service.RedisService; import com.sunjoo.auth.domain.service.UserService; import com.sunjoo.auth.global.Response; +import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.sql.SQLException; +import java.util.HashMap; @RequiredArgsConstructor @Slf4j @RestController public class UserController { private final UserService userService; + private final JwtService jwtService; + private final RedisService redisService; @PostMapping("/register") public ResponseEntity register(@Validated @RequestBody UserRegisterRequestDto registerRequest, BindingResult br) throws SQLException { @@ -29,4 +35,26 @@ public ResponseEntity register(@Validated @RequestBody UserRegisterRequestDto re UserRegisterResponseDto registerResponse = userService.register(registerRequest); return ResponseEntity.ok(Response.success(registerResponse)); } + + @GetMapping("/login/kakao") + public ResponseEntity kakaoLogin(@RequestParam(required = false) String token, HttpServletResponse response) { + try { + KakaoLoginRequestDto kakaoRequest = userService.getKakaoUserInfo(token); + + // 카카오 로그인 + KakaoLoginResponseDto kakaoResponse = userService.kakaoLogin(kakaoRequest); + String accessToken = jwtService.createAccessToken(kakaoResponse.getUserNo()); + String refreshToken = jwtService.createRefreshToken(); + + jwtService.sendAccessAndRefreshToken(response, accessToken, refreshToken); + redisService.setValues(kakaoResponse.getName(), refreshToken); + + // 우리 서버 jwt 넘겨주기 + response.setHeader("Authorization", "Bearer " + accessToken); + return ResponseEntity.ok(Response.success(kakaoResponse)); + } catch (Exception e) { + e.printStackTrace(); + return ResponseEntity.internalServerError().build(); + } + } } diff --git a/src/main/java/com/sunjoo/auth/domain/dto/KakaoLoginRequestDto.java b/src/main/java/com/sunjoo/auth/domain/dto/KakaoLoginRequestDto.java new file mode 100644 index 0000000..b0d68c6 --- /dev/null +++ b/src/main/java/com/sunjoo/auth/domain/dto/KakaoLoginRequestDto.java @@ -0,0 +1,14 @@ +package com.sunjoo.auth.domain.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class KakaoLoginRequestDto { + private String name; +} diff --git a/src/main/java/com/sunjoo/auth/domain/dto/KakaoLoginResponseDto.java b/src/main/java/com/sunjoo/auth/domain/dto/KakaoLoginResponseDto.java new file mode 100644 index 0000000..ffaf15e --- /dev/null +++ b/src/main/java/com/sunjoo/auth/domain/dto/KakaoLoginResponseDto.java @@ -0,0 +1,23 @@ +package com.sunjoo.auth.domain.dto; + +import com.sunjoo.auth.domain.User; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class KakaoLoginResponseDto { + private long userNo; + private String name; + private String type; + + public KakaoLoginResponseDto(User user) { + this.userNo = user.getUserNo(); + this.name = user.getName(); + this.type = user.getType(); + } +} diff --git a/src/main/java/com/sunjoo/auth/domain/service/UserService.java b/src/main/java/com/sunjoo/auth/domain/service/UserService.java index 2f84be4..ae69d04 100644 --- a/src/main/java/com/sunjoo/auth/domain/service/UserService.java +++ b/src/main/java/com/sunjoo/auth/domain/service/UserService.java @@ -1,8 +1,14 @@ package com.sunjoo.auth.domain.service; +import com.sunjoo.auth.domain.dto.KakaoLoginRequestDto; +import com.sunjoo.auth.domain.dto.KakaoLoginResponseDto; import com.sunjoo.auth.domain.dto.UserRegisterRequestDto; import com.sunjoo.auth.domain.dto.UserRegisterResponseDto; +import java.util.HashMap; + public interface UserService { public UserRegisterResponseDto register(UserRegisterRequestDto registerRequestDto); + public KakaoLoginRequestDto getKakaoUserInfo(String accessToken); + public KakaoLoginResponseDto kakaoLogin(KakaoLoginRequestDto kakaoRequest); } diff --git a/src/main/java/com/sunjoo/auth/domain/service/UserServiceImpl.java b/src/main/java/com/sunjoo/auth/domain/service/UserServiceImpl.java index b455e2b..1f65ac6 100644 --- a/src/main/java/com/sunjoo/auth/domain/service/UserServiceImpl.java +++ b/src/main/java/com/sunjoo/auth/domain/service/UserServiceImpl.java @@ -1,7 +1,12 @@ package com.sunjoo.auth.domain.service; +import com.nimbusds.jose.shaded.gson.JsonElement; +import com.nimbusds.jose.shaded.gson.JsonObject; +import com.nimbusds.jose.shaded.gson.JsonParser; import com.sunjoo.auth.domain.User; import com.sunjoo.auth.domain.UserRepository; +import com.sunjoo.auth.domain.dto.KakaoLoginRequestDto; +import com.sunjoo.auth.domain.dto.KakaoLoginResponseDto; import com.sunjoo.auth.domain.dto.UserRegisterRequestDto; import com.sunjoo.auth.domain.dto.UserRegisterResponseDto; import com.sunjoo.auth.global.exception.AppException; @@ -10,7 +15,16 @@ import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; import java.time.LocalDate; +import java.util.HashMap; +import java.util.Optional; @RequiredArgsConstructor @Service @@ -35,6 +49,66 @@ public UserRegisterResponseDto register(UserRegisterRequestDto registerRequestDt return new UserRegisterResponseDto(saved); } + // 카카오 로그인 + @Override + public KakaoLoginRequestDto getKakaoUserInfo(String accessToken) { + KakaoLoginRequestDto kakaoRequest = new KakaoLoginRequestDto(); + String postURL = "https://kapi.kakao.com/v2/user/me"; + + try { + URL url = new URL(postURL); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + conn.setRequestProperty("Authorization", "Bearer " + accessToken); + + int responseCode = conn.getResponseCode(); + + BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); + String line = ""; + StringBuilder result = new StringBuilder(); + + while((line = br.readLine()) != null) { + result.append(line); + } + + JsonElement element = JsonParser.parseString(result.toString()); + JsonObject properties = element.getAsJsonObject().get("properties").getAsJsonObject(); + JsonObject kakaoAccount = element.getAsJsonObject().get("kakao_account").getAsJsonObject(); + + String name = properties.getAsJsonObject().get("nickname").getAsString(); + kakaoRequest.setName(name); + } catch (Exception e) { + e.printStackTrace(); + } + return kakaoRequest; + } + + @Override + public KakaoLoginResponseDto kakaoLogin(KakaoLoginRequestDto kakaoRequest) { + KakaoLoginResponseDto kakaoResponse = new KakaoLoginResponseDto(); + + userRepository.findByName(kakaoRequest.getName()).ifPresentOrElse( + user -> { + kakaoResponse.setUserNo(user.getUserNo()); + kakaoResponse.setName(user.getName()); + kakaoResponse.setType("KAKAO"); + }, + () -> { + User newKakaoLogin = userRepository.save( + User.builder() + .name(kakaoRequest.getName()) + .type("KAKAO") + .createdAt(LocalDate.now()) + .build()); + kakaoResponse.setUserNo(newKakaoLogin.getUserNo()); + kakaoResponse.setType("KAKAO"); + kakaoResponse.setName(newKakaoLogin.getName()); + } + ); + + return kakaoResponse; + } + private void userJoinValid(String id) { userRepository.findById(id) .ifPresent(user -> {throw new AppException(ErrorCode.DUPLICATED_USER_ID);}); diff --git a/src/main/java/com/sunjoo/auth/global/config/SecurityConfig.java b/src/main/java/com/sunjoo/auth/global/config/SecurityConfig.java index edbca76..5ef33ef 100644 --- a/src/main/java/com/sunjoo/auth/global/config/SecurityConfig.java +++ b/src/main/java/com/sunjoo/auth/global/config/SecurityConfig.java @@ -43,11 +43,12 @@ public class SecurityConfig { public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .csrf(AbstractHttpConfigurer::disable) + .cors(AbstractHttpConfigurer::disable) .httpBasic(AbstractHttpConfigurer::disable) .formLogin(AbstractHttpConfigurer::disable) .addFilterAfter(jsonIdPasswordAuthenticationFilter(), LogoutFilter.class) .authorizeHttpRequests((authorize)->authorize - .requestMatchers("/register", "/", "/login").permitAll() + .requestMatchers("/register", "/", "/login/**").permitAll() // .requestMatchers("/**").access((authentication, context) -> new AuthorizationDecision(hasIpAddress.matches(context.getRequest()))) .anyRequest().authenticated()) // .authorizeHttpRequests((authorize) -> authorize diff --git a/src/main/java/com/sunjoo/auth/global/config/WebConfig.java b/src/main/java/com/sunjoo/auth/global/config/WebConfig.java index 806b48a..799918d 100644 --- a/src/main/java/com/sunjoo/auth/global/config/WebConfig.java +++ b/src/main/java/com/sunjoo/auth/global/config/WebConfig.java @@ -1,4 +1,16 @@ package com.sunjoo.auth.global.config; -public class WebConfig { +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfig implements WebMvcConfigurer { + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOrigins("*") + .allowedMethods("GET", "POST", "PUT", "DELETE"); + } }