Skip to content

Commit

Permalink
[#89] fix: security를 붙임에 따라 jwt 토큰 유저의 데이터를 내려주도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
yeseul106 committed Dec 5, 2023
1 parent 95feae0 commit 7afb7b5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class SecurityConfig {

@Bean
@Profile("dev")
SecurityFilterChain prodSecurityFilterChain(HttpSecurity http) throws Exception {
SecurityFilterChain devSecurityFilterChain(HttpSecurity http) throws Exception {
return http.csrf((csrfConfig) ->
csrfConfig.disable()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.sopt.makers.crew.main.common.response.CommonResponseDto;
import org.sopt.makers.crew.main.common.response.ErrorStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
Expand All @@ -25,9 +24,6 @@ public void commence(HttpServletRequest request, HttpServletResponse response,
public void setResponse(HttpServletResponse response, ErrorStatus status) throws IOException {
response.setContentType("application/json;charset=UTF-8");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

CommonResponseDto apiResponse = CommonResponseDto.fail(status.getErrorCode());
response.getWriter().println(mapper.writeValueAsString(apiResponse));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.sopt.makers.crew.main.common.util;

import java.security.Principal;
import lombok.RequiredArgsConstructor;
import org.sopt.makers.crew.main.common.exception.UnAuthorizedException;

@RequiredArgsConstructor
public class UserUtil {

public static Integer getUserId(Principal principal) {
if (principal == null) {
throw new UnAuthorizedException();
}
return Integer.valueOf(principal.getName());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package org.sopt.makers.crew.main.user.v2;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.security.Principal;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.sopt.makers.crew.main.common.util.UserUtil;
import org.sopt.makers.crew.main.user.v2.dto.response.UserV2GetAllMeetingByUserMeetingDto;
import org.sopt.makers.crew.main.user.v2.service.UserV2Service;
import org.springframework.http.HttpStatus;
Expand All @@ -20,10 +26,16 @@ public class UserV2Controller {

private final UserV2Service userV2Service;

@Operation(summary = "내가 속한 모임 조회")
@GetMapping("/meeting/all")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<List<UserV2GetAllMeetingByUserMeetingDto>> getAllMeetingByUser() {
Integer userId = 267; //현재는 security 붙이기 전이라 추후 수정
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "성공"),
@ApiResponse(responseCode = "204", description = "내가 속한 모임 리스트가 없는 경우", content = @Content),
})
public ResponseEntity<List<UserV2GetAllMeetingByUserMeetingDto>> getAllMeetingByUser(
Principal principal) {
Integer userId = UserUtil.getUserId(principal);
return ResponseEntity.ok(userV2Service.getAllMeetingByUser(userId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import org.sopt.makers.crew.main.common.exception.BaseException;
import org.sopt.makers.crew.main.entity.apply.ApplyRepository;
import org.sopt.makers.crew.main.entity.apply.enums.EnApplyStatus;
import org.sopt.makers.crew.main.entity.user.User;
import org.sopt.makers.crew.main.entity.user.UserRepository;
import org.sopt.makers.crew.main.user.v2.dto.response.UserV2GetAllMeetingByUserMeetingDto;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -25,7 +27,7 @@ public class UserV2ServiceImpl implements UserV2Service {
public List<UserV2GetAllMeetingByUserMeetingDto> getAllMeetingByUser(Integer userId) {
User user = userRepository.findByIdOrThrow(userId);

return Stream.concat(
List<UserV2GetAllMeetingByUserMeetingDto> userJoinedList = Stream.concat(
user.getMeetings().stream(),
applyRepository.findAllByUserIdAndStatus(userId, EnApplyStatus.APPROVE)
.stream()
Expand All @@ -40,5 +42,10 @@ public List<UserV2GetAllMeetingByUserMeetingDto> getAllMeetingByUser(Integer use
))
.sorted(Comparator.comparing(UserV2GetAllMeetingByUserMeetingDto::getId).reversed())
.collect(Collectors.toList());

if (userJoinedList.isEmpty()) {
throw new BaseException(HttpStatus.NO_CONTENT);
}
return userJoinedList;
}
}

0 comments on commit 7afb7b5

Please sign in to comment.