Skip to content

Commit

Permalink
Merge pull request #12 from K-Hackathon-Fledge/11-support
Browse files Browse the repository at this point in the history
11 support 후원 게시글 등록
  • Loading branch information
JuseungL authored Aug 2, 2024
2 parents 664a253 + 23fa59e commit 3f2c55c
Show file tree
Hide file tree
Showing 35 changed files with 1,089 additions and 218 deletions.
53 changes: 0 additions & 53 deletions src/main/java/com/fledge/fledgeserver/auth/dto/OAuth2UserInfo.java

This file was deleted.

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 src/main/java/com/fledge/fledgeserver/auth/dto/OAuthUserImpl.java
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;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fledge.fledgeserver.auth.jwt;

import com.fledge.fledgeserver.auth.service.CustomUserDetailsService;
import com.fledge.fledgeserver.auth.service.TokenService;
import com.fledge.fledgeserver.exception.TokenException;
import io.jsonwebtoken.Claims;
Expand All @@ -21,6 +22,8 @@
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

Expand All @@ -42,6 +45,7 @@ public class TokenProvider {

private static final String KEY_ROLE = "role";
private final TokenService tokenService;
private final CustomUserDetailsService userDetailsService;

@PostConstruct
private void setSecretKey() {
Expand Down Expand Up @@ -78,8 +82,9 @@ public Authentication getAuthentication(String token) {
Claims claims = parseClaims(token);
List<SimpleGrantedAuthority> authorities = getAuthorities(claims);

User principal = new User(claims.getSubject(), "", authorities);
return new UsernamePasswordAuthenticationToken(principal, token, authorities);
UserDetails userDetails = userDetailsService.loadUserByUsername(
claims.getSubject());
return new UsernamePasswordAuthenticationToken(userDetails, token, authorities);
}

private List<SimpleGrantedAuthority> getAuthorities(Claims claims) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.fledge.fledgeserver.auth.service;

import java.util.Collections;
import java.util.Map;

import com.fledge.fledgeserver.auth.dto.OAuthUserImpl;
import com.fledge.fledgeserver.auth.dto.OAuthAttributes;
import com.fledge.fledgeserver.member.entity.Member;
import com.fledge.fledgeserver.member.repository.MemberRepository;
import com.fledge.fledgeserver.auth.dto.OAuth2UserInfo;
import com.fledge.fledgeserver.auth.dto.PrincipalDetails;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
Expand All @@ -28,15 +30,16 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
String userNameAttributeName = userRequest.getClientRegistration().getProviderDetails()
.getUserInfoEndpoint().getUserNameAttributeName();

OAuth2UserInfo oAuth2UserInfo = OAuth2UserInfo.of(registrationId, oAuth2UserAttributes);
Member member = getOrSave(oAuth2UserInfo);
OAuthAttributes oauthAttributes = OAuthAttributes.of(registrationId, userNameAttributeName, oAuth2UserAttributes);
Member member = getOrSave(oauthAttributes);

return new PrincipalDetails(member, oAuth2UserAttributes, userNameAttributeName);
return new OAuthUserImpl(Collections.singleton(new SimpleGrantedAuthority(member.getRole().getKey())),
oAuth2UserAttributes, oauthAttributes.getNameAttributeKey(), member);
}

private Member getOrSave(OAuth2UserInfo oAuth2UserInfo) {
Member member = memberRepository.findBySocialId(oAuth2UserInfo.socialId())
.orElseGet(oAuth2UserInfo::toEntity);
private Member getOrSave(OAuthAttributes oauth2Attributes) {
Member member = memberRepository.findBySocialId(oauth2Attributes.getSocialId())
.orElseGet(oauth2Attributes::toEntity);
return memberRepository.save(member);
}
}
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;
}
}
Loading

0 comments on commit 3f2c55c

Please sign in to comment.