[Spring Boot] aLog Project - Spring Security와 OAuth2.0으로 로그인 기능 구현하기 4

김광현·2023년 9월 20일
0

Project - aLog

목록 보기
12/12
post-thumbnail

[Spring Boot] aLog Project - Spring Security와 OAuth2.0으로 로그인 기능 구현하기 3 에 이어서 Spring Security와 OAuth2.0으로 카카오 로그인 기능을 구현합니다. 🏆


💻 카카오 API 등록

[Spring Boot] 카카오 로그인 API 연동 과정 글을 읽는 것을 추천드립니다.

Kakao Developers로 이동합니다. 그리고 다음과 같이 각 항목을 채웁니다.


구글, 네이버에서와 마찬가지로 URL을 등록합니다.


Redirect URI을 등록해줍니다. Redirect URL은 구글에서 등록한 리디렉션 URL과 네이버에서 Callback URL과 같은 역할을 합니다.


수집할 개인 정보 설정을 해줍니다. 네이버, 구글과 마찬가지로 닉네임, 프로필 사진, 이메일을 선택해줍니다.



application-oauth.yml

해당 키값들을 application-oauth.yml에 등록합니다.

spring:
  security:
    oauth2:
      client:
        registration:
		...
          kakao:
            client-id: REST_API 키
            redirect-uri: "{baseUrl}/{action}/oauth2/code/{registrationId}"
            authorization_grant_type: authorization_code
            client-authentication-method: POST
            scope: profile_nickname, account_email, profile_image
            client-name: Kakao
            
        provider:
          kakao:
            authorization_uri: https://kauth.kakao.com/oauth/authorize
            token_uri: https://kauth.kakao.com/oauth/token
            user-info-uri: https://kapi.kakao.com/v2/user/me
            user_name_attribute: id


💻 Spring Security 설정 등록

OAuthAttributes.java

OAuthAttributes.java에 다음과 같이 카카오인지 판단하는 코드와 카카오 생성자를 추가해줍니다.

@Getter
public class OAuthAttributes {
	...
    
    public static OAuthAttributes of(String registrationId, String userNameAttributeName, Map<String, Object> attributes) {
        if("naver".equals(registrationId)) {
            return ofNaver("id", attributes);
        }
        if ("kakao".equals(registrationId)) {
            return ofKakao("id", attributes);
        }

        return ofGoogle(userNameAttributeName, attributes);
    }
    ...
     private static OAuthAttributes ofKakao(String userNameAttributeName, Map<String, Object> attributes) {
        Map<String, Object> kakaoAccount = (Map<String, Object>)attributes.get("kakao_account");
        Map<String, Object> kakaoProfile = (Map<String, Object>)kakaoAccount.get("profile");

        return OAuthAttributes.builder()
                .name((String) kakaoProfile.get("nickname"))
                .email((String) kakaoAccount.get("email"))
                .picture((String) kakaoProfile.get("profile_image_url"))
                .attributes(attributes)
                .nameAttributeKey(userNameAttributeName)
                .build();
    }
    ...

index.mustache

index.mustache에 카카오 로그인 버튼을 추가합니다.

		...
		{{^userName}}
			<a href="/oauth2/authorization/google" class="btn btn-outline-secondary" role="button">Google Login</a>
			<a href="/oauth2/authorization/naver" class="btn btn-outline-success" role="button">Naver Login</a>
			<a href="/oauth2/authorization/kakao" class="btn btn-outline-warning" role="button">Kakao Login</a>
		{{/userName}}
	</div>
</div>
...

✔ Spring Security와 OAuth2.0으로 카카오 로그인, 로그아웃, 회원가입, 권한관리 기능을 모두 구현해봤습니다.

profile
🅸nterest 🆂olving 🆃horough 🅹udgment

0개의 댓글