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