Skip to content

Commit

Permalink
Merge pull request #135 from Mile-Writings/feat/#132
Browse files Browse the repository at this point in the history
#132 [feat] principal null exception 해결 및 secure url 로직 분리
  • Loading branch information
sohyundoh authored Jan 16, 2024
2 parents a0c8fc5 + 992edb5 commit 1a58ef1
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mile.controller.comment;


import com.mile.authentication.PrincipalHandler;
import com.mile.comment.service.CommentService;
import com.mile.dto.SuccessResponse;
import com.mile.exception.message.SuccessMessage;
Expand All @@ -19,14 +20,14 @@
public class CommentController implements CommentControllerSwagger{

private final CommentService commentService;
private final PrincipalHandler principalHandler;

@DeleteMapping("/{commentId}")
public SuccessResponse deleteComment(
@CommentIdPathVariable final Long commentId,
final Principal principal,
@PathVariable("commentId") final String commentUrl
) {
commentService.deleteComment(commentId, Long.valueOf(principal.getName()));
commentService.deleteComment(commentId, principalHandler.getUserIdFromPrincipal());
return SuccessResponse.of(SuccessMessage.COMMENT_DELETE_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public interface CommentControllerSwagger {
)
SuccessResponse deleteComment(
final Long commentId,
final Principal principal,
@PathVariable("commentId") final String commentUrl
);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mile.controller.moim;

import com.mile.authentication.PrincipalHandler;
import com.mile.dto.SuccessResponse;
import com.mile.exception.message.SuccessMessage;
import com.mile.moim.service.MoimService;
Expand All @@ -14,9 +15,8 @@
import com.mile.resolver.moim.MoimIdPathVariable;
import com.mile.writerName.service.dto.PopularWriterListResponse;

import java.security.Principal;

import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -28,25 +28,24 @@
public class MoimController implements MoimControllerSwagger {

private final MoimService moimService;
private final PrincipalHandler principalHandler;

@Override
@GetMapping("/{moimId}")
public SuccessResponse<ContentListResponse> getTopicsFromMoim(
@MoimIdPathVariable final Long moimId,
final Principal principal,
@PathVariable("moimId") final String moimUrl
) {
return SuccessResponse.of(SuccessMessage.TOPIC_SEARCH_SUCCESS, moimService.getContentsFromMoim(moimId, Long.valueOf(principal.getName())));
return SuccessResponse.of(SuccessMessage.TOPIC_SEARCH_SUCCESS, moimService.getContentsFromMoim(moimId, principalHandler.getUserIdFromPrincipal()));
}

@Override
@GetMapping("/{moimId}/authenticate")
public SuccessResponse<MoimAuthenticateResponse> getAuthenticationOfMoim(
@MoimIdPathVariable final Long moimId,
final Principal principal,
@PathVariable("moimId") final String moimUrl
) {
return SuccessResponse.of(SuccessMessage.MOIM_AUTHENTICATE_SUCCESS, moimService.getAuthenticateUserOfMoim(moimId, Long.valueOf(principal.getName())));
return SuccessResponse.of(SuccessMessage.MOIM_AUTHENTICATE_SUCCESS, moimService.getAuthenticateUserOfMoim(moimId, principalHandler.getUserIdFromPrincipal()));
}

@Override
Expand Down Expand Up @@ -105,9 +104,8 @@ public SuccessResponse<BestMoimListResponse> getBestMoimAndPostList() {
@GetMapping("/{moimId}/temporary")
public SuccessResponse<TemporaryPostExistResponse> getTemporaryPost(
@MoimIdPathVariable final Long moimId,
final Principal principal,
@PathVariable("moimId") final String moimUrl
) {
return SuccessResponse.of(SuccessMessage.IS_TEMPORARY_POST_EXIST_GET_SUCCESS, moimService.getTemporaryPost(moimId, Long.valueOf(principal.getName())));
return SuccessResponse.of(SuccessMessage.IS_TEMPORARY_POST_EXIST_GET_SUCCESS, moimService.getTemporaryPost(moimId, principalHandler.getUserIdFromPrincipal()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public interface MoimControllerSwagger {
)
SuccessResponse<ContentListResponse> getTopicsFromMoim(
@Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long moimId,
final Principal principal,
@PathVariable("moimId") final String moimUrl
);

Expand All @@ -56,7 +55,6 @@ SuccessResponse<ContentListResponse> getTopicsFromMoim(
)
SuccessResponse getAuthenticationOfMoim(
final Long moimId,
final Principal principal,
@PathVariable("moimId") final String moimUrl
);

Expand Down Expand Up @@ -157,7 +155,6 @@ SuccessResponse<TopicListResponse> getTopicList(
)
SuccessResponse<TemporaryPostExistResponse> getTemporaryPost(
final Long moimId,
final Principal principal,
@PathVariable("moimId") final String moimUrl
);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mile.controller.post;

import com.mile.authentication.PrincipalHandler;
import com.mile.curious.service.dto.CuriousInfoResponse;
import com.mile.dto.SuccessResponse;
import com.mile.exception.message.SuccessMessage;
Expand All @@ -17,6 +18,7 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -35,18 +37,18 @@
public class PostController implements PostControllerSwagger {

private final PostService postService;
private final PrincipalHandler principalHandler;

@PostMapping("/{postId}/comment")
@Override
public SuccessResponse postComment(
@PostIdPathVariable final Long postId,
@Valid @RequestBody final CommentCreateRequest commentCreateRequest,
final Principal principal,
@PathVariable("postId") final String postUrl
) {
postService.createCommentOnPost(
postId,
Long.valueOf(principal.getName()),
principalHandler.getUserIdFromPrincipal(),
commentCreateRequest
);
return SuccessResponse.of(SuccessMessage.COMMENT_CREATE_SUCCESS);
Expand All @@ -57,85 +59,77 @@ public SuccessResponse postComment(
@Override
public SuccessResponse<PostCuriousResponse> postCurious(
@PostIdPathVariable final Long postId,
final Principal principal,
@PathVariable("postId") final String postUrl
) {
return SuccessResponse.of(SuccessMessage.CURIOUS_CREATE_SUCCESS, postService.createCuriousOnPost(postId, Long.valueOf(principal.getName())));
return SuccessResponse.of(SuccessMessage.CURIOUS_CREATE_SUCCESS, postService.createCuriousOnPost(postId, principalHandler.getUserIdFromPrincipal()));
}

@GetMapping("/{postId}/comment")
@Override
public SuccessResponse<CommentListResponse> getComments(
@PostIdPathVariable final Long postId,
final Principal principal,
@PathVariable("postId") final String postUrl
) {
return SuccessResponse.of(SuccessMessage.COMMENT_SEARCH_SUCCESS, postService.getComments(postId, Long.valueOf(principal.getName())));
return SuccessResponse.of(SuccessMessage.COMMENT_SEARCH_SUCCESS, postService.getComments(postId, principalHandler.getUserIdFromPrincipal()));
}


@GetMapping("/{postId}/curiousInfo")
@Override
public SuccessResponse<CuriousInfoResponse> getCuriousInfo(
@PostIdPathVariable final Long postId,
final Principal principal,
@PathVariable("postId") final String postUrl
) {
return SuccessResponse.of(SuccessMessage.CURIOUS_INFO_SEARCH_SUCCESS, postService.getCuriousInfo(postId, Long.valueOf(principal.getName())));
return SuccessResponse.of(SuccessMessage.CURIOUS_INFO_SEARCH_SUCCESS, postService.getCuriousInfo(postId, principalHandler.getUserIdFromPrincipal()));
}

@DeleteMapping("/{postId}/curious")
@Override
public SuccessResponse<PostCuriousResponse> deleteCurious(
@PostIdPathVariable final Long postId,
final Principal principal,
@PathVariable("postId") final String postUrl
) {
return SuccessResponse.of(SuccessMessage.CURIOUS_DELETE_SUCCESS, postService.deleteCuriousOnPost(postId, Long.valueOf(principal.getName())));
return SuccessResponse.of(SuccessMessage.CURIOUS_DELETE_SUCCESS, postService.deleteCuriousOnPost(postId, principalHandler.getUserIdFromPrincipal()));
}

@GetMapping("/{postId}/authenticate")
@Override
public SuccessResponse getAuthenticateWrite(
@PostIdPathVariable final Long postId,
final Principal principal,
@PathVariable("postId") final String postUrl
) {
return SuccessResponse.of(SuccessMessage.WRITER_AUTHENTIACTE_SUCCESS, postService.getAuthenticateWriter(postId, Long.valueOf(principal.getName())));
return SuccessResponse.of(SuccessMessage.WRITER_AUTHENTIACTE_SUCCESS, postService.getAuthenticateWriter(postId, principalHandler.getUserIdFromPrincipal()));
}

@PutMapping("/{postId}")
@Override
public SuccessResponse putPost(
@PostIdPathVariable final Long postId,
@Valid @RequestBody final PostPutRequest putRequest,
final Principal principal,
@PathVariable("postId") final String postUrl
) {
postService.updatePost(postId, Long.valueOf(principal.getName()), putRequest);
postService.updatePost(postId, principalHandler.getUserIdFromPrincipal(), putRequest);
return SuccessResponse.of(SuccessMessage.POST_PUT_SUCCESS);
}

@DeleteMapping("/{postId}")
@Override
public SuccessResponse deletePost(
@PostIdPathVariable final Long postId,
final Principal principal,
@PathVariable("postId") final String postUrl
) {
postService.deletePost(postId, Long.valueOf(principal.getName()));
postService.deletePost(postId, principalHandler.getUserIdFromPrincipal());
return SuccessResponse.of(SuccessMessage.POST_DELETE_SUCCESS);
}

@Override
@GetMapping("/temporary/{postId}")
public SuccessResponse<TemporaryPostGetResponse> getTemporaryPost(
@PostIdPathVariable final Long postId,
final Principal principal,
@PathVariable("postId") final String postUrl
) {
return SuccessResponse.of(SuccessMessage.TEMPORARY_POST_GET_SUCCESS,
postService.getTemporaryPost(postId, Long.valueOf(principal.getName())));
postService.getTemporaryPost(postId, principalHandler.getUserIdFromPrincipal()));
}

@Override
Expand All @@ -151,22 +145,20 @@ public SuccessResponse<PostGetResponse> getPost(
@Override
@PostMapping
public SuccessResponse<WriterNameResponse> createPost(
@Valid @RequestBody final PostCreateRequest postCreateRequest,
final Principal principal
@Valid @RequestBody final PostCreateRequest postCreateRequest
) {
return SuccessResponse.of(SuccessMessage.POST_CREATE_SUCCESS, postService.createPost(
Long.valueOf(principal.getName()),
principalHandler.getUserIdFromPrincipal(),
postCreateRequest
));
}

@PostMapping("/temporary")
public SuccessResponse createTemporaryPost(
@RequestBody final TemporaryPostCreateRequest temporaryPostCreateRequest,
final Principal principal
@RequestBody final TemporaryPostCreateRequest temporaryPostCreateRequest
) {
postService.createTemporaryPost(
Long.valueOf(principal.getName()),
principalHandler.getUserIdFromPrincipal(),
temporaryPostCreateRequest
);
return SuccessResponse.of(SuccessMessage.TEMPORARY_POST_CREATE_SUCCESS);
Expand All @@ -175,12 +167,11 @@ public SuccessResponse createTemporaryPost(
@PutMapping("/temporary/{postId}")
public SuccessResponse<WriterNameResponse> putFixedPost(
@PostIdPathVariable final Long postId,
final Principal principal,
@RequestBody final PostPutRequest request,
@PathVariable("postId") final String postUrl
) {
return SuccessResponse.of(SuccessMessage.POST_CREATE_SUCCESS, postService.putFixedPost(
Long.valueOf(principal.getName()),
principalHandler.getUserIdFromPrincipal(),
request,
postId
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public interface PostControllerSwagger {
SuccessResponse postComment(
final Long postId,
@Valid @RequestBody final CommentCreateRequest commentCreateRequest,
final Principal principal,
@PathVariable("postId") final String postUrl
);

Expand All @@ -61,7 +60,6 @@ SuccessResponse postComment(
)
SuccessResponse<PostCuriousResponse> postCurious(
final Long postId,
final Principal principal,
@PathVariable("postId") final String postUrl
);

Expand All @@ -79,7 +77,6 @@ SuccessResponse<PostCuriousResponse> postCurious(
)
SuccessResponse<CommentListResponse> getComments(
final Long postId,
final Principal principal,
@PathVariable("postId") final String postUrl
);

Expand All @@ -96,7 +93,6 @@ SuccessResponse<CommentListResponse> getComments(
)
SuccessResponse<CuriousInfoResponse> getCuriousInfo(
final Long postId,
final Principal principal,
@PathVariable("postId") final String postUrl
);

Expand All @@ -113,7 +109,6 @@ SuccessResponse<CuriousInfoResponse> getCuriousInfo(
)
SuccessResponse<PostCuriousResponse> deleteCurious(
final Long postId,
final Principal principal,
@PathVariable("postId") final String postUrl
);

Expand All @@ -133,7 +128,6 @@ SuccessResponse<PostCuriousResponse> deleteCurious(
)
SuccessResponse getAuthenticateWrite(
final Long postId,
final Principal principal,
@PathVariable("postId") final String postUrl
);

Expand Down Expand Up @@ -163,7 +157,6 @@ SuccessResponse getAuthenticateWrite(
SuccessResponse putPost(
final Long postId,
@RequestBody final PostPutRequest putRequest,
final Principal principal,
@PathVariable("postId") final String postUrl
);

Expand All @@ -181,7 +174,6 @@ SuccessResponse putPost(
)
SuccessResponse deletePost(
final Long postId,
final Principal principal,
@PathVariable("postId") final String postUrl
);

Expand All @@ -197,7 +189,6 @@ SuccessResponse deletePost(
)
SuccessResponse<TemporaryPostGetResponse> getTemporaryPost(
final Long postId,
final Principal principal,
@PathVariable("postId") final String postUrl
);

Expand Down Expand Up @@ -233,8 +224,7 @@ SuccessResponse<PostGetResponse> getPost(
}
)
SuccessResponse createTemporaryPost(
@Valid @RequestBody final TemporaryPostCreateRequest temporaryPostCreateRequest,
final Principal principal
@Valid @RequestBody final TemporaryPostCreateRequest temporaryPostCreateRequest
);


Expand All @@ -255,8 +245,7 @@ SuccessResponse createTemporaryPost(
}
)
SuccessResponse createPost(
@Valid @RequestBody final PostCreateRequest postCreateRequest,
final Principal principal
@Valid @RequestBody final PostCreateRequest postCreateRequest
);

@Operation(summary = "임시 저장된 글 작성")
Expand All @@ -270,7 +259,7 @@ SuccessResponse createPost(

}
)
SuccessResponse<WriterNameResponse> putFixedPost(final Long postId, final Principal principal,
SuccessResponse<WriterNameResponse> putFixedPost(final Long postId,
@RequestBody final PostPutRequest request,
@PathVariable("postId") final String postUrl);
}
Loading

0 comments on commit 1a58ef1

Please sign in to comment.