Skip to content

Commit

Permalink
UPDATE: PrincipalDetail과 UserDetailsImpl 통합
Browse files Browse the repository at this point in the history
  • Loading branch information
BYEONGRYEOL committed Apr 29, 2024
1 parent f63c3c2 commit 9d5b3fd
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 183 deletions.
30 changes: 9 additions & 21 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ configurations {
extendsFrom annotationProcessor
}
}
configurations.all {
exclude group: 'commons-logging', module: 'commons-logging'
}

repositories {
mavenCentral()
Expand Down Expand Up @@ -46,16 +49,16 @@ dependencies {
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5'

// querydsl 설정
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:5.0.0:jakarta"
// Unable to load class 'javax.persistence.Entity'. 오류 방지
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"
// implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
// annotationProcessor "com.querydsl:querydsl-apt:5.0.0:jakarta"
// Unable to load class 'javax.persistence.Entity'. 오류 방지
// annotationProcessor "jakarta.annotation:jakarta.annotation-api"
// annotationProcessor "jakarta.persistence:jakarta.persistence-api"

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
Expand All @@ -76,19 +79,4 @@ test {

jar {
enabled = false
}

// === ⭐ QueryDsl 빌드 옵션 (선택) ===
def querydslDir = "$buildDir/generated/querydsl"

sourceSets {
main.java.srcDirs += [ querydslDir ]
}

tasks.withType(JavaCompile).configureEach {
options.annotationProcessorGeneratedSourcesDirectory = file(querydslDir)
}

clean.doLast {
file(querydslDir).deleteDir()
}
4 changes: 2 additions & 2 deletions src/main/java/com/gt/genti/aop/UserAop.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import com.gt.genti.security.PrincipalDetail;
import com.gt.genti.config.auth.UserDetailsImpl;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -13,7 +13,7 @@
@Slf4j
public class UserAop {
@Before("@annotation(com.gt.genti.aop.annotation.CheckUserIsQuit) && args(principalDetail)")
public void checkUserIsQuit(final PrincipalDetail principalDetail) {
public void checkUserIsQuit(final UserDetailsImpl principalDetail) {
log.info("유저탈퇴확인aop실행");
if (!principalDetail.isEnabled()) {
throw new RuntimeException("탈퇴한 사용자입니다.");
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/com/gt/genti/config/QueryDSLConfig.java

This file was deleted.

17 changes: 16 additions & 1 deletion src/main/java/com/gt/genti/config/auth/UserDetailsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.gt.genti.domain.User;

import lombok.Builder;
import lombok.Getter;

public class UserDetailsImpl implements UserDetails, OAuth2User, Serializable {

Expand All @@ -28,10 +29,20 @@ public class UserDetailsImpl implements UserDetails, OAuth2User, Serializable {
private String nickname; //닉네임
private Collection<GrantedAuthority> authorities; //권한 목록

@Getter
private User user;
private Map<String, Object> attributes;

//Social Login 용
//non socical 용
@Builder
public UserDetailsImpl(User user, String roles) {
//PrincipalOauth2UserService 참고
this.user = user;
this.id = user.getId().toString();
this.authorities = createAuthorities(roles);
}

// social 용
@Builder
public UserDetailsImpl(User user, String roles, Map<String, Object> attributes) {
//PrincipalOauth2UserService 참고
Expand All @@ -41,6 +52,10 @@ public UserDetailsImpl(User user, String roles, Map<String, Object> attributes)
this.attributes = attributes;
}

public Long getId() {
return Long.parseLong(this.id);
}

//Non Social + Employer 로그인 용도
@Builder
public UserDetailsImpl(Long authId, String roles, String userEmail, String userPw, boolean emailVerified,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.gt.genti.config.auth;

import java.util.Collections;

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 com.gt.genti.domain.enums.UserRole;
import com.gt.genti.repository.UserRepository;
import com.gt.genti.security.PrincipalDetail;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -28,8 +25,8 @@ public UserDetails loadUserByUsername(String email) throws UsernameNotFoundExcep

return userRepository.findByEmail(email)
.map(
findUser -> new PrincipalDetail(findUser,
Collections.singleton(new SimpleGrantedAuthority(findUser.getRole()))))
findUser -> new UserDetailsImpl(findUser,
UserRole.getAllRoles(findUser.getUserRole())))
.orElseThrow(() -> new UsernameNotFoundException("등록되지 않은 사용자입니다"));
}
}
19 changes: 12 additions & 7 deletions src/main/java/com/gt/genti/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.gt.genti.controller;

import static com.gt.genti.util.ApiUtils.*;

import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import com.gt.genti.fortest.TestJwtResponseDto;
import com.gt.genti.security.JwtTokenProvider;
import com.gt.genti.util.ApiUtils;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -19,16 +25,15 @@ public class LoginController {

@GetMapping("/oauth2")
public String index() {
log.info("인덱스 컨트롤러 접근");
return "oauth2";
}

// @GetMapping("/login/testjwt")
// public ResponseEntity<ApiResult<TestJwtResponseDto>> getTestJwt() {
//
// String accessToken = jwtTokenProvider.generateToken(tempClaim, 1000000);
// return success(TestJwtResponseDto.builder().accessToken(accessToken).refreshToken(accessToken).build());
// }
@GetMapping("/login/testjwt")
public ResponseEntity<ApiResult<TestJwtResponseDto>> getTestJwt() {
Map<String, Object> tempClaim = Map.of("auth", "ROLE_USER", "sub", "3");
String accessToken = jwtTokenProvider.generateToken(tempClaim, 1000000);
return success(TestJwtResponseDto.builder().accessToken(accessToken).refreshToken(accessToken).build());
}

@GetMapping("/oauth2/login")
public String oauth() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import org.springframework.web.bind.annotation.RestController;

import com.gt.genti.aop.annotation.CheckUserIsQuit;
import com.gt.genti.config.auth.UserDetailsImpl;
import com.gt.genti.dto.PictureGenerateRequestDetailResponseDto;
import com.gt.genti.dto.PictureGenerateRequestModifyDto;
import com.gt.genti.dto.PictureGenerateRequestRequestDto;
import com.gt.genti.dto.PictureGenerateRequestResponseDto;
import com.gt.genti.security.PrincipalDetail;
import com.gt.genti.service.PictureGenerateRequestService;

import lombok.RequiredArgsConstructor;
Expand All @@ -32,29 +32,29 @@ public class PictureGenerateRequestController {
@CheckUserIsQuit
@GetMapping("/active")
public ResponseEntity<ApiResult<List<PictureGenerateRequestDetailResponseDto>>> getMyActivePictureGenerateRequest(
@AuthenticationPrincipal PrincipalDetail principalDetail) {
@AuthenticationPrincipal UserDetailsImpl userDetails) {
return success(
pictureGenerateRequestService.getMyActivePictureGenerateRequest(principalDetail.getUser().getId()));
pictureGenerateRequestService.getMyActivePictureGenerateRequest(userDetails.getId()));
}

@CheckUserIsQuit
@PostMapping("")
public ResponseEntity<ApiResult<PictureGenerateRequestResponseDto>> createPictureGenerateRequest(
@AuthenticationPrincipal PrincipalDetail principalDetail,
@AuthenticationPrincipal UserDetailsImpl userDetails,
@RequestBody PictureGenerateRequestRequestDto pictureGenerateRequestRequestDto) {

return success(
pictureGenerateRequestService.createPictureGenerateRequest(principalDetail.getUser(),
pictureGenerateRequestService.createPictureGenerateRequest(userDetails.getId(),
pictureGenerateRequestRequestDto));
}

@CheckUserIsQuit
@PutMapping("")
public ResponseEntity<ApiResult<Boolean>> modifyPictureGenerateRequest(
@AuthenticationPrincipal PrincipalDetail principalDetail,
@AuthenticationPrincipal UserDetailsImpl userDetails,
@RequestBody PictureGenerateRequestModifyDto pictureGenerateRequestModifyDto) {
return success(
pictureGenerateRequestService.modifyPictureGenerateRequest(principalDetail.getUser().getId(),
pictureGenerateRequestService.modifyPictureGenerateRequest(userDetails.getId(),
pictureGenerateRequestModifyDto));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gt.genti.config.auth.UserDetailsImpl;
import com.gt.genti.dto.PictureGenerateRequestBriefResponseDto;
import com.gt.genti.security.PrincipalDetail;
import com.gt.genti.service.PictureGenerateWorkService;

import lombok.RequiredArgsConstructor;
Expand All @@ -23,8 +23,8 @@ public class PictureGenerateWorkController {
@GetMapping("")
public ResponseEntity<ApiResult<PictureGenerateRequestBriefResponseDto>> getMyAssignedPictureGenerateRequest(
@AuthenticationPrincipal
PrincipalDetail principalDetail) {
UserDetailsImpl userDetails) {
return success(pictureGenerateWorkService.getCreatorAssignedPictureGenerateRequestBrief(
principalDetail.getUser().getId()));
userDetails.getId()));
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/gt/genti/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import org.springframework.web.bind.annotation.RestController;

import com.gt.genti.aop.annotation.CheckUserIsQuit;
import com.gt.genti.config.auth.UserDetailsImpl;
import com.gt.genti.dto.PostBriefResponseDto;
import com.gt.genti.dto.PostDetailResponseDto;
import com.gt.genti.security.PrincipalDetail;
import com.gt.genti.service.PostService;

import lombok.RequiredArgsConstructor;
Expand All @@ -37,9 +37,9 @@ public ResponseEntity<ApiResult<List<PostDetailResponseDto>>> getAllPostsDetailP
@CheckUserIsQuit
@GetMapping("/detail/my")
public ResponseEntity<ApiResult<List<PostDetailResponseDto>>> getMyAllPostsDetailPagination(
@AuthenticationPrincipal PrincipalDetail principalDetail,
@AuthenticationPrincipal UserDetailsImpl userDetails,
@RequestParam(value = "cursor", required = false) Long cursor) {
return success(postService.getPostDetailAllByUserIdPagination(principalDetail.getUser().getId(), cursor));
return success(postService.getPostDetailAllByUserIdPagination(userDetails.getId(), cursor));
}

@CheckUserIsQuit
Expand All @@ -60,8 +60,8 @@ public ResponseEntity<ApiResult<List<PostBriefResponseDto>>> getUsersAllPostBrie
@CheckUserIsQuit
@GetMapping("/brief/my")
public ResponseEntity<ApiResult<List<PostBriefResponseDto>>> getUsersAllPostBrief(
@AuthenticationPrincipal PrincipalDetail principalDetail) {
return success(postService.getPostBriefAllByUserId(principalDetail.getUser().getId()));
@AuthenticationPrincipal UserDetailsImpl userDetails) {
return success(postService.getPostBriefAllByUserId(userDetails.getId()));
}

}
18 changes: 9 additions & 9 deletions src/main/java/com/gt/genti/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import org.springframework.web.bind.annotation.RestController;

import com.gt.genti.aop.annotation.CheckUserIsQuit;
import com.gt.genti.config.auth.UserDetailsImpl;
import com.gt.genti.dto.UserInfoResponseDto;
import com.gt.genti.dto.UserInfoUpdateRequestDto;
import com.gt.genti.security.PrincipalDetail;
import com.gt.genti.service.UserService;
import com.gt.genti.util.ApiUtils;

Expand All @@ -29,29 +29,29 @@ public class UserController {
@CheckUserIsQuit
@GetMapping("")
public ResponseEntity<ApiUtils.ApiResult<UserInfoResponseDto>> getUserInfo(
@AuthenticationPrincipal PrincipalDetail principalDetail) {
return success(userService.getUserInfo(principalDetail.getUser().getId()));
@AuthenticationPrincipal UserDetailsImpl userDetails) {
return success(userService.getUserInfo(userDetails.getId()));
}

@CheckUserIsQuit
@PutMapping("")
public ResponseEntity<ApiUtils.ApiResult<UserInfoResponseDto>> updateUserInfo(
@AuthenticationPrincipal PrincipalDetail principalDetail,
@AuthenticationPrincipal UserDetailsImpl userDetails,
@RequestBody UserInfoUpdateRequestDto userInfoUpdateRequestDto) {
return success(userService.updateUserInfo(principalDetail.getUser().getId(), userInfoUpdateRequestDto));
return success(userService.updateUserInfo(userDetails.getId(), userInfoUpdateRequestDto));
}

@CheckUserIsQuit
@DeleteMapping("")
public ResponseEntity<ApiUtils.ApiResult<Boolean>> deleteUserSoft(
@AuthenticationPrincipal PrincipalDetail principalDetail) {
return success(userService.deleteUserInfoSoft(principalDetail.getUser().getId()));
@AuthenticationPrincipal UserDetailsImpl userDetails) {
return success(userService.deleteUserInfoSoft(userDetails.getId()));
}

@PutMapping("/restore")
public ResponseEntity<ApiUtils.ApiResult<Boolean>> restoreSoftDeletedUser(
@AuthenticationPrincipal PrincipalDetail principalDetail) {
return success(userService.restoreSoftDeletedUser(principalDetail.getUser().getId()));
@AuthenticationPrincipal UserDetailsImpl userDetails) {
return success(userService.restoreSoftDeletedUser(userDetails.getId()));
}

}
4 changes: 0 additions & 4 deletions src/main/java/com/gt/genti/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ public static User createNewSocialUser(OAuthAttributes oauthAttributes) {
return new User(email, username, nickname, oauthType, UserRole.USER);
}

public String getRole() {
return this.getUserRole().getStringValue();
}

public void update(UserInfoUpdateRequestDto userInfoUpdateRequestDto) {
this.username = userInfoUpdateRequestDto.getUserName();
this.getProfilePicture().getPicture().modify(userInfoUpdateRequestDto.getProfilePictureUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public static <E extends Enum<E> & ConvertableEnum> E stringToEnum(Class<E> enum

public static <E extends Enum<E> & ConvertableEnum> E stringToEnumIgnoreCase(Class<E> enumType, String value) {
for (E enumValue : enumType.getEnumConstants()) {
log.info("enumValue.name() : " + enumValue.name());
log.info("enumValue.getStringValue() : " + enumValue.getStringValue());
log.info("value : " + value);
// log.info("enumValue.name() : " + enumValue.name());
// log.info("enumValue.getStringValue() : " + enumValue.getStringValue());
// log.info("value : " + value);
if (StringUtils.equalsIgnoreCase(value, enumValue.getStringValue())) {
return enumValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public class S3Controller {
public ResponseEntity<ApiResult<PreSignedUrlResponseDto>> getPreSignedUrl(
@AuthenticationPrincipal UserDetailsImpl userDetails,
@RequestBody PreSignedUrlRequestDto preSignedUrlRequestDto) {
return success(s3Service.getPreSignedUrl(preSignedUrlRequestDto));
return success(s3Service.getPreSignedUrl(userDetails.getId(), preSignedUrlRequestDto));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class S3Service {

private final AmazonS3 amazonS3;

public PreSignedUrlResponseDto getPreSignedUrl(PreSignedUrlRequestDto preSignedUrlRequestDto) {
public PreSignedUrlResponseDto getPreSignedUrl(Long userId, PreSignedUrlRequestDto preSignedUrlRequestDto) {
return getPreSignedUrl(preSignedUrlRequestDto.getFileType().getStringValue(), preSignedUrlRequestDto);
}

Expand Down
Loading

0 comments on commit 9d5b3fd

Please sign in to comment.