-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from SWYP-InNOut/6-카카오-로그인
Feat(#6): 카카오로그인
- Loading branch information
Showing
14 changed files
with
603 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package inandout.backend.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
@EnableRedisRepositories // reids 사용한다고 명시 | ||
public class RedisConfig { | ||
|
||
@Value("${spring.data.redis.host}") | ||
private String redisHost; | ||
|
||
@Value("${spring.data.redis.port}") | ||
private int redisPort; | ||
|
||
@Value("${spring.data.redis.password}") | ||
private String redisPassword; | ||
|
||
// 연결 생성 | ||
@Bean | ||
public LettuceConnectionFactory redisConnectionFactory() { | ||
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); | ||
redisStandaloneConfiguration.setHostName(redisHost); | ||
redisStandaloneConfiguration.setPort(redisPort); | ||
redisStandaloneConfiguration.setPassword(redisPassword); | ||
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration); | ||
return lettuceConnectionFactory; | ||
} | ||
|
||
// 데이터 저장/조회 | ||
@Bean | ||
public RedisTemplate<String, Object> redisTemplate() { | ||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
return redisTemplate; | ||
} | ||
|
||
// @Bean | ||
// public StringRedisTemplate stringRedisTemplate() { | ||
// final StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); | ||
// stringRedisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
// stringRedisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
// stringRedisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
// return stringRedisTemplate; | ||
// } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/java/inandout/backend/controller/login/KakaoLoginController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package inandout.backend.controller.login; | ||
|
||
|
||
import inandout.backend.dto.login.KakoLoginResponseDTO; | ||
import inandout.backend.dto.login.LoginDTO; | ||
import inandout.backend.entity.auth.Platform; | ||
import inandout.backend.entity.member.Member; | ||
import inandout.backend.service.login.KakaoLoginService; | ||
import inandout.backend.service.login.RedisService; | ||
import inandout.backend.service.login.user.UserService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Optional; | ||
|
||
|
||
@RestController | ||
@RequestMapping("/kakaologin") | ||
public class KakaoLoginController { | ||
|
||
@Autowired | ||
public KakaoLoginService kakaoLoginService; | ||
@Autowired | ||
public RedisService redisService; | ||
@Autowired | ||
public UserService userService; | ||
|
||
|
||
@GetMapping("") | ||
public void KakaoLogin() { | ||
System.out.println("KakaoLoginController/KakaoLogin"); | ||
// 버튼 눌렀을때 로그인창 뜨게하는거 백에서 구현할거면 여기 사용 | ||
} | ||
|
||
@GetMapping("/callback") | ||
public ResponseEntity KakaoLoginCallBack(@RequestParam(value = "code") String code) throws IOException { | ||
System.out.println("KakaoLoginController/KakaoLoginCallBack"); | ||
KakoLoginResponseDTO kakoLoginResponseDTO = null; | ||
|
||
// 엑세스/리프레쉬 토큰 받기 | ||
HashMap<String, String> kakaoToken = kakaoLoginService.getAccessToken(code); | ||
|
||
// 유저 정보 받기 | ||
HashMap<String, Object> kakaoUserInfo = kakaoLoginService.getUserInfo(kakaoToken.get("accessToken")); | ||
String accessToken = kakaoToken.get("accessToken"); | ||
String refreshToken = kakaoToken.get("refreshToken"); | ||
String email = (String) kakaoUserInfo.get("email"); | ||
|
||
// email로 회원 찾기 | ||
Optional<Member> member = userService.findUser(email); | ||
|
||
|
||
if (member.isPresent()) { //회원 -> 로그인처리 | ||
|
||
//redis에서 refreshToken 칮기 | ||
String prevRefreshToken = redisService.getRefreshToken(email); | ||
kakoLoginResponseDTO = new KakoLoginResponseDTO(accessToken, prevRefreshToken,member.get().getName()); | ||
|
||
|
||
}else{ //비회원 ->가입 | ||
kakoLoginResponseDTO = new KakoLoginResponseDTO(accessToken, refreshToken, "홍길동"); | ||
|
||
LoginDTO loginDTO = new LoginDTO(); | ||
loginDTO.setName("홍길동"); // 닉네임 랜덤으로 부여 | ||
loginDTO.setEmail(email); | ||
loginDTO.setPassword(""); | ||
loginDTO.setPlatform(Platform.KAKAO); | ||
loginDTO.setPlatformId("1"); | ||
|
||
userService.save(loginDTO); | ||
|
||
//redis에 refreshToken 저장 | ||
redisService.setValues(email, refreshToken); | ||
|
||
} | ||
|
||
// //accessToken 만료되었는지 검사 | ||
// boolean isTokenValid = kakaoLoginService.isValidToken("KMXxzLPp_GjjTaMW1-3Z8t2GmCRxTqV9AAAAAQopyV8AAAGQplhQWxKZRqbpl2cW"); | ||
// System.out.println("accessToken 유효한지: "+isTokenValid); | ||
|
||
return ResponseEntity.ok().body(kakoLoginResponseDTO); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/inandout/backend/dto/login/KakoLoginResponseDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package inandout.backend.dto.login; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class KakoLoginResponseDTO { | ||
|
||
private String accessToken; | ||
private String refreshToken; | ||
private String name; | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package inandout.backend.dto.login; | ||
|
||
import inandout.backend.entity.auth.Platform; | ||
import inandout.backend.entity.member.MemberStatus; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class LoginDTO { | ||
|
||
private String name; | ||
private String email; | ||
private String password; | ||
private Platform platform; | ||
private String platformId; | ||
// private LocalDateTime createdAt; | ||
// private LocalDateTime updatedAt; | ||
// private MemberStatus status; | ||
// private boolean isPublic; | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/inandout/backend/repository/login/MemberRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package inandout.backend.repository.login; | ||
|
||
import inandout.backend.entity.member.Member; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface MemberRepository extends JpaRepository<Member, Long> { | ||
Optional<Member> findByEmail(String email); | ||
} |
Oops, something went wrong.