-
Notifications
You must be signed in to change notification settings - Fork 5
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 #9 from 1223v/security
Feat: KakaoOAuth 기능 추가
- Loading branch information
Showing
23 changed files
with
1,164 additions
and
28 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
28 changes: 28 additions & 0 deletions
28
src/main/java/com/readyvery/readyverydemo/domain/BaseTimeEntity.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,28 @@ | ||
package com.readyvery.readyverydemo.domain; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.LastModifiedDate; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.MappedSuperclass; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@EntityListeners(AuditingEntityListener.class) | ||
@MappedSuperclass | ||
@Getter | ||
@Setter | ||
public class BaseTimeEntity { | ||
|
||
@CreatedDate | ||
@Column(name = "created_at") | ||
private LocalDateTime createdAt; | ||
|
||
@LastModifiedDate | ||
@Column(name = "last_modified_at") | ||
private LocalDateTime lastModifiedAt; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/readyvery/readyverydemo/domain/Role.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.readyvery.readyverydemo.domain; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum Role { | ||
|
||
GUEST("ROLE_GUEST"), USER("ROLE_USER"); | ||
|
||
private final String key; | ||
} | ||
|
6 changes: 6 additions & 0 deletions
6
src/main/java/com/readyvery/readyverydemo/domain/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,6 @@ | ||
package com.readyvery.readyverydemo.domain; | ||
|
||
public enum SocialType { | ||
KAKAO, NAVER, GOOGLE | ||
} | ||
|
54 changes: 53 additions & 1 deletion
54
src/main/java/com/readyvery/readyverydemo/domain/UserInfo.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 |
---|---|---|
@@ -1,4 +1,56 @@ | ||
package com.readyvery.readyverydemo.domain; | ||
|
||
public class UserInfo { | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EnumType; | ||
import jakarta.persistence.Enumerated; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Entity | ||
@Builder | ||
@Table(name = "USERS") | ||
@AllArgsConstructor | ||
@Slf4j | ||
public class UserInfo extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "user_id") | ||
private Long id; | ||
|
||
@Column(nullable = false) | ||
private String email; // 이메일 | ||
|
||
@Column(nullable = false) | ||
private String nickName; // 닉네임 | ||
|
||
|
||
private String imageUrl; // 프로필 이미지 | ||
private int age; // 나이 | ||
|
||
@Enumerated(EnumType.STRING) | ||
private Role role; | ||
|
||
@Column(nullable = false) | ||
@Enumerated(EnumType.STRING) | ||
private SocialType socialType; // KAKAO, NAVER, GOOGLE | ||
|
||
@Column(nullable = false) | ||
private String socialId; // 로그인한 소셜 타입의 식별자 값 (일반 로그인인 경우 null) | ||
|
||
private String refreshToken; // 리프레시 토큰 | ||
|
||
public void updateRefresh(String updateRefreshToken) { | ||
this.refreshToken = updateRefreshToken; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/readyvery/readyverydemo/domain/repository/UserRepository.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 com.readyvery.readyverydemo.domain.repository; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.readyvery.readyverydemo.domain.SocialType; | ||
import com.readyvery.readyverydemo.domain.UserInfo; | ||
|
||
public interface UserRepository extends JpaRepository<UserInfo, Long> { | ||
Optional<UserInfo> findByEmail(String email); | ||
|
||
Optional<UserInfo> findByRefreshToken(String refreshToken); | ||
|
||
/** | ||
* 소셜 타입과 소셜의 식별값으로 회원 찾는 메소드 | ||
* 정보 제공을 동의한 순간 DB에 저장해야하지만, 아직 추가 정보(사는 도시, 나이 등)를 입력받지 않았으므로 | ||
* 유저 객체는 DB에 있지만, 추가 정보가 빠진 상태이다. | ||
* 따라서 추가 정보를 입력받아 회원 가입을 진행할 때 소셜 타입, 식별자로 해당 회원을 찾기 위한 메소드 | ||
*/ | ||
Optional<UserInfo> findBySocialTypeAndSocialId(SocialType socialType, String socialId); | ||
} |
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
38 changes: 38 additions & 0 deletions
38
...n/java/com/readyvery/readyverydemo/security/exception/CustomAuthenticationEntryPoint.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,38 @@ | ||
package com.readyvery.readyverydemo.security.exception; | ||
|
||
import java.io.IOException; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.readyvery.readyverydemo.security.exception.dto.ErrorResponse; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
|
||
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
public CustomAuthenticationEntryPoint(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, | ||
AuthenticationException authException) throws IOException { | ||
|
||
response.setStatus(HttpServletResponse.SC_FORBIDDEN); | ||
response.setContentType("application/json"); | ||
response.setCharacterEncoding("UTF-8"); | ||
|
||
ErrorResponse errorResponse = new ErrorResponse("Access Denied", request.getRequestURI()); | ||
response.getWriter().write(objectMapper.writeValueAsString(errorResponse)); | ||
} | ||
} | ||
|
12 changes: 12 additions & 0 deletions
12
src/main/java/com/readyvery/readyverydemo/security/exception/dto/ErrorResponse.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,12 @@ | ||
package com.readyvery.readyverydemo.security.exception.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public class ErrorResponse { | ||
private String message; | ||
private String path; | ||
|
||
} |
Oops, something went wrong.