-
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.
Merge pull request #12 from K-Hackathon-Fledge/11-support
11 support 후원 게시글 등록
- Loading branch information
Showing
35 changed files
with
1,089 additions
and
218 deletions.
There are no files selected for viewing
53 changes: 0 additions & 53 deletions
53
src/main/java/com/fledge/fledgeserver/auth/dto/OAuth2UserInfo.java
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
src/main/java/com/fledge/fledgeserver/auth/dto/OAuthAttributes.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,73 @@ | ||
package com.fledge.fledgeserver.auth.dto; | ||
|
||
import com.fledge.fledgeserver.exception.AuthException; | ||
import com.fledge.fledgeserver.member.entity.Member; | ||
import com.fledge.fledgeserver.member.entity.Role; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import static com.fledge.fledgeserver.exception.ErrorCode.ILLEGAL_REGISTRATION_ID; | ||
|
||
@Getter | ||
public class OAuthAttributes { | ||
private final Map<String, Object> attributes; | ||
private final String nameAttributeKey; | ||
private final String nickname; | ||
private final String email; | ||
private final String profile; | ||
private final String registerType; | ||
private final Long socialId; | ||
|
||
@Builder | ||
public OAuthAttributes(Map<String, Object> attributes, String nameAttributeKey, String nickname, String email, String profile, String registerType, Long socialId) { | ||
this.attributes = attributes; | ||
this.nameAttributeKey = nameAttributeKey; | ||
this.nickname = nickname; | ||
this.email = email; | ||
this.profile = profile; | ||
this.registerType = registerType; | ||
this.socialId = socialId; | ||
} | ||
|
||
public static OAuthAttributes of(String socialName, String userNameAttributeName, Map<String, Object> attributes) { | ||
|
||
if ("kakao".equals(socialName)) { | ||
return ofKakao(userNameAttributeName, attributes); | ||
} | ||
|
||
throw new AuthException(ILLEGAL_REGISTRATION_ID); | ||
} | ||
|
||
private static OAuthAttributes ofKakao(String userNameAttributeName, Map<String, Object> attributes) { | ||
Map<String, Object> account = (Map<String, Object>) attributes.get("kakao_account"); | ||
Map<String, Object> profile = (Map<String, Object>) account.get("profile"); | ||
|
||
return OAuthAttributes.builder() | ||
.nameAttributeKey(userNameAttributeName) | ||
.attributes(attributes) | ||
.socialId(Long.valueOf(attributes.get("id").toString())) | ||
.nickname((String) profile.get("nickname")) | ||
.email((String) account.get("email")) | ||
.profile((String) profile.get("profile_image_url")) | ||
.registerType("KAKAO") | ||
.build(); | ||
} | ||
|
||
public Member toEntity() { | ||
return Member.builder() | ||
.socialId(socialId) | ||
.nickname(generateRandomNickname()) | ||
.email(email) | ||
.profile(profile) | ||
.role(Role.USER) | ||
.registerType("KAKAO") | ||
.build(); | ||
} | ||
|
||
private static String generateRandomNickname() { | ||
return "User_" + UUID.randomUUID().toString().replace("-", "").substring(0, 8); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/com/fledge/fledgeserver/auth/dto/OAuthUserImpl.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,58 @@ | ||
package com.fledge.fledgeserver.auth.dto; | ||
|
||
import com.fledge.fledgeserver.member.entity.Member; | ||
import lombok.Getter; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
@Getter | ||
public class OAuthUserImpl extends DefaultOAuth2User implements UserDetails { | ||
private final Member member; | ||
|
||
public OAuthUserImpl(Collection<? extends GrantedAuthority> authorities, | ||
Map<String, Object> attributes, | ||
String nameAttributeKey, | ||
Member member) { | ||
super(authorities, attributes, nameAttributeKey); | ||
this.member = member; | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return member.getId().toString(); | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonExpired() { | ||
return member.isActive(); | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonLocked() { | ||
return member.isActive(); | ||
} | ||
|
||
@Override | ||
public boolean isCredentialsNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return member.isActive(); | ||
} | ||
|
||
public Member getMember() { | ||
return member; | ||
} | ||
|
||
} |
63 changes: 0 additions & 63 deletions
63
src/main/java/com/fledge/fledgeserver/auth/dto/PrincipalDetails.java
This file was deleted.
Oops, something went wrong.
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
45 changes: 45 additions & 0 deletions
45
src/main/java/com/fledge/fledgeserver/auth/service/CustomUserDetailsService.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,45 @@ | ||
package com.fledge.fledgeserver.auth.service; | ||
|
||
import com.fledge.fledgeserver.auth.dto.OAuthUserImpl; | ||
import com.fledge.fledgeserver.member.entity.Member; | ||
import com.fledge.fledgeserver.member.repository.MemberRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CustomUserDetailsService implements UserDetailsService { | ||
private final MemberRepository memberRepository; | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
Member member = memberRepository.findById(Long.valueOf(username)) | ||
.orElseThrow(() -> new UsernameNotFoundException("사용자를 찾을 수 없습니다: " + username)); | ||
|
||
return new OAuthUserImpl( | ||
Collections.singleton(new SimpleGrantedAuthority(member.getRole().name())), | ||
createAttributes(member), | ||
"email", | ||
member | ||
); | ||
} | ||
|
||
// TODO :: Method 분리 | ||
private Map<String, Object> createAttributes(Member member) { | ||
Map<String, Object> attributes = new HashMap<>(); | ||
attributes.put("socialId", member.getSocialId()); | ||
attributes.put("nickname", member.getNickname()); | ||
attributes.put("email", member.getEmail()); | ||
attributes.put("profile", member.getProfile()); | ||
attributes.put("registerType", member.getRegisterType()); | ||
return attributes; | ||
} | ||
} |
Oops, something went wrong.