Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdwns5063 committed Feb 2, 2024
2 parents f8d3a59 + 088288c commit ee91244
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 180 deletions.
12 changes: 6 additions & 6 deletions src/main/java/com/strcat/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.strcat.config.oauth.JwtAuthenticationEntryPoint;
import com.strcat.config.oauth.OAuthFailureHandler;
import com.strcat.config.oauth.OAuthSuccessHandler;
import com.strcat.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
Expand All @@ -24,7 +25,7 @@ public class SecurityConfig {
private final OAuthSuccessHandler oAuthSuccessHandler;
private final OAuthFailureHandler oAuthFailureHandler;
private final WebConfig webConfig;
private final JwtAuthFilter jwtAuthFilter;
private final UserService userService;
private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;


Expand All @@ -40,18 +41,17 @@ public class SecurityConfig {
"boards/*/summaries",
"boards/*/contents",
"boards/*/contents/pictures",
"board-groups/*"
};

@Autowired
public SecurityConfig(OAuthSuccessHandler oAuthSuccessHandler,
OAuthFailureHandler oAuthFailureHandler, WebConfig webConfig,
JwtAuthFilter jwtAuthFilter,
UserService userService,
JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint) {
this.oAuthSuccessHandler = oAuthSuccessHandler;
this.oAuthFailureHandler = oAuthFailureHandler;
this.webConfig = webConfig;
this.jwtAuthFilter = jwtAuthFilter;
this.userService = userService;
this.jwtAuthenticationEntryPoint = jwtAuthenticationEntryPoint;
}

Expand All @@ -71,10 +71,10 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.csrf(AbstractHttpConfigurer::disable)
.addFilter(webConfig.corsFilter())
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(new JwtAuthFilter(userService), UsernamePasswordAuthenticationFilter.class)
.exceptionHandling((httpSecurityExceptionHandlingConfigurer) -> httpSecurityExceptionHandlingConfigurer
.authenticationEntryPoint(jwtAuthenticationEntryPoint)
);
return http.build();
}
}
}
34 changes: 14 additions & 20 deletions src/main/java/com/strcat/config/oauth/JwtAuthFilter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.strcat.config.oauth;


import com.strcat.util.JwtUtils;
import com.strcat.domain.User;
import com.strcat.service.UserService;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
Expand All @@ -13,15 +14,13 @@
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

@Component
public class JwtAuthFilter extends GenericFilterBean {
private final JwtUtils jwtUtils;
private final UserService userService;

public JwtAuthFilter(JwtUtils jwtUtils) {
this.jwtUtils = jwtUtils;
public JwtAuthFilter(UserService userService) {
this.userService = userService;
}

@Override
Expand All @@ -31,21 +30,16 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
return;
}

String token = jwtUtils.exportToken(httpServletRequest);
userService.validate(httpServletRequest.getHeader("Authorization")).ifPresent(this::registerAuthentication);

if (token == null || token.isBlank()) {
chain.doFilter(request, response);
return;
}
chain.doFilter(request, response);
}

if (jwtUtils.isValidateToken(token)) {
String oauthId = jwtUtils.parseUserId(token).toString();
private void registerAuthentication(User user) {
Authentication authentication =
new UsernamePasswordAuthenticationToken(user.getId(), user,
List.of(new SimpleGrantedAuthority("ROLE_USER")));

Authentication authentication =
new UsernamePasswordAuthenticationToken(oauthId, token,
List.of(new SimpleGrantedAuthority("ROLE_USER")));
SecurityContextHolder.getContext().setAuthentication(authentication);
}
chain.doFilter(request, response);
SecurityContextHolder.getContext().setAuthentication(authentication);
}
}
}
39 changes: 32 additions & 7 deletions src/main/java/com/strcat/controller/BoardController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.strcat.controller;

import com.strcat.domain.Board;
import com.strcat.domain.User;
import com.strcat.dto.CreateBoardReqDto;
import com.strcat.dto.CreateContentReqDto;
import com.strcat.dto.DeleteContentReqDto;
import com.strcat.dto.ReadBoardResDto;
import com.strcat.dto.ReadBoardSummaryResDto;
import com.strcat.service.BoardService;
import com.strcat.service.ContentService;
import com.strcat.service.PictureService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -16,6 +18,9 @@
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatusCode;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -25,27 +30,33 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ResponseStatusException;

