-
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.
Browse files
Browse the repository at this point in the history
카카오 로그인 서비스 구현
- Loading branch information
Showing
20 changed files
with
486 additions
and
122 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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/example/pnuunivmiryangcampus/auth/Exception500.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 com.example.pnuunivmiryangcampus.auth; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class Exception500 extends RuntimeException { | ||
public Exception500(String message) { | ||
super(message); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/com/example/pnuunivmiryangcampus/auth/JwtOIDCProvider.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,79 @@ | ||
package com.example.pnuunivmiryangcampus.auth; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.ExpiredJwtException; | ||
import io.jsonwebtoken.Jws; | ||
import io.jsonwebtoken.Jwts; | ||
import java.math.BigInteger; | ||
import java.security.KeyFactory; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PublicKey; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.RSAPublicKeySpec; | ||
import java.util.Base64; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.configurationprocessor.json.JSONException; | ||
import org.springframework.boot.configurationprocessor.json.JSONObject; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Slf4j | ||
public class JwtOIDCProvider { | ||
|
||
public String getKidFromTokenHeader(String token) { | ||
|
||
String KID = "kid"; | ||
String[] splitToken = token.split("\\."); | ||
String header = splitToken[0]; | ||
|
||
byte[] decodeJson = Base64.getDecoder().decode(header); | ||
String decodeHeader = new String(decodeJson); | ||
|
||
try { | ||
JSONObject jsonObject = new JSONObject(decodeHeader); | ||
return jsonObject.get(KID).toString(); | ||
} catch (JSONException e) { | ||
return e.toString(); | ||
} | ||
} | ||
|
||
public Jws<Claims> getOIDCTokenJws(String token, String modulus, String exponent, String iss, String aud) { | ||
|
||
try { | ||
return Jwts.parser() | ||
.verifyWith(getRSAPublicKey(modulus, exponent)) | ||
.requireAudience(aud) | ||
.requireIssuer(iss) | ||
.build() | ||
.parseSignedClaims(token); | ||
} catch (ExpiredJwtException e) { | ||
throw new Exception500(e.getMessage()); | ||
} catch (Exception e) { | ||
log.error(e.toString()); | ||
throw new Exception500(e.getMessage()); | ||
} | ||
} | ||
|
||
public OIDCDecodePayload getOIDCTokenBody(String token, String modulus, String exponent, String iss, String aud) { | ||
|
||
Claims payload = getOIDCTokenJws(token, modulus, exponent, iss, aud).getPayload(); | ||
|
||
return new OIDCDecodePayload( | ||
payload.getIssuer(), | ||
payload.getAudience().toString(), | ||
payload.getSubject(), | ||
payload.get("email", String.class)); | ||
} | ||
|
||
private PublicKey getRSAPublicKey(String modulus, String exponent) throws NoSuchAlgorithmException, InvalidKeySpecException { | ||
|
||
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); | ||
byte[] decodeN = Base64.getUrlDecoder().decode(modulus); | ||
byte[] decodeE = Base64.getUrlDecoder().decode(exponent); | ||
BigInteger n = new BigInteger(1, decodeN); | ||
BigInteger e = new BigInteger(1, decodeE); | ||
|
||
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(n, e); | ||
return keyFactory.generatePublic(keySpec); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/pnuunivmiryangcampus/auth/KakaoInfoClient.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,14 @@ | ||
package com.example.pnuunivmiryangcampus.auth; | ||
|
||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
|
||
@FeignClient( | ||
name = "KakaoInfoClient", | ||
url = "${feign.client.kakao.oicd-base-url}") | ||
public interface KakaoInfoClient { | ||
|
||
@GetMapping("${feign.client.kakao.oicd-userinfo-uri}") | ||
KakaoInformationResponse kakaoUserInfo(@RequestHeader("Authorization") String accessToken); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/example/pnuunivmiryangcampus/auth/KakaoInformationResponse.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,13 @@ | ||
package com.example.pnuunivmiryangcampus.auth; | ||
|
||
public record KakaoInformationResponse( | ||
String sub, | ||
String nickname, | ||
String email, | ||
boolean emailVerified | ||
) { | ||
|
||
public static KakaoInformationResponse of(String sub, String nickname, String email, boolean emailVerified) { | ||
return new KakaoInformationResponse(sub, nickname, email, emailVerified); | ||
} | ||
} |
14 changes: 11 additions & 3 deletions
14
...nivmiryangcampus/auth/KakaoApiCaller.java → ...vmiryangcampus/auth/KakaoOauthClient.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 |
---|---|---|
@@ -1,16 +1,24 @@ | ||
package com.example.pnuunivmiryangcampus.auth; | ||
|
||
import com.example.pnuunivmiryangcampus.dto.KakaoTokenDto; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
@FeignClient(name = "${feign.client.kakao.name}", url = "${feign.client.kakao.base-url}") | ||
public interface KakaoApiCaller { | ||
@FeignClient( | ||
name = "KakaoOauthClient", | ||
url = "${feign.client.kakao.base-url}") | ||
public interface KakaoOauthClient { | ||
|
||
@PostMapping("/${feign.client.kakao.token-uri}") | ||
@PostMapping("${feign.client.kakao.token-uri}") | ||
KakaoTokenDto getKakaoToken(@RequestParam("client_id") String restApiKey, | ||
@RequestParam("redirect_uri") String redirectUrl, | ||
@RequestParam("code") String code, | ||
@RequestParam("grant_type") String grantType); | ||
|
||
@Cacheable(cacheNames = "KakaoOICD", cacheManager = "oidcCacheManager") | ||
@GetMapping("${feign.client.kakao.oicd-open-key-uri}") | ||
OIDCPublicKeysResponse getKakaoOIDCOpenKeys(); | ||
} |
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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/example/pnuunivmiryangcampus/auth/OIDCDecodePayload.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,16 @@ | ||
package com.example.pnuunivmiryangcampus.auth; | ||
|
||
public record OIDCDecodePayload( | ||
/* issuer ex https://kauth.kakao.com */ | ||
String iss, | ||
/* client id */ | ||
String aud, | ||
/* oauth provider account unique id */ | ||
String sub, | ||
String email | ||
) { | ||
|
||
public static OIDCDecodePayload of(String iss, String aud, String sub, String email) { | ||
return new OIDCDecodePayload(iss, aud, sub, email); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/pnuunivmiryangcampus/auth/OIDCPublicKeyDto.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,14 @@ | ||
package com.example.pnuunivmiryangcampus.auth; | ||
|
||
public record OIDCPublicKeyDto( | ||
String kid, | ||
String alg, | ||
String use, | ||
String n, | ||
String e | ||
) { | ||
|
||
public static OIDCPublicKeyDto of(String kid, String alg, String use, String n, String e) { | ||
return new OIDCPublicKeyDto(kid, alg, use, n, e); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/example/pnuunivmiryangcampus/auth/OIDCPublicKeysResponse.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,12 @@ | ||
package com.example.pnuunivmiryangcampus.auth; | ||
|
||
import java.util.List; | ||
|
||
public record OIDCPublicKeysResponse( | ||
List<OIDCPublicKeyDto> keys | ||
) { | ||
|
||
public static OIDCPublicKeysResponse of(List<OIDCPublicKeyDto> keys) { | ||
return new OIDCPublicKeysResponse(keys); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/example/pnuunivmiryangcampus/auth/OauthOIDCHelper.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,34 @@ | ||
package com.example.pnuunivmiryangcampus.auth; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class OauthOIDCHelper { | ||
|
||
private final JwtOIDCProvider jwtOIDCProvider; | ||
private final KakaoOauthClient kakaoOauthClient; | ||
private final KakaoProperties kakaoProperties; | ||
|
||
private OIDCDecodePayload getPayloadFromIdToken(String token, String iss, String aud, OIDCPublicKeysResponse oidcPublicKeysResponse) { | ||
String kid = jwtOIDCProvider.getKidFromTokenHeader(token); | ||
|
||
OIDCPublicKeyDto oidcPublicKeyDto = oidcPublicKeysResponse.keys().stream() | ||
.filter(o -> o.kid().equals(kid)) | ||
.findFirst() | ||
.orElseThrow(); | ||
|
||
return jwtOIDCProvider.getOIDCTokenBody(token, oidcPublicKeyDto.n(), oidcPublicKeyDto.e(), iss, aud); | ||
} | ||
|
||
public OIDCDecodePayload getKakaoOIDCDecodePayload(String token) { | ||
|
||
OIDCPublicKeysResponse oidcPublicKeysResponse = kakaoOauthClient.getKakaoOIDCOpenKeys(); | ||
return getPayloadFromIdToken( | ||
token, | ||
kakaoProperties.getBaseUrl(), | ||
kakaoProperties.getRestApiKey(), | ||
oidcPublicKeysResponse); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/example/pnuunivmiryangcampus/auth/RedisCacheConfig.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,34 @@ | ||
package com.example.pnuunivmiryangcampus.auth; | ||
|
||
import java.time.Duration; | ||
import org.springframework.cache.CacheManager; | ||
import org.springframework.cache.annotation.EnableCaching; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.cache.RedisCacheConfiguration; | ||
import org.springframework.data.redis.cache.RedisCacheManager; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@EnableCaching | ||
@Configuration | ||
public class RedisCacheConfig { | ||
@Bean | ||
public CacheManager oidcCacheManager(RedisConnectionFactory cf) { | ||
RedisCacheConfiguration redisCacheConfiguration = | ||
RedisCacheConfiguration.defaultCacheConfig() | ||
.serializeKeysWith( | ||
RedisSerializationContext.SerializationPair.fromSerializer( | ||
new StringRedisSerializer())) | ||
.serializeValuesWith( | ||
RedisSerializationContext.SerializationPair.fromSerializer( | ||
new GenericJackson2JsonRedisSerializer())) | ||
.entryTtl(Duration.ofDays(7L)); | ||
|
||
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(cf) | ||
.cacheDefaults(redisCacheConfiguration) | ||
.build(); | ||
} | ||
} |
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
Oops, something went wrong.