스프링부트+jsp로 배달사이트 만들기-36 카카오아이디로 로그인

2021. 12. 23. 17:29스프링부트

https://developers.kakao.com/console/app

 

카카오계정 로그인

여기를 눌러 링크를 확인하세요.

accounts.kakao.com

 

 

애플리케이션 추가하기

 

 

동의항목에서 이메일 체크

이메일은 필수동의로 하고 싶은데 안되네요

 

사이트 도메인 설정하기

 

 

 

 

 

Redirect URI 설정, 활성화 on

 

 

 

 

 

시크릿 키 발급받기

 

 

 

application.properties에 발급받은 REST API키와 시크릿 키를 등록

##카카오 로그인
spring.security.oauth2.client.registration.kakao.client-id=
spring.security.oauth2.client.registration.kakao.client-secret=
spring.security.oauth2.client.registration.kakao.redirect-uri=http://localhost:8080/login/oauth2/code/kakao
spring.security.oauth2.client.registration.kakao.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.kakao.scope=profile_nickname,account_email
spring.security.oauth2.client.registration.kakao.client-name:Kakao
spring.security.oauth2.client.registration.kakao.client-authentication-method=POST
## kAKAO Provider
spring.security.oauth2.client.provider.kakao.authorization-uri=https://kauth.kakao.com/oauth/authorize
spring.security.oauth2.client.provider.kakao.token-uri=https://kauth.kakao.com/oauth/token
spring.security.oauth2.client.provider.kakao.user-info-uri=https://kapi.kakao.com/v2/user/me
spring.security.oauth2.client.provider.kakao.user-name-attribute=id

 

 

 

 

OAuthUserInfo 클래스에 카카오를 추가합니다

@Service
public class OAuthUserInfo {
	
	@Autowired
	private BCryptPasswordEncoder encodePwd;
	
	
	public Join createUser(String provider, String username, OAuth2User oauth2user) {
		UUID uid = UUID.randomUUID();
		
		String password = encodePwd.encode(uid.toString());
		String email = "";
		String phone = "";
		switch (provider) {

		case "google":
			email = oauth2user.getAttribute("email");
			phone = oauth2user.getAttribute("phone") == null ? "" : oauth2user.getAttribute("phone");
			break;

		case "naver":
			Map<String, Object> response = oauth2user.getAttribute("response");
			email = (String) response.get("email");
			phone = (String) response.get("mobile") == null ? "" : (String) response.get("mobile");
			break;
			
		case "kakao":
			Map<String, Object> kakaoAccount = oauth2user.getAttribute("kakao_account");
			email = (String) kakaoAccount.get("email");
			phone = oauth2user.getAttribute("phone") == null ? "" : oauth2user.getAttribute("phone");
			break;
		}
		
		
		phone = phone.replace("-", "");
		
		Join join = new Join();
		join.setUsername(username);
		join.setPassword(password);
		join.setEmail(email);
		join.setNickname(username);
		join.setPhone(phone);
		
		return join;
	}
	
	
	
	public String getUsername(String provider,OAuth2User oauth2user) {
		String id = "";
		switch (provider) {

		case "google":
			id = oauth2user.getAttribute("sub");
			break;

		case "naver":
			Map<String, Object> response = oauth2user.getAttribute("response");
			id = (String) response.get("id");
			break;
			
		case "kakao":
			id = oauth2user.getAttribute("id").toString();
			break;
			
		}
		return provider + "_" + id;
		
	}

}