-
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 #3 from kimyubi/feature/social-login
close #2 Stateless OAuth2.0 Social Login (Spring Security + JWT + OAuth 2.0)
- Loading branch information
Showing
46 changed files
with
1,408 additions
and
82 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
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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/ourhours/server/domain/member/domain/entity/Member.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,30 @@ | ||
package com.ourhours.server.domain.member.domain.entity; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Member { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private Long kakaoId; | ||
|
||
private String name; | ||
|
||
@Builder | ||
public Member(String name, Long kakaoId) { | ||
this.name = name; | ||
this.kakaoId = kakaoId; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/ourhours/server/domain/member/repository/MemberRepository.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.ourhours.server.domain.member.repository; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.ourhours.server.domain.member.domain.entity.Member; | ||
|
||
@Repository | ||
public interface MemberRepository extends JpaRepository<Member, Long> { | ||
Optional<Member> findByKakaoId(Long kakaoId); | ||
} | ||
|
33 changes: 33 additions & 0 deletions
33
src/main/java/com/ourhours/server/domain/sample/domain/entity/Sample.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,33 @@ | ||
package com.ourhours.server.domain.sample.domain.entity; | ||
|
||
import com.ourhours.server.global.model.BaseEntity; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Sample extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String content; | ||
|
||
@Builder | ||
public Sample(String content) { | ||
this.content = content; | ||
} | ||
|
||
public void updateContent(String content) { | ||
this.content = content; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/ourhours/server/domain/sample/repository/SampleRepository.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,8 @@ | ||
package com.ourhours.server.domain.sample.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.ourhours.server.domain.sample.domain.entity.Sample; | ||
|
||
public interface SampleRepository extends JpaRepository<Sample, Long> { | ||
} |
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
9 changes: 9 additions & 0 deletions
9
src/main/java/com/ourhours/server/global/config/jpa/JpaAuditingConfiguration.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,9 @@ | ||
package com.ourhours.server.global.config.jpa; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; | ||
|
||
@Configuration | ||
@EnableJpaAuditing(auditorAwareRef = "customAuditorAware") | ||
public class JpaAuditingConfiguration { | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/ourhours/server/global/config/security/CustomOAuth2UserService.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,66 @@ | ||
package com.ourhours.server.global.config.security; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
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.client.userinfo.OAuth2UserService; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.ourhours.server.domain.member.domain.entity.Member; | ||
import com.ourhours.server.domain.member.repository.MemberRepository; | ||
import com.ourhours.server.global.model.security.Role; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class CustomOAuth2UserService implements OAuth2UserService<OAuth2UserRequest, OAuth2User> { | ||
|
||
private final MemberRepository memberRepository; | ||
public static final String ATTRIBUTE_KEY = "memberId"; | ||
|
||
@Override | ||
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { | ||
OAuth2UserService<OAuth2UserRequest, OAuth2User> oAuth2UserService = new DefaultOAuth2UserService(); | ||
OAuth2User oAuth2User = oAuth2UserService.loadUser(userRequest); | ||
|
||
Map<String, Object> kakaoAccount = (Map<String, Object>)oAuth2User.getAttributes().get("kakao_account"); | ||
Map<String, Object> kakaoProfile = (Map<String, Object>)kakaoAccount.get("profile"); | ||
String name = (String)kakaoProfile.get("nickname"); | ||
Long kakaoId = (Long)oAuth2User.getAttributes().get("id"); | ||
|
||
Map<String, Object> attributes = new HashMap<>(); | ||
attributes.put(ATTRIBUTE_KEY, getOrSaveMember(kakaoId, name).getId()); | ||
|
||
return new DefaultOAuth2User( | ||
Collections.singleton(new SimpleGrantedAuthority(Role.USER.name())), | ||
attributes, ATTRIBUTE_KEY); | ||
} | ||
|
||
private Member getOrSaveMember(Long kakaoId, String name) { | ||
Optional<Member> optionalMember = memberRepository.findByKakaoId(kakaoId); | ||
if (optionalMember.isEmpty()) | ||
return saveMember(kakaoId, name); | ||
|
||
return optionalMember.get(); | ||
} | ||
|
||
private Member saveMember(Long kakaoId, String name) { | ||
Member member = Member.builder() | ||
.name(name) | ||
.kakaoId(kakaoId) | ||
.build(); | ||
return memberRepository.save(member); | ||
} | ||
} | ||
|
75 changes: 75 additions & 0 deletions
75
...om/ourhours/server/global/config/security/HttpCookieOAuth2AuthorizedClientRepository.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,75 @@ | ||
package com.ourhours.server.global.config.security; | ||
|
||
import static java.util.Objects.*; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.time.Duration; | ||
|
||
import org.springframework.security.oauth2.client.web.AuthorizationRequestRepository; | ||
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.SerializationUtils; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.ourhours.server.global.util.cipher.Aes256; | ||
import com.ourhours.server.global.util.jpa.cookie.CookieUtil; | ||
|
||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class HttpCookieOAuth2AuthorizedClientRepository | ||
implements AuthorizationRequestRepository<OAuth2AuthorizationRequest> { | ||
|
||
public static final String OAUTH2_COOKIE_NAME = "OAUTH2_AUTHORIZATION_REQUEST"; | ||
public static final Duration OAUTH_COOKIE_EXPIRY = Duration.ofMinutes(5); | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
@Override | ||
public OAuth2AuthorizationRequest loadAuthorizationRequest(HttpServletRequest request) { | ||
return getCookie(request); | ||
} | ||
|
||
@Override | ||
public void saveAuthorizationRequest(OAuth2AuthorizationRequest authorizationRequest, HttpServletRequest request, | ||
HttpServletResponse response) { | ||
if (isNull(authorizationRequest)) { | ||
removeAuthorizationRequest(request, response); | ||
return; | ||
} | ||
|
||
CookieUtil.addCookie(response, OAUTH2_COOKIE_NAME, encrypt(authorizationRequest), OAUTH_COOKIE_EXPIRY); | ||
} | ||
|
||
@Override | ||
public OAuth2AuthorizationRequest removeAuthorizationRequest(HttpServletRequest request, | ||
HttpServletResponse response) { | ||
OAuth2AuthorizationRequest oAuth2AuthorizationRequest = getCookie(request); | ||
CookieUtil.removeCookie(request, response, OAUTH2_COOKIE_NAME); | ||
return oAuth2AuthorizationRequest; | ||
} | ||
|
||
private OAuth2AuthorizationRequest getCookie(HttpServletRequest request) { | ||
return CookieUtil.getCookie(request, OAUTH2_COOKIE_NAME).map(this::decrypt).orElse(null); | ||
} | ||
|
||
private String encrypt(OAuth2AuthorizationRequest authorizationRequest) { | ||
byte[] bytes = SerializationUtils.serialize(authorizationRequest); | ||
return Aes256.encrypt(bytes); | ||
} | ||
|
||
private OAuth2AuthorizationRequest decrypt(Cookie cookie) { | ||
byte[] bytes = Aes256.decrypt(cookie.getValue().getBytes(StandardCharsets.UTF_8)); | ||
return (OAuth2AuthorizationRequest)SerializationUtils.deserialize(bytes); | ||
} | ||
|
||
} | ||
|
||
|
||
|
Oops, something went wrong.