Skip to content

Commit

Permalink
Merge branch '9oormthon-univ:develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
LEEJaeHyeok97 authored Nov 20, 2024
2 parents e20a015 + e74ebe9 commit c627cc2
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Message addCart(String userProviderId, AddCartRequest addCartRequest) {
.orElseThrow(() -> new NullPointerException());

Menu menu = menuRepository.findById(addCartRequest.menuId())
.orElseThrow(() -> new IllegalArgumentException("๋“ฑ๋ก๋œ ๋ฉ”๋‰ด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."));
.orElseThrow(() -> new IllegalArgumentException("๋“ฑ๋ก๋œ ๋ฉ”๋‰ด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. "));

System.out.println("menu.getId() = " + menu.getId());
System.out.println("user.getUserId() = " + user.getUserId());
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/jangburich/domain/store/domain/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public static Store of(Owner owner, StoreCreateRequestDTO storeCreateRequestDTO)
newStore.setOpenTime(storeCreateRequestDTO.getOpenTime());
newStore.setCloseTime(storeCreateRequestDTO.getCloseTime());
newStore.setContactNumber(storeCreateRequestDTO.getContactNumber());
newStore.setReservationAvailable(storeCreateRequestDTO.getReservationAvailable());
newStore.setMaxReservation(storeCreateRequestDTO.getMaxReservation());
newStore.setMinPrepayment(storeCreateRequestDTO.getMinPrepayment());
newStore.setPrepaymentDuration(storeCreateRequestDTO.getPrepaymentDuration());
return newStore;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.time.LocalTime;
import java.util.List;

import com.jangburich.domain.menu.domain.MenuCreateRequestDTO;

import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import lombok.Getter;
Expand Down Expand Up @@ -34,4 +36,11 @@ public class StoreCreateRequestDTO {
private List<DayOfWeek> dayOfWeek;
private LocalTime openTime;
private LocalTime closeTime;

private Boolean reservationAvailable;
private Long maxReservation;
private Long minPrepayment;
private Long prepaymentDuration;

private List<MenuCreateRequestDTO> menuCreateRequestDTOS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,6 @@ public ResponseCustom<Message> createStore(
return ResponseCustom.OK(Message.builder().message("success").build());
}

@Operation(summary = "๊ฐ€๊ฒŒ ์ถ”๊ฐ€์ •๋ณด ์ €์žฅ", description = "์˜ˆ์•ฝ ๊ฐ€๋Šฅ ์—ฌ๋ถ€, ์ตœ์†Œ ์„ ๊ฒฐ์ œ ๊ธˆ์•ก, ์„ ๊ฒฐ์ œ ์‚ฌ์šฉ ๊ธฐ๊ฐ„์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.")
@PostMapping("/create/additionalInfo")
public ResponseCustom<Message> createAdditionalInfo(
Authentication authentication,
@RequestBody StoreAdditionalInfoCreateRequestDTO storeAdditionalInfoCreateRequestDTO) {
storeService.createAdditionalInfo(AuthenticationParser.parseUserId(authentication),
storeAdditionalInfoCreateRequestDTO);
return ResponseCustom.OK(Message.builder().message("success").build());
}

@Operation(summary = "๊ฐ€๊ฒŒ ์ •๋ณด ์ˆ˜์ •", description = "๊ฐ€๊ฒŒ ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.")
@PatchMapping("/update")
public ResponseCustom<Message> updateStore(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jangburich.domain.menu.domain.Menu;
import com.jangburich.domain.menu.domain.MenuCreateRequestDTO;
import com.jangburich.domain.menu.domain.repository.MenuRepository;
import com.jangburich.domain.owner.domain.Owner;
import com.jangburich.domain.owner.domain.repository.OwnerRepository;
import com.jangburich.domain.payment.domain.TeamChargeHistoryResponse;
Expand Down Expand Up @@ -43,6 +46,7 @@ public class StoreService {
private final StoreTeamRepository storeTeamRepository;
private final TeamRepository teamRepository;
private final TeamChargeHistoryRepository teamChargeHistoryRepository;
private final MenuRepository menuRepository;

@Transactional
public void createStore(String authentication, StoreCreateRequestDTO storeCreateRequestDTO) {
Expand All @@ -52,7 +56,12 @@ public void createStore(String authentication, StoreCreateRequestDTO storeCreate
Owner owner = ownerRepository.findByUser(user)
.orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION));

storeRepository.save(Store.of(owner, storeCreateRequestDTO));
Store store = storeRepository.save(Store.of(owner, storeCreateRequestDTO));

for (MenuCreateRequestDTO menuCreateRequestDTO : storeCreateRequestDTO.getMenuCreateRequestDTOS()) {
menuRepository.save(Menu.create(menuCreateRequestDTO.getName(), menuCreateRequestDTO.getDescription(),
menuCreateRequestDTO.getImage_url(), menuCreateRequestDTO.getPrice(), store));
}
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.jangburich.domain.user.domain.AdditionalInfoCreateDTO;
import com.jangburich.domain.user.domain.KakaoApiResponseDTO;
import com.jangburich.domain.user.domain.TokenResponseDTO;
import com.jangburich.domain.user.service.UserService;
import com.jangburich.global.payload.Message;
import com.jangburich.global.payload.ResponseCustom;
import com.jangburich.utils.parser.AuthenticationParser;

import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;

@RestController
Expand All @@ -33,29 +37,25 @@ public class UserController {
private final UserService userService;

@PostMapping("/login")
public ResponseCustom<TokenResponseDTO> login(
@RequestParam String authorizationHeader) {
public ResponseCustom<TokenResponseDTO> login(@RequestParam String authorizationHeader) {
TokenResponseDTO login = userService.login(authorizationHeader);
return ResponseCustom.OK(login);
}

@GetMapping("/user-info")
public ResponseEntity<KakaoApiResponseDTO> getUserInfo(
Authentication authentication) {
public ResponseEntity<KakaoApiResponseDTO> getUserInfo(Authentication authentication) {
KakaoApiResponseDTO userInfo = userService.getUserInfo(AuthenticationParser.parseUserId(authentication));

return ResponseEntity.ok(userInfo);
}

@PostMapping("/join/user")
public ResponseCustom<TokenResponseDTO> joinUser(
@RequestParam String authorizationHeader) {
public ResponseCustom<TokenResponseDTO> joinUser(@RequestParam String authorizationHeader) {
return ResponseCustom.OK(userService.joinUser(authorizationHeader));
}

@PostMapping("/join/owner")
public ResponseCustom<TokenResponseDTO> joinOwner(
@RequestParam String authorizationHeader) {
public ResponseCustom<TokenResponseDTO> joinOwner(@RequestParam String authorizationHeader) {
return ResponseCustom.OK(userService.joinOwner(authorizationHeader));
}

Expand Down Expand Up @@ -91,4 +91,12 @@ public ResponseEntity<Map<String, Object>> getAccessToken(@RequestParam("code")
return ResponseEntity.ok(response.getBody());
}

@Operation(summary = "์œ ์ € ์ถ”๊ฐ€์ •๋ณด ์ €์žฅ", description = "์œ ์ € ์ถ”๊ฐ€์ •๋ณด ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.")
@PostMapping("/additionalInfo")
public ResponseCustom<?> saveAdditionalInfo(Authentication authentication,
@RequestBody AdditionalInfoCreateDTO additionalInfoCreateDTO) {
userService.additionalInfo(AuthenticationParser.parseUserId(authentication), additionalInfoCreateDTO);
return ResponseCustom.OK(Message.builder().message("success").build());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.jangburich.domain.user.domain;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class AdditionalInfoCreateDTO {
private String name;
private String phoneNum;
private Boolean agreeMarketing;
private Boolean agreeAdvertisement;
}
9 changes: 9 additions & 0 deletions src/main/java/com/jangburich/domain/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class User extends BaseEntity {
@Column(name = "nickname", nullable = false, unique = true)
private String nickname;

@Column(name = "name")
private String name;

@Column(name = "profile_image_url")
private String profileImageUrl;

Expand All @@ -50,6 +53,12 @@ public class User extends BaseEntity {
@Column(name = "refresh_token")
private String refreshToken;

@Column(name = "agree_marketing")
private Boolean agreeMarketing;

@Column(name = "agree_advertisement")
private Boolean agreeAdvertisement;

public void updateRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/com/jangburich/domain/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
import com.jangburich.domain.owner.domain.repository.OwnerRepository;
import com.jangburich.domain.store.domain.Store;
import com.jangburich.domain.store.domain.repository.StoreRepository;
import com.jangburich.domain.user.domain.AdditionalInfoCreateDTO;
import com.jangburich.domain.user.domain.KakaoApiResponseDTO;
import com.jangburich.domain.user.domain.TokenResponseDTO;
import com.jangburich.domain.user.domain.User;
import com.jangburich.domain.user.repository.UserRepository;
import com.jangburich.global.error.DefaultNullPointerException;
import com.jangburich.global.payload.ErrorCode;
import com.jangburich.utils.JwtUtil;

import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -81,9 +84,8 @@ public TokenResponseDTO joinOwner(String kakaoAccessToken) {

User user = userRepository.findByProviderId("kakao_" + userInfo.getId()).orElse(null);
if (user == null) {
user = userRepository.save(
User.create("kakao_" + userInfo.getId(), userInfo.getProperties().getNickname(),
userInfo.getKakaoAccount().getEmail(), userInfo.getProperties().getProfileImage(), "ROLE_OWNER"));
user = userRepository.save(User.create("kakao_" + userInfo.getId(), userInfo.getProperties().getNickname(),
userInfo.getKakaoAccount().getEmail(), userInfo.getProperties().getProfileImage(), "ROLE_OWNER"));
Owner newOwner = ownerRepository.save(Owner.create(user));
storeRepository.save(Store.create(newOwner));
}
Expand Down Expand Up @@ -137,4 +139,15 @@ public String reissueAccessToken(String refreshToken) {
return jwtUtil.createAccessToken(user.getProviderId(), user.getRole());
}

@Transactional
public void additionalInfo(String userId, AdditionalInfoCreateDTO additionalInfoCreateDTO) {
User user = userRepository.findByProviderId(userId)
.orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION));

user.setName(additionalInfoCreateDTO.getName());
user.setPhoneNumber(additionalInfoCreateDTO.getPhoneNum());
user.setAgreeAdvertisement(additionalInfoCreateDTO.getAgreeAdvertisement());
user.setAgreeMarketing(additionalInfoCreateDTO.getAgreeMarketing());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
@EnableWebSecurity
public class SecurityConfig {

private final JwtUtil jwtUtil;
private final JwtFilter jwtFilter;

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf(AbstractHttpConfigurer::disable)
.httpBasic(AbstractHttpConfigurer::disable)
.formLogin(AbstractHttpConfigurer::disable)
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.addFilterBefore(new JwtFilter(jwtUtil), UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class)

.authorizeHttpRequests(
request -> request.requestMatchers("/**", "/oauth2/**", "/login/**", "/swagger-ui/**",
Expand Down
48 changes: 38 additions & 10 deletions src/main/java/com/jangburich/utils/JwtFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.io.IOException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import com.jangburich.domain.oauth.domain.CustomOAuthUser;
Expand All @@ -18,8 +20,15 @@
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class JwtFilter extends OncePerRequestFilter {

@Value("${test.jwt.permanentUserToken}")
private String permanentUserToken;

@Value("${test.jwt.permanentOwnerToken}")
private String permanentOwnerToken;

private final JwtUtil jwtUtil;

public JwtFilter(JwtUtil jwtUtil) {
Expand All @@ -29,43 +38,50 @@ public JwtFilter(JwtUtil jwtUtil) {
@Override
protected void doFilterInternal(HttpServletRequest request, @NonNull HttpServletResponse response,
@NonNull FilterChain filterChain) throws ServletException, IOException {
// Authorization ํ—ค๋”๋ฅผ ์ฐพ์Œ
String authorizationHeader = request.getHeader("Authorization");

if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) {
filterChain.doFilter(request, response);
return;
}

// "Bearer " ์ดํ›„์˜ ํ† ํฐ ๋ถ€๋ถ„๋งŒ ์ถ”์ถœ
String token = authorizationHeader.substring(7);
String token = authorizationHeader.substring(7); // "Bearer " ์ดํ›„์˜ ํ† ํฐ ์ถ”์ถœ

try {
// ํ† ํฐ ์†Œ๋ฉธ ์‹œ๊ฐ„ ๊ฒ€์ฆ
if (token.equals(permanentUserToken)) {
log.info("Permanent user token detected. Skipping validation.");
setPermanentAuthentication("test-user", "ROLE_USER");
filterChain.doFilter(request, response);
return;
}

if (token.equals(permanentOwnerToken)) {
log.info("Permanent owner token detected. Skipping validation.");
setPermanentAuthentication("test-owner", "ROLE_OWNER");
filterChain.doFilter(request, response);
return;
}

// ์ผ๋ฐ˜ ํ† ํฐ ์ฒ˜๋ฆฌ
if (jwtUtil.isTokenExpired(token)) {
filterChain.doFilter(request, response);
return;
}

// ํ† ํฐ์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด ํš๋“
String userId = jwtUtil.getUserId(token);
String role = jwtUtil.getRole(token);

// OAuth2UserDTO ์ƒ์„ฑ ๋ฐ ์„ค์ •
OAuthUserDTO userDTO = new OAuthUserDTO();
userDTO.setUserId(userId);
userDTO.setRole(role);

// CustomOAuth2User ์ƒ์„ฑ
CustomOAuthUser customOAuth2User = new CustomOAuthUser(userDTO);

// ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ์ธ์ฆ ํ† ํฐ ์ƒ์„ฑ ๋ฐ ์„ค์ •
Authentication authToken = new UsernamePasswordAuthenticationToken(customOAuth2User, null,
customOAuth2User.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authToken);

} catch (Exception e) {
// ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ๋กœ๊ทธ ์ถœ๋ ฅ ๋ฐ ์ธ์ฆ ์ •๋ณด ์ œ๊ฑฐ
SecurityContextHolder.clearContext();
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, e.getMessage());
return;
Expand All @@ -74,4 +90,16 @@ protected void doFilterInternal(HttpServletRequest request, @NonNull HttpServlet
filterChain.doFilter(request, response);
}

}
private void setPermanentAuthentication(String userId, String role) {
OAuthUserDTO userDTO = new OAuthUserDTO();
userDTO.setUserId(userId);
userDTO.setRole(role);

CustomOAuthUser customOAuth2User = new CustomOAuthUser(userDTO);

Authentication authToken = new UsernamePasswordAuthenticationToken(customOAuth2User, null,
customOAuth2User.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authToken);
}

}

0 comments on commit c627cc2

Please sign in to comment.