jpablog : kakao login api - oauth
This commit is contained in:
@@ -7,6 +7,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
public class JpablogApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
SpringApplication.run(JpablogApplication.class, args);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,34 @@
|
||||
package com.example.jpablog.controller;
|
||||
|
||||
import com.example.jpablog.model.KakaoProfile;
|
||||
import com.example.jpablog.model.OAuthToken;
|
||||
import com.example.jpablog.model.User;
|
||||
import com.example.jpablog.service.KakaoLogin;
|
||||
import com.example.jpablog.service.UserService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
// 인증이 안된 사용자들이 들어오는 경로 /auth/**
|
||||
// "/" 요청시 index.jsp 허용
|
||||
// static 이하에 있는 /js/**, /css/**, /image/** 허용
|
||||
|
||||
@Controller
|
||||
@RequiredArgsConstructor
|
||||
public class UserController {
|
||||
|
||||
private final UserService userService;
|
||||
private final AuthenticationManager authenticationManager;
|
||||
private final BCryptPasswordEncoder encoder;
|
||||
|
||||
@GetMapping("/auth/joinForm")
|
||||
public String joinForm() {
|
||||
return "user/joinForm";
|
||||
@@ -25,4 +43,34 @@ public class UserController {
|
||||
public String updateForm() {
|
||||
return "user/updateForm";
|
||||
}
|
||||
|
||||
@GetMapping("/auth/kakao/callback")
|
||||
public String kakaoCallback(String code) {
|
||||
// Retrofit2, OkHttp, RestTemplate, HttpsURLConnection 등이 있음
|
||||
KakaoLogin kakaoLogin = new KakaoLogin();
|
||||
|
||||
OAuthToken token = kakaoLogin.getCode(code);
|
||||
KakaoProfile kakaoProfile = kakaoLogin.getKakaoProfile(token);
|
||||
String tempPassword = UUID.randomUUID().toString();
|
||||
String username = kakaoProfile.getKakao_account().getEmail() + "_" + kakaoProfile.getId();
|
||||
User user = userService.회원찾기(username);
|
||||
if (user.getUsername() == null) {
|
||||
User kakaoUser = User.builder()
|
||||
.username(username)
|
||||
.password(tempPassword)
|
||||
.email(kakaoProfile.getKakao_account().getEmail())
|
||||
.oauth("kakao")
|
||||
.build();
|
||||
userService.회원가입(kakaoUser);
|
||||
} else {
|
||||
user.setPassword(encoder.encode(tempPassword));
|
||||
}
|
||||
|
||||
Authentication authentication =
|
||||
authenticationManager.authenticate(
|
||||
new UsernamePasswordAuthenticationToken(username, tempPassword));
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
|
||||
return "redirect:/";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,20 +6,16 @@ import com.example.jpablog.model.OAuthToken;
|
||||
import com.example.jpablog.model.User;
|
||||
import com.example.jpablog.service.KakaoLogin;
|
||||
import com.example.jpablog.service.UserService;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@@ -47,16 +43,6 @@ public class UserApiController {
|
||||
return new ResponseDto<>(-1, HttpStatus.BAD_REQUEST.value());
|
||||
}
|
||||
|
||||
@GetMapping("/auth/kakao/callback")
|
||||
public String kakaoCallback(String code) {
|
||||
// Retrofit2, OkHttp, RestTemplate, HttpsURLConnection 등이 있음
|
||||
KakaoLogin kakaoLogin = new KakaoLogin();
|
||||
|
||||
OAuthToken token = kakaoLogin.getCode(code);
|
||||
KakaoProfile kakaoProfile = kakaoLogin.getKakaoProfile(token);
|
||||
System.out.println(kakaoProfile);
|
||||
return "ㅎㅇ";
|
||||
}
|
||||
/*// 기본 로그인
|
||||
@PostMapping("/user/login")
|
||||
public ResponseDto<Integer> login(@RequestBody User user, HttpSession session) {
|
||||
|
||||
@@ -4,7 +4,6 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.hibernate.annotations.ColumnDefault;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@@ -1,41 +1,41 @@
|
||||
package com.example.jpablog.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class KakaoProfile {
|
||||
public Integer id;
|
||||
public String connected_at;
|
||||
public Properties properties;
|
||||
public KakaoAccount kakao_account;
|
||||
}
|
||||
@Data
|
||||
class Properties {
|
||||
public String nickname;
|
||||
public String profile_image;
|
||||
public String thumbnail_image;
|
||||
}
|
||||
private Integer id;
|
||||
private String connected_at;
|
||||
private Properties properties;
|
||||
private KakaoAccount kakao_account;
|
||||
|
||||
@Data
|
||||
class KakaoAccount {
|
||||
public Boolean profile_needs_agreement;
|
||||
public Profile profile;
|
||||
public Boolean has_email;
|
||||
public Boolean email_needs_agreement;
|
||||
public Boolean is_email_valid;
|
||||
public Boolean is_email_verified;
|
||||
public String email;
|
||||
}
|
||||
@Data
|
||||
public static class Properties {
|
||||
private String nickname;
|
||||
private String profile_image;
|
||||
private String thumbnail_image;
|
||||
}
|
||||
|
||||
@Data
|
||||
class Profile {
|
||||
public String nickname;
|
||||
public String thumbnail_image_url;
|
||||
public String profile_image_url;
|
||||
@Data
|
||||
public static class KakaoAccount {
|
||||
private Boolean profile_needs_agreement;
|
||||
private Profile profile;
|
||||
private Boolean has_email;
|
||||
private Boolean email_needs_agreement;
|
||||
private Boolean is_email_valid;
|
||||
private Boolean is_email_verified;
|
||||
private String email;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Profile {
|
||||
private String nickname;
|
||||
private String thumbnail_image_url;
|
||||
private String profile_image_url;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -31,6 +31,8 @@ public class User {
|
||||
@Column(nullable = false, length = 50)
|
||||
private String email;
|
||||
|
||||
private String oauth;
|
||||
|
||||
// @ColumnDefault("'USER'")
|
||||
@Enumerated(EnumType.STRING)
|
||||
private RoleType role;
|
||||
|
||||
@@ -29,9 +29,16 @@ public class UserService {
|
||||
public void 회원수정(Long id, User user) {
|
||||
User persistence = userRepository.findById(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("회원 찾기 실패"));
|
||||
String encPassword = encoder.encode(user.getPassword());
|
||||
persistence.setPassword(encPassword);
|
||||
persistence.setEmail(user.getEmail());
|
||||
|
||||
if (persistence.getOauth() == null || persistence.getOauth().equals("")) {
|
||||
String encPassword = encoder.encode(user.getPassword());
|
||||
persistence.setPassword(encPassword);
|
||||
persistence.setEmail(user.getEmail());
|
||||
}
|
||||
}
|
||||
|
||||
public User 회원찾기(String username) {
|
||||
return userRepository.findByUsername(username).orElseGet(User::new);
|
||||
}
|
||||
|
||||
/*// 기본 로그인
|
||||
|
||||
@@ -9,10 +9,13 @@
|
||||
<label for="username">Username:</label>
|
||||
<input type="text" class="form-control" value="${principal.user.username}" id="username" readonly>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="password">Password:</label>
|
||||
<input type="password" class="form-control" placeholder="Enter password" id="password">
|
||||
</div>
|
||||
<c:if test="${empty principal.user.oauth}">
|
||||
<div class="form-group">
|
||||
<label for="password">Password:</label>
|
||||
<input type="password" class="form-control" placeholder="Enter password" id="password">
|
||||
</div>
|
||||
</c:if>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="email">Email:</label>
|
||||
<input type="email" class="form-control" value="${principal.user.email}" placeholder="Enter email" id="email">
|
||||
|
||||
Reference in New Issue
Block a user