@Tag(name = "보드 및 컨텐츠")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "성공"),
@ApiResponse(responseCode = "401", description = "인증 실패"),
@ApiResponse(responseCode = "406", description = "잘못된 요청"),
@ApiResponse(responseCode = "500", description = "서버 에러"),
@ApiResponse(responseCode = "406", description = "잘못된 요청", content = {
@Content(examples = {@ExampleObject("잘못된 요청")})
}),
@ApiResponse(responseCode = "500", description = "서버 에러", content = {
@Content(examples = {@ExampleObject("서버 에러")})
}),
})
@RestController
@RequestMapping("/boards")
@RequiredArgsConstructor
public class BoardController {
private final ContentService contentService;
private final BoardService boardService;
private final PictureService pictureService;

@PostMapping
@SecurityRequirement(name = "Bearer Authentication")
@Operation(summary = "보드 생성", description = "생성 성공 후 board의 encryptedId를 반환합니다.")
@ApiResponse(responseCode = "200", description = "성공", content = {
@Content(examples = {@ExampleObject("Wd5lUSQnmEjnMVl043cEzZzNqqrA3Z9pBAVImYNwI14=")})})
@ApiResponse(responseCode = "401", description = "인증 실패", content = {
@Content(examples = {@ExampleObject("인증 실패")})
})
public String createBoard(@Parameter(hidden = true) @RequestHeader("Authorization") String token,
@RequestBody CreateBoardReqDto dto) {
return boardService.createBoard(dto, token);
Expand All @@ -57,7 +68,7 @@ public String createBoard(@Parameter(hidden = true) @RequestHeader("Authorizatio
@Content(examples = {@ExampleObject("32")})})
public Long createContent(@PathVariable(name = "boardId") String encryptedBoardId,
@RequestBody CreateContentReqDto dto) {
return contentService.create(dto, encryptedBoardId).getId();
return contentService.create(dto, encryptedBoardId);
}

@PostMapping("/{boardId}/contents/pictures")
Expand All @@ -67,7 +78,7 @@ public Long createContent(@PathVariable(name = "boardId") String encryptedBoardI
@ExampleObject("https://elasticbeanstalk-ap-northeast-2-168479654979.s3.ap-northeast-2.amazonaws.com/pictures/strcat%3Aj5AAnW0Dq0Q5qS4g56IiRj1W8xQUKPbva1hbe_4STryyELo6vaiFqPgjJH5-_iIv%3A1701366068275%3Aphotofilename.jpg")})})
public String createPicture(@PathVariable(name = "boardId") String encryptedBoardId,
@RequestParam MultipartFile picture) {
return pictureService.postPicture(encryptedBoardId, picture);
return contentService.postPicture(encryptedBoardId, picture);
}

@GetMapping("/{boardId}")
Expand All @@ -83,4 +94,18 @@ public ReadBoardResDto readBoard(@Parameter(hidden = true) @RequestHeader("Autho
public ReadBoardSummaryResDto readSummary(@PathVariable(name = "boardId") String encryptedBoardId) {
return boardService.readSummary(encryptedBoardId);
}

@DeleteMapping("/{boardId}/contents")
@SecurityRequirement(name = "Bearer Authentication")
@Operation(summary = "컨텐츠 삭제", description = "컨텐츠를 삭제합니다.")
@ApiResponse(responseCode = "401", description = "인증 실패", content = {
@Content(examples = {@ExampleObject("인증 실패")})})
public ReadBoardResDto deleteContents(@PathVariable(name = "boardId") String encryptedBoardId, @RequestBody
DeleteContentReqDto dto, Authentication authentication) {
if (authentication == null) throw new ResponseStatusException(HttpStatusCode.valueOf(401));

User user = (User) authentication.getCredentials();

return contentService.deleteContent(encryptedBoardId, dto, user);
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/strcat/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import com.strcat.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,6 +17,15 @@
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "로그인")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "성공"),
@ApiResponse(responseCode = "406", description = "잘못된 요청", content = {
@Content(examples = {@ExampleObject("잘못된 요청")})
}),
@ApiResponse(responseCode = "500", description = "서버 에러", content = {
@Content(examples = {@ExampleObject("서버 에러")})
}),
})
@RestController
@RequestMapping("/login")
@RequiredArgsConstructor
Expand Down
24 changes: 0 additions & 24 deletions src/main/java/com/strcat/controller/ShareController.java

This file was deleted.

16 changes: 16 additions & 0 deletions src/main/java/com/strcat/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import com.strcat.service.BoardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,6 +17,18 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@ApiResponses({
@ApiResponse(responseCode = "200", description = "성공"),
@ApiResponse(responseCode = "401", description = "인증 실패", content = {
@Content(examples = {@ExampleObject("인증 실패")})
}),
@ApiResponse(responseCode = "406", description = "잘못된 요청", content = {
@Content(examples = {@ExampleObject("잘못된 요청")})
}),
@ApiResponse(responseCode = "500", description = "서버 에러", content = {
@Content(examples = {@ExampleObject("서버 에러")})
}),
})
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/strcat/domain/Board.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.strcat.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.strcat.dto.BoardResponse;
import com.strcat.dto.ReadBoardResDto;
import com.strcat.dto.ReadBoardSummaryResDto;
import com.strcat.dto.ReadMyInfoResDto;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -62,4 +66,20 @@ public Long calculateTotalContentLength() {
.mapToLong(content -> content.getText().length())
.sum();
}

private BoardResponse toBoardResponse() {
return new BoardResponse(encryptedId, title, theme, contents);
}

public ReadBoardResDto toReadBoardResDto(Boolean isOwner) {
return new ReadBoardResDto(isOwner, toBoardResponse());
}

public ReadBoardSummaryResDto toReadBoardSummaryDto() {
return new ReadBoardSummaryResDto(title, theme, contents.size(), calculateTotalContentLength());
}

public ReadMyInfoResDto toReadMyInfoResDto() {
return new ReadMyInfoResDto(encryptedId, title);
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/strcat/dto/DeleteContentReqDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.strcat.dto;

import java.util.List;

public record DeleteContentReqDto(
List<Long> contentIds
) {
}
7 changes: 7 additions & 0 deletions src/main/java/com/strcat/dto/DeleteContentResDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.strcat.dto;

import com.strcat.domain.Content;
import java.util.List;

public record DeleteContentResDto(List<Content> contents) {
}
19 changes: 4 additions & 15 deletions src/main/java/com/strcat/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.strcat.domain.Board;
import com.strcat.domain.User;
import com.strcat.dto.BoardResponse;
import com.strcat.dto.CreateBoardReqDto;
import com.strcat.dto.ReadBoardResDto;
import com.strcat.dto.ReadBoardSummaryResDto;
Expand Down Expand Up @@ -33,8 +32,7 @@ public List<ReadMyInfoResDto> readMyBoardInfo(String token) {
User user = userService.getUser(token);
List<Board> boards = findByUserId(user.getId());
return boards.stream()
.map(board -> new ReadMyInfoResDto(board.getEncryptedId(),
board.getTitle()))
.map(Board::toReadMyInfoResDto)
.collect(Collectors.toList());
}

Expand All @@ -52,21 +50,17 @@ public String createBoard(CreateBoardReqDto dto, String token) {

public ReadBoardResDto readBoard(String encryptedBoardId, String token) {
Board board = getBoard(encryptedBoardId);
BoardResponse boardResponse = fetchBoardResponse(board);
try {
Long userId = jwtUtils.parseUserId(jwtUtils.removeBearerString(token));
Boolean isOwner = userId.equals(board.getUser().getId());
return new ReadBoardResDto(isOwner, boardResponse);
return board.toReadBoardResDto(isOwner);
} catch (NotAcceptableException e) {
return new ReadBoardResDto(false, boardResponse);
return board.toReadBoardResDto(false);
}
}

public ReadBoardSummaryResDto readSummary(String encryptedBoardId) {
Board board = getBoard(encryptedBoardId);

return new ReadBoardSummaryResDto(board.getTitle(), board.getTheme(), board.getContents().size(),
board.calculateTotalContentLength());
return getBoard(encryptedBoardId).toReadBoardSummaryDto();
}

public Board getBoard(String encryptedBoardId) {
Expand All @@ -78,9 +72,4 @@ public Board getBoard(String encryptedBoardId) {
}
return optionalBoard.get();
}

public BoardResponse fetchBoardResponse(Board board) {
return new BoardResponse(board.getEncryptedId(),
board.getTitle(), board.getTheme(), board.getContents());
}
}
Loading

0 comments on commit ee91244

Please sign in to comment.