forked from kookmin-sw/cap-template
-
Notifications
You must be signed in to change notification settings - Fork 1
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 kookmin-sw#10 from capstone-maru/feat/kookmin-sw#2-…
…OAuthKakao feat kookmin-sw#2 - OAuth2.0 로그인 기본 기능 구현 완료
- Loading branch information
Showing
21 changed files
with
1,086 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.psd filter=lfs diff=lfs merge=lfs -text |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.capstone.maru.config; | ||
|
||
import java.util.Optional; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.domain.AuditorAware; | ||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; | ||
|
||
/** | ||
* login 구현시 tester -> name 변경 | ||
*/ | ||
@EnableJpaAuditing | ||
@Configuration | ||
public class JpaConfig { | ||
|
||
@Bean | ||
public AuditorAware<String> auditorAware() { | ||
return () -> Optional.of("tester"); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/org/capstone/maru/config/SecurityConfig.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,52 @@ | ||
package org.capstone.maru.config; | ||
|
||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.capstone.maru.security.service.CustomOAuth2UserService; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.autoconfigure.security.servlet.PathRequest; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
|
||
@Slf4j | ||
@Configuration | ||
public class SecurityConfig { | ||
|
||
@Bean | ||
@ConditionalOnProperty(name = "spring.h2.console.enabled", havingValue = "true") | ||
public WebSecurityCustomizer configureH2ConsoleEnable() { | ||
return web -> web.ignoring() | ||
.requestMatchers(PathRequest.toH2Console()); | ||
} | ||
|
||
@Bean | ||
public SecurityFilterChain securityFilterChain( | ||
HttpSecurity httpSecurity, | ||
CustomOAuth2UserService customOAuth2UserService | ||
) throws Exception { | ||
return httpSecurity | ||
.authorizeHttpRequests(auth -> auth | ||
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() | ||
.requestMatchers( | ||
HttpMethod.GET, | ||
"/" | ||
).permitAll() | ||
.anyRequest().authenticated() | ||
) | ||
.oauth2Login(oAuth -> oAuth | ||
.userInfoEndpoint(userInfo -> userInfo | ||
.userService(customOAuth2UserService) | ||
) | ||
) | ||
.csrf( | ||
csrf -> csrf | ||
.ignoringRequestMatchers("/api/**") | ||
.disable() | ||
) | ||
.build(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/capstone/maru/controller/MainController.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,21 @@ | ||
package org.capstone.maru.controller; | ||
|
||
import org.capstone.maru.security.principal.SharedPostPrincipal; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class MainController { | ||
|
||
@GetMapping("/") | ||
public String root() { | ||
return "health check"; | ||
} | ||
|
||
@GetMapping("/test") | ||
public String test(@AuthenticationPrincipal SharedPostPrincipal sharedPostPrincipal) { | ||
return sharedPostPrincipal.getName(); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/org/capstone/maru/domain/AuditingFields.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,39 @@ | ||
package org.capstone.maru.domain; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.MappedSuperclass; | ||
import java.time.LocalDateTime; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
import org.springframework.data.annotation.CreatedBy; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.LastModifiedBy; | ||
import org.springframework.data.annotation.LastModifiedDate; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
import org.springframework.format.annotation.DateTimeFormat; | ||
|
||
@Getter | ||
@ToString(callSuper = true) | ||
@MappedSuperclass | ||
@EntityListeners(AuditingEntityListener.class) | ||
public class AuditingFields { | ||
|
||
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | ||
@CreatedDate | ||
@Column(nullable = false) | ||
protected LocalDateTime createdAt; // 생성일시 | ||
|
||
@CreatedBy | ||
@Column(nullable = false, updatable = false, length = 100) | ||
protected String createdBy; // 생성자 | ||
|
||
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | ||
@LastModifiedDate | ||
@Column(nullable = false) | ||
protected LocalDateTime modifiedAt; // 수정일시 | ||
|
||
@LastModifiedBy | ||
@Column(nullable = false, length = 100) | ||
protected String modifiedBy; // 수정자 | ||
} |
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,84 @@ | ||
package org.capstone.maru.domain; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Index; | ||
import jakarta.persistence.Table; | ||
import java.util.Objects; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@ToString(callSuper = true) | ||
@Table(indexes = { | ||
@Index(columnList = "memberId", unique = true), | ||
@Index(columnList = "email", unique = true), | ||
@Index(columnList = "createdAt"), | ||
@Index(columnList = "createdBy") | ||
}) | ||
@Entity | ||
public class MemberAccount extends AuditingFields { | ||
|
||
@Id | ||
@Column(nullable = false, length = 50) | ||
private String memberId; | ||
|
||
@Setter | ||
@Column(length = 100) | ||
private String email; | ||
|
||
@Setter | ||
@Column(length = 100) | ||
private String nickname; | ||
|
||
private MemberAccount( | ||
String memberId, | ||
String email, | ||
String nickname, | ||
String createdBy | ||
) { | ||
this.memberId = memberId; | ||
this.email = email; | ||
this.nickname = nickname; | ||
this.createdBy = createdBy; | ||
this.modifiedBy = createdBy; | ||
} | ||
|
||
public static MemberAccount of( | ||
String memberId, | ||
String email, | ||
String nickname | ||
) { | ||
return new MemberAccount(memberId, email, nickname, null); | ||
} | ||
|
||
public static MemberAccount of( | ||
String memberId, | ||
String email, | ||
String nickname, | ||
String createdBy | ||
) { | ||
return new MemberAccount(memberId, email, nickname, createdBy); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof MemberAccount that)) { | ||
return false; | ||
} | ||
return this.getMemberId() != null && this.getMemberId().equals(that.getMemberId()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(this.getMemberId()); | ||
} | ||
} |
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,72 @@ | ||
package org.capstone.maru.dto; | ||
|
||
import java.time.LocalDateTime; | ||
import org.capstone.maru.domain.MemberAccount; | ||
|
||
public record MemberAccountDto( | ||
String memberId, | ||
String email, | ||
String nickname, | ||
LocalDateTime createdAt, | ||
String createdBy, | ||
LocalDateTime modifiedAt, | ||
String modifiedBy | ||
) { | ||
|
||
public static MemberAccountDto of( | ||
String memberId, | ||
String email, | ||
String nickname | ||
) { | ||
return new MemberAccountDto( | ||
memberId, | ||
email, | ||
nickname, | ||
null, | ||
null, | ||
null, | ||
null | ||
); | ||
} | ||
|
||
public static MemberAccountDto of( | ||
String memberId, | ||
String email, | ||
String nickname, | ||
LocalDateTime createdAt, | ||
String createdBy, | ||
LocalDateTime modifiedAt, | ||
String modifiedBy | ||
) { | ||
return new MemberAccountDto( | ||
memberId, | ||
email, | ||
nickname, | ||
createdAt, | ||
createdBy, | ||
modifiedAt, | ||
modifiedBy | ||
); | ||
} | ||
|
||
public static MemberAccountDto from(MemberAccount entity) { | ||
return new MemberAccountDto( | ||
entity.getMemberId(), | ||
entity.getEmail(), | ||
entity.getNickname(), | ||
entity.getCreatedAt(), | ||
entity.getCreatedBy(), | ||
entity.getModifiedAt(), | ||
entity.getModifiedBy() | ||
); | ||
} | ||
|
||
public MemberAccount toEntity() { | ||
return MemberAccount.of( | ||
memberId, | ||
email, | ||
nickname, | ||
createdBy | ||
); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/org/capstone/maru/repository/MemberAccountRepository.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 org.capstone.maru.repository; | ||
|
||
import org.capstone.maru.domain.MemberAccount; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface MemberAccountRepository extends JpaRepository<MemberAccount, String> { | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/org/capstone/maru/security/constant/SocialType.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,22 @@ | ||
package org.capstone.maru.security.constant; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum SocialType { | ||
|
||
KAKAO("kakao"), NAVER("naver"); | ||
|
||
private final String key; | ||
|
||
public static SocialType of(String registrationId) { | ||
return switch (registrationId) { | ||
case "kakao" -> SocialType.KAKAO; | ||
case "naver" -> SocialType.NAVER; | ||
default -> throw new IllegalArgumentException( | ||
"Unsupported social registrationId: " + registrationId); | ||
}; | ||
} | ||
} |
Oops, something went wrong.