chore : merge main(from spring-security-10)
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.banjjoknim.playground.config.security
|
package com.banjjoknim.playground.config.security
|
||||||
|
|
||||||
|
import com.banjjoknim.playground.domain.auth.OAuth2Type
|
||||||
import com.banjjoknim.playground.domain.user.User
|
import com.banjjoknim.playground.domain.user.User
|
||||||
import com.banjjoknim.playground.domain.user.UserRepository
|
import com.banjjoknim.playground.domain.user.UserRepository
|
||||||
import org.springframework.context.annotation.Bean
|
import org.springframework.context.annotation.Bean
|
||||||
@@ -238,12 +239,15 @@ class PrincipalOAuth2UserService(
|
|||||||
println("${userRequest.additionalParameters}") // 5.1 버전 이후일 경우.
|
println("${userRequest.additionalParameters}") // 5.1 버전 이후일 경우.
|
||||||
|
|
||||||
// 강제로 회원가입 진행
|
// 강제로 회원가입 진행
|
||||||
|
val oAuth2Type = OAuth2Type.findByProvider(userRequest.clientRegistration.registrationId)
|
||||||
val oAuth2User = super.loadUser(userRequest)
|
val oAuth2User = super.loadUser(userRequest)
|
||||||
val provider = userRequest.clientRegistration.clientId // google
|
val oAuth2UserInfo = oAuth2Type.createOAuth2UserInfo(oAuth2User.attributes)
|
||||||
val providerId = oAuth2User.attributes["sub"] // googleId(PK)
|
|
||||||
|
val provider = oAuth2UserInfo.getProvider() // 값의 유무로 일반 로그인, OAuth2 로그인을 구분한다.
|
||||||
|
val providerId = oAuth2UserInfo.getProviderId()
|
||||||
val username = "${provider}_${providerId}" // OAuth2 로 로그인시, 필요 없지만 그냥 만들어준다.
|
val username = "${provider}_${providerId}" // OAuth2 로 로그인시, 필요 없지만 그냥 만들어준다.
|
||||||
val password = passwordEncoder.encode("비밀번호") // OAuth2 로 로그인시, 필요 없지만 그냥 만들어준다.
|
val password = passwordEncoder.encode("비밀번호") // OAuth2 로 로그인시, 필요 없지만 그냥 만들어준다.
|
||||||
val email = oAuth2User.attributes["email"]
|
val email = oAuth2UserInfo.getEmail()
|
||||||
val role = "ROLE_USER"
|
val role = "ROLE_USER"
|
||||||
|
|
||||||
// 회원가입 여부 확인 및 저장
|
// 회원가입 여부 확인 및 저장
|
||||||
@@ -252,10 +256,10 @@ class PrincipalOAuth2UserService(
|
|||||||
user = User(
|
user = User(
|
||||||
username = username,
|
username = username,
|
||||||
password = password,
|
password = password,
|
||||||
email = email as String,
|
email = email,
|
||||||
role = role,
|
role = role,
|
||||||
provider = provider,
|
provider = provider,
|
||||||
providerId = providerId as String
|
providerId = providerId
|
||||||
)
|
)
|
||||||
userRepository.save(user) // 회원정보 저장
|
userRepository.save(user) // 회원정보 저장
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.banjjoknim.playground.domain.auth
|
||||||
|
|
||||||
|
enum class OAuth2Type(
|
||||||
|
private val provider: String,
|
||||||
|
private val createUserInfo: (attributes: Map<String, Any?>) -> OAuth2UserInfo
|
||||||
|
) {
|
||||||
|
GOOGLE("google", { attributes -> GoogleUserInfo(attributes) }),
|
||||||
|
FACEBOOK("facebook", { attributes -> FacebookUserInfo(attributes) });
|
||||||
|
|
||||||
|
fun createOAuth2UserInfo(attributes: Map<String, Any?>): OAuth2UserInfo {
|
||||||
|
return createUserInfo(attributes)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun findByProvider(provider: String): OAuth2Type {
|
||||||
|
return values()
|
||||||
|
.find { oAuth2Type -> oAuth2Type.provider == provider }
|
||||||
|
?: throw IllegalArgumentException("존재하지 않는 OAuth2 인증 타입입니다.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.banjjoknim.playground.domain.auth
|
||||||
|
|
||||||
|
interface OAuth2UserInfo {
|
||||||
|
|
||||||
|
fun getProviderId(): String
|
||||||
|
|
||||||
|
fun getProvider(): String
|
||||||
|
|
||||||
|
fun getEmail(): String
|
||||||
|
|
||||||
|
fun getName(): String
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package com.banjjoknim.playground.domain.auth
|
||||||
|
|
||||||
|
class GoogleUserInfo(
|
||||||
|
/**
|
||||||
|
* DefaultOAuth2Service#loadUser(OAuth2UserRequest)
|
||||||
|
* ```kotlin
|
||||||
|
* val oAuth2User = super.loadUser(userRequest)
|
||||||
|
* val attributes = oAuth2User.attributes
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
private val attributes: Map<String, Any?>
|
||||||
|
) : OAuth2UserInfo {
|
||||||
|
|
||||||
|
override fun getProviderId(): String {
|
||||||
|
return attributes["sub"] as String
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getProvider(): String {
|
||||||
|
return "google"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getEmail(): String {
|
||||||
|
return attributes["email"] as String
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getName(): String {
|
||||||
|
return attributes["name"] as String
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class FacebookUserInfo(
|
||||||
|
/**
|
||||||
|
* DefaultOAuth2Service#loadUser(OAuth2UserRequest)
|
||||||
|
* ```kotlin
|
||||||
|
* val oAuth2User = super.loadUser(userRequest)
|
||||||
|
* val attributes = oAuth2User.attributes
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
private val attributes: Map<String, Any?>
|
||||||
|
) : OAuth2UserInfo {
|
||||||
|
override fun getProviderId(): String {
|
||||||
|
return attributes["id"] as String
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getProvider(): String {
|
||||||
|
return "facebook"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getEmail(): String {
|
||||||
|
return attributes["email"] as String
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getName(): String {
|
||||||
|
return attributes["name"] as String
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,9 +6,17 @@ spring:
|
|||||||
google:
|
google:
|
||||||
client-id: my-google-client-id
|
client-id: my-google-client-id
|
||||||
client-secret: my-google-client-secret
|
client-secret: my-google-client-secret
|
||||||
|
scope:
|
||||||
|
- email
|
||||||
|
- profile
|
||||||
|
|
||||||
facebook:
|
facebook:
|
||||||
client-id: my-facebook-client-id
|
client-id: my-facebook-client-id
|
||||||
client-secret: my-facebook-client-secret
|
client-secret: my-facebook-client-secret
|
||||||
|
scope:
|
||||||
|
- email
|
||||||
|
- public_profile
|
||||||
|
|
||||||
naver:
|
naver:
|
||||||
client-id: my-naver-client-id
|
client-id: my-naver-client-id
|
||||||
client-secret: my-naver-client-secret
|
client-secret: my-naver-client-secret
|
||||||
@@ -25,4 +33,3 @@ spring:
|
|||||||
token-uri: https://nid.naver.com/oauth2.0/token
|
token-uri: https://nid.naver.com/oauth2.0/token
|
||||||
user-info-uri: https://openapi.naver.com/v1/nid/me
|
user-info-uri: https://openapi.naver.com/v1/nid/me
|
||||||
user-name-attribute: response # 회원정보를 json으로 받는데 response라는 키값으로 네이버가 리턴해준다.
|
user-name-attribute: response # 회원정보를 json으로 받는데 response라는 키값으로 네이버가 리턴해준다.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user