Skip to content

Conversation

@sin-hyunjin
Copy link
Contributor

@sin-hyunjin sin-hyunjin commented Mar 26, 2024

Issues 번호 :

Closes #29

변경, 추가된 코드(설명 등)

OAUTH2.0

  • GOOGLE

  • KAKAO

  • NAVER

image
  • 로그인 테스트 및 토큰생성 처리 확인

  • 비밀번호 null 로직에서 -> 비밀번호를 OAuth2로 로그인시 고유한 id값으로 셋팅

코드 주의점

FRONT-END

현재 테스트 url
front - http://localhost:8080
back - http://localhost:3000

  • endpoint
    <a href="http://localhost:3000/oauth2/authorization/google?redirect_uri=http://localhost:3000&mode=login">
    이런식으로 접근해야하나? 좀 더 알아봐야 할 것 같다

Summary by CodeRabbit

- New Feature: PostgreSQL JDBC 드라이버 및 h2 라이브러리 관련 의존성 추가
- New Feature: modelmapper와 OAuth2에 대한 새로운 의존성 추가
- Refactor: Jakarta Validation API 의존성 제거
- New Feature: AppConfig 클래스에 ModelMapper와 BCryptPasswordEncoder를 등록하는 Bean 추가
- New Feature: Spring Security 설정 변경 (JwtAuthenticationFilter, JwtTokenProvider, CustomOAuth2UserService 등)
- Refactor: com.chukapoka.server.common.authority 패키지명을 com.chukapoka.server.common.authority.jwt로 변경
- Refactor: JwtTokenProvider 클래스 변경 (패키지명 변경, CustomUser 클래스 대신 CustomUserDetails 클래스 사용, UserRepository 추가, 토큰 만료기한 포맷 메서드 추가)
- New Feature: OAuth2Attribute 클래스 추가 (OAuth2 로그인 사용자 속성 정보 처리)
- New Feature: CustomAuthenticationFailHandler 클래스 추가 (인증 실패 시 메인 URL로 리다이렉트)
- New Feature: CustomAuthenticationSuccessHandler 클래스 추가 (OAuth2 인증 성공 시 토큰 생성 및 JSON 응답 반환)
- New Feature: CustomOAuth2UserService 클래스 추가 (OAuth2 사용자 정보 처리, 사용자 저장 및 업데이트 로직 포함)
- Refactor: CustomUser 클래스 삭제, CustomUserDetails 클래스 추가 (Spring Security의 User 클래스 확장하여 사용자 정보 저장)
- Refactor: TokenDto 클래스의 accessTokenExpiresIn 필드를 atExpiration과 rtExpiration으로 변경
- New Feature: Token 클래스에 atExpiration(액세스 토큰 만료 시간)과 rtExpiration(리프레시 토큰 만료 시간) 추가

@github-actions
Copy link

github-actions bot commented Mar 26, 2024

Image description CodeRabbit

개요:

의존성 변경과 보안 관련 클래스 및 설정에 대한 다양한 수정이 있었습니다. PostgreSQL JDBC 드라이버, h2 라이브러리 추가, Jakarta Validation API 제거, modelmapper 및 OAuth2 의존성 추가 등이 포함되어 있습니다.

상세 정보:

파일 경로 변경 내용
build.gradle PostgreSQL JDBC 드라이버, h2 라이브러리 추가, Jakarta Validation API 제거
AppConfig.java ModelMapper와 BCryptPasswordEncoder Bean 추가
SecurityConfig.java Spring Security 설정 변경, JwtAuthenticationFilter, JwtTokenProvider, CustomOAuth2UserService 수정
JwtAuthenticationFilter.java 패키지명 변경, 주석 제거, 코드 라인 주석 처리
JwtTokenProvider.java 패키지명 변경, CustomUser 대신 CustomUserDetails 사용, UserRepository 추가, 토큰 만료기한 포맷 메서드 추가
OAuth2Attribute.java 새로운 OAuth2Attribute 클래스 추가
CustomAuthenticationFailHandler.java 새로운 CustomAuthenticationFailHandler 클래스 추가
CustomAuthenticationSuccessHandler.java 새로운 CustomAuthenticationSuccessHandler 클래스 추가
CustomOAuth2UserService.java 새로운 CustomOAuth2UserService 클래스 추가
CustomUser.java CustomUser 클래스 삭제, CustomUserDetails 클래스 추가
TokenDto.java accessTokenExpiresIn 필드 atExpiration과 rtExpiration으로 변경
Token.java Token 클래스에 atExpiration과 rtExpiration 추가

Uplevel your code reviews with CodeRabbit Pro

CodeRabbit Pro

If you like this project, please support us by purchasing the Pro version. The Pro version has advanced context, superior noise reduction and several proprietary improvements compared to the open source version. Moreover, CodeRabbit Pro is free for open source projects.

@sin-hyunjin sin-hyunjin self-assigned this Mar 26, 2024
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

Commits Files that changed from the base of the PR and between 384453f and e3b0a89 commits.
Files selected (41)
  • build.gradle (2)
  • src/main/java/com/chukapoka/server/common/authority/AppConfig.java (1)
  • src/main/java/com/chukapoka/server/common/authority/SecurityConfig.java (1)
  • src/main/java/com/chukapoka/server/common/authority/jwt/JwtAuthenticationFilter.java (3)
  • src/main/java/com/chukapoka/server/common/authority/jwt/JwtTokenProvider.java (9)
  • src/main/java/com/chukapoka/server/common/authority/oauth2/dto/OAuth2Attribute.java (1)
  • src/main/java/com/chukapoka/server/common/authority/oauth2/handler/CustomAuthenticationFailHandler.java (1)
  • src/main/java/com/chukapoka/server/common/authority/oauth2/handler/CustomAuthenticationSuccessHandler.java (1)
  • src/main/java/com/chukapoka/server/common/authority/oauth2/service/CustomOAuth2UserService.java (1)
  • src/main/java/com/chukapoka/server/common/dto/CustomUser.java (1)
  • src/main/java/com/chukapoka/server/common/dto/CustomUserDetails.java (1)
  • src/main/java/com/chukapoka/server/common/dto/TokenDto.java (1)
  • src/main/java/com/chukapoka/server/common/entity/Token.java (2)
  • src/main/java/com/chukapoka/server/common/enums/TreeType.java (1)
  • src/main/java/com/chukapoka/server/common/repository/TokenRepository.java (1)
  • src/main/java/com/chukapoka/server/common/service/CustomUserDetailsService.java (2)
  • src/main/java/com/chukapoka/server/tree/controller/TreeController.java (1)
  • src/main/java/com/chukapoka/server/tree/dto/TreeCreateRequestDto.java (1)
  • src/main/java/com/chukapoka/server/tree/dto/TreeDetailResponseDto.java (1)
  • src/main/java/com/chukapoka/server/tree/dto/TreeList.java (1)
  • src/main/java/com/chukapoka/server/tree/dto/TreeListResponseDto.java (1)
  • src/main/java/com/chukapoka/server/tree/dto/TreeModifyRequestDto.java (1)
  • src/main/java/com/chukapoka/server/tree/entity/Tree.java (1)
  • src/main/java/com/chukapoka/server/tree/repository/TreeRepository.java (1)
  • src/main/java/com/chukapoka/server/tree/service/TreeService.java (1)
  • src/main/java/com/chukapoka/server/tree/service/TreeServiceImpl.java (1)
  • src/main/java/com/chukapoka/server/treeItem/controller/TreeItemController.java (1)
  • src/main/java/com/chukapoka/server/treeItem/dto/TreeItemCreateRequestDto.java (1)
  • src/main/java/com/chukapoka/server/treeItem/dto/TreeItemDetailResponseDto.java (1)
  • src/main/java/com/chukapoka/server/treeItem/dto/TreeItemListResponseDto.java (1)
  • src/main/java/com/chukapoka/server/treeItem/dto/TreeItemModifyRequestDto.java (1)
  • src/main/java/com/chukapoka/server/treeItem/entity/TreeItem.java (1)
  • src/main/java/com/chukapoka/server/treeItem/repository/TreeItemRepository.java (1)
  • src/main/java/com/chukapoka/server/treeItem/service/TreeItemService.java (1)
  • src/main/java/com/chukapoka/server/treeItem/service/TreeItemServiceImpl.java (1)
  • src/main/java/com/chukapoka/server/user/controller/HealthController.java (1)
  • src/main/java/com/chukapoka/server/user/controller/UserController.java (2)
  • src/main/java/com/chukapoka/server/user/dto/UserResponseDto.java (1)
  • src/main/java/com/chukapoka/server/user/entity/User.java (1)
  • src/main/java/com/chukapoka/server/user/repository/UserRepository.java (1)
  • src/main/java/com/chukapoka/server/user/sevice/UserService.java (3)
Files ignored due to filter (2)
  • screenshots/Oauth2.png
  • src/main/resources/application.yaml
Files skipped from review due to trivial changes (10)
  • src/main/java/com/chukapoka/server/common/authority/AppConfig.java
  • src/main/java/com/chukapoka/server/common/dto/CustomUser.java
  • src/main/java/com/chukapoka/server/common/repository/TokenRepository.java
  • src/main/java/com/chukapoka/server/tree/dto/TreeList.java
  • src/main/java/com/chukapoka/server/tree/dto/TreeListResponseDto.java
  • src/main/java/com/chukapoka/server/treeItem/dto/TreeItemListResponseDto.java
  • src/main/java/com/chukapoka/server/user/controller/HealthController.java
  • src/main/java/com/chukapoka/server/user/dto/UserResponseDto.java
  • src/main/java/com/chukapoka/server/user/repository/UserRepository.java
  • src/main/java/com/chukapoka/server/common/authority/SecurityConfig.java (diff too large)
Review comments generated (28)
  • Review: 19
  • LGTM: 9

Tips

Chat with Image description CodeRabbit Bot (@coderabbitai)

  • Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
  • Invite the bot into a review comment chain by tagging @coderabbitai in a reply.

Code suggestions

  • The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
  • You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.

Pausing incremental reviews

  • Add @coderabbitai: ignore anywhere in the PR description to pause further reviews from the bot.

Comment on lines 25 to 30

public static final String AUTHORIZATION_HEADER = "Authorization";
public static final String BEARER_PREFIX = "Bearer";

private final JwtTokenProvider jwtTokenProvider;
private final TokenRepository tokenRepository;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [24-25]

이전 코드와 비교하여 변경된 부분이 없습니다.

Comment on lines +26 to +27
public class CustomOAuth2UserService implements OAuth2UserService<OAuth2UserRequest, OAuth2User> {
private final UserRepository userRepository;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

CustomOAuth2UserService 클래스에서 loadUser 메서드가 구현되어 있습니다. 해당 메서드는 OAuth2 사용자 정보를 가져오고 회원 가입 여부를 확인하여 처리하는 중요한 역할을 합니다.

Comment on lines +40 to +42
String registrationId = userRequest.getClientRegistration().getRegistrationId();
String userNameAttributeName = userRequest.getClientRegistration()
.getProviderDetails().getUserInfoEndpoint().getUserNameAttributeName();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

클라이언트 등록 ID와 사용자 이름 속성을 가져오는 부분입니다. 이 정보들은 OAuth2 로그인 시 필요한 중요한 정보이므로 정확하게 가져오는 것이 중요합니다.

Comment on lines +46 to +49
OAuth2Attribute.of(registrationId, userNameAttributeName, oAuth2User.getAttributes());

// 5. OAuth2Attribute의 속성값들을 Map으로 반환 받는다.
Map<String, Object> memberAttribute = oAuth2Attribute.convertToMap();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

OAuth2Attribute 객체를 생성하고 속성값을 Map으로 변환하는 부분입니다. 이 과정에서 속성값들이 올바르게 매핑되고 있는지 확인해야 합니다.

Comment on lines +52 to +53
String email = (String) memberAttribute.get("email");
log.debug("Email retrieved from OAuth2 attributes: {}", email);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

이메일 정보를 가져오는 부분입니다. 이메일은 회원 식별에 중요한 정보이므로 올바르게 가져오는지 확인해야 합니다.

Comment on lines 31 to 50

// 만료 시간을 나타내는 컬럼 추가
@Column(name = "at_expiration")
private String atExpiration; // access token 만료 시간

@Column(name = "rt_expiration")
private String rtExpiration; // refresh token 만료 시간

@Builder
public Token(String key, String atValue, String rtValue) {
public Token(String key, String atValue, String rtValue, String atExpiration, String rtExpiration) {
this.key = key;
this.atValue = atValue;
this.rtValue = rtValue;
this.atExpiration = atExpiration;
this.rtExpiration = rtExpiration;
}

public Token updateValues(String accessToken, String refreshToken) {
this.atValue = accessToken;
this.rtValue = refreshToken;
return this;
}


public TokenResponseDto toResponseDto(){
return new TokenResponseDto(this.atValue);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

새로운 생성자 및 메서드가 추가되었습니다. 이에 대한 로직 및 역할을 확인하고 필요한 경우 주석을 추가하거나 코드를 수정해야 합니다.

Comment on lines 1 to 11
package com.chukapoka.server.treeItem.dto;

import lombok.Data;

@Data
public class TreeItemModifyRequestDto {

private String title;
private String content;
private String treeItemColor;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

Note: This review was outside of the patch, but no patch was found that overlapped with it. Original lines [22-22]

이 부분에서 CustomUser 클래스가 삭제되고 CustomUserDetails 클래스가 추가되었습니다. 이에 대한 변경 사항을 확인해야 합니다.

-    retrn z
+    return z

Comment on lines 1 to 12
package com.chukapoka.server.treeItem.repository;

import com.chukapoka.server.treeItem.entity.TreeItem;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface TreeItemRepository extends JpaRepository<TreeItem, String> {
List<TreeItem> findByTreeId(String treeId);
Optional<TreeItem> findByIdAndUpdatedBy(String treeItemId, long userId);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

Note: This review was outside of the patch, but no patch was found that overlapped with it. Original lines [22-22]

There's a typo in the add function.

-    retrn z
+    return z

@PostMapping("/reissue")
public BaseResponse<TokenResponseDto> reissue() {
long userId = ((CustomUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
long userId = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The type casting from SecurityContextHolder.getContext().getAuthentication().getPrincipal() seems to have changed from CustomUser to CustomUserDetails. Please verify if this change is intentional and ensure it aligns with the rest of the codebase.

-        long userId = ((CustomUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
+        long userId = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();

public BaseResponse<ResultType> logout() {
// 인증된 사용자 Id
long userId = ((CustomUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
long userId = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

Similar to the previous comment, there is a type casting change from CustomUser to CustomUserDetails. Verify if this change is intended and consistent with other parts of the code.

-        long userId = ((CustomUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
+        long userId = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();

@doyou1 doyou1 changed the base branch from dev to feat/27-user,tree,treeITem+jwt March 26, 2024 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OAUTH2.0 Version

2 participants