This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from f-lab-edu/feature/10
#23 Feature/10
- Loading branch information
Showing
40 changed files
with
2,279 additions
and
650 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.delfood.aop; | ||
|
||
import javax.servlet.http.HttpSession; | ||
import org.aspectj.lang.JoinPoint; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.HttpStatusCodeException; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
import com.delfood.service.ShopService; | ||
import com.delfood.utils.SessionUtil; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
@Aspect | ||
@Component | ||
@Log4j2 | ||
@SuppressWarnings("unchecked") | ||
public class AuthCheckAspect { | ||
@Autowired | ||
private ShopService shopService; | ||
|
||
/** | ||
* session에서 owner 로그인을 체크한다. | ||
* 로그인되어있지 않을 시 해당 메서드 로직을 중지시킨 후 리턴한다. | ||
* @OwnerLoginCheck 해당 어노테이션이 적용된 메서드를 검사한다. | ||
* @author jun | ||
* @param pjp | ||
* @return 로그인시 SUCCESS, 비로그인시 NO_LOGIN | ||
* @throws Throwable | ||
*/ | ||
@Before("@annotation(com.delfood.aop.OwnerLoginCheck)") | ||
public void ownerLoginCheck(JoinPoint jp) throws Throwable { | ||
log.debug("AOP - Owner Login Check Started"); | ||
|
||
HttpSession session = ((ServletRequestAttributes)(RequestContextHolder.currentRequestAttributes())).getRequest().getSession(); | ||
String ownerId = SessionUtil.getLoginOwnerId(session); | ||
|
||
if(ownerId == null) { | ||
log.debug("AOP - Owner Login Check Result - NO_LOGIN"); | ||
throw new HttpStatusCodeException(HttpStatus.UNAUTHORIZED, "NO_LOGIN") {}; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* 세션에서 사장님 로그인을 체크 한다. | ||
* 그 후 입력받은 파라미터 값 중 매장 id를 검색하여 해당 매장이 접속한 사장님의 것인지 검사한다. | ||
* @author jun | ||
* @param pjp | ||
* @return 비로그인시 NO_LOGIN, 해당 매장의 사장이 아닐 시 UNAUTHORIZED, 권한이 있을 시 SUCCESS | ||
* @throws Throwable | ||
*/ | ||
@Before("@annotation(com.delfood.aop.OwnerShopCheck)") | ||
public void ownerShopCheck(JoinPoint jp) throws Throwable { | ||
log.debug("AOP - Owner Shop Check Started"); | ||
|
||
|
||
HttpSession session = ((ServletRequestAttributes)(RequestContextHolder.currentRequestAttributes())).getRequest().getSession(); | ||
String ownerId = SessionUtil.getLoginOwnerId(session); | ||
|
||
if(ownerId == null) { | ||
log.debug("AOP - Owner Shop Check Result - NO_LOGIN"); | ||
throw new HttpStatusCodeException(HttpStatus.UNAUTHORIZED, "NO_LOGIN") {}; | ||
} | ||
|
||
Object[] args = jp.getArgs(); | ||
Long shopId = (Long) args[0]; | ||
|
||
if (!shopService.isShopOwner(shopId, ownerId)) { | ||
log.debug("AOP - Owner Shop Check Result - UNAUTHORIZED"); | ||
throw new HttpStatusCodeException(HttpStatus.UNAUTHORIZED, "UNAUTHORIZED") {}; | ||
} | ||
} | ||
|
||
/** | ||
* 고객의 로그인을 체크한다. | ||
* @author jun | ||
* @param pjp | ||
* @return | ||
* @throws Throwable | ||
*/ | ||
@Before("@annotation(com.delfood.aop.MemberLoginCheck)") | ||
public void memberLoginCheck(JoinPoint jp) throws Throwable { | ||
log.debug("AOP - Member Login Check Started"); | ||
|
||
HttpSession session = ((ServletRequestAttributes)(RequestContextHolder.currentRequestAttributes())).getRequest().getSession(); | ||
String memberId = SessionUtil.getLoginMemberId(session); | ||
|
||
if (memberId == null) { | ||
throw new HttpStatusCodeException(HttpStatus.UNAUTHORIZED, "NO_LOGIN") {}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.delfood.aop; | ||
|
||
public @interface MemberLoginCheck { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.delfood.aop; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
public @interface OwnerLoginCheck { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.delfood.aop; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* <b>매장 id가 첫 번째 파라미터로 와야한다.</b> | ||
* 접속한 사장님이 해당 매장의 주인인지 확인한다. | ||
* @author yyy99 | ||
* | ||
*/ | ||
@Target(ElementType.METHOD) | ||
public @interface OwnerShopCheck { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/main/java/com/delfood/controller/LocationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package com.delfood.controller; | ||
|
||
import com.delfood.aop.OwnerShopCheck; | ||
import com.delfood.controller.reqeust.GetAddressByZipRequest; | ||
import com.delfood.controller.reqeust.GetAddressesByRoadRequest; | ||
import com.delfood.dto.AddressDTO; | ||
import com.delfood.dto.DeliveryLocationDTO; | ||
import com.delfood.service.AddressService; | ||
import com.delfood.service.ShopService; | ||
import java.util.List; | ||
import java.util.Set; | ||
import javax.servlet.http.HttpSession; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.Setter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.http.HttpStatus; | ||
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; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/locations/") | ||
public class LocationController { | ||
@Autowired | ||
private ShopService shopService; | ||
|
||
@Autowired | ||
private AddressService addressService; | ||
|
||
/** | ||
* 매장의 배달 가능 지역을 추가한다. 배달 가능 지역에 포함되어 있는 사용자에게 검색이 된다. 클라이언트에서는 요청 전 중복된 배달지역이 있는지 체크해야한다. 체크하지 않은 | ||
* 상태로 중복된 추가 요청을 보낼 경우 예외처리를 진행한다. | ||
* | ||
* @param shopId 배달 지역을 추가할 매장의 아이디 | ||
* @param addDeliveryLocationRequest 추가할 지역 리스트 | ||
* @return | ||
*/ | ||
@PostMapping("deliveries/{shopId}/possibles") | ||
@OwnerShopCheck | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public void addDeliveryLocation( | ||
@PathVariable(name = "shopId") Long shopId, | ||
@RequestBody(required = true) AddDeliveryLocationRequest addDeliveryLocationRequest) { | ||
Set<String> townCodes = addDeliveryLocationRequest.getTownCodes(); | ||
shopService.addDeliveryLocation(shopId, townCodes); | ||
} | ||
|
||
/** | ||
* 매장의 배달가능지역을 조회한다. | ||
* | ||
* @author jun | ||
* @param shopId 배달가능 지역을 조회할 매장의 id | ||
* @return | ||
*/ | ||
@GetMapping("deliveries/{shopId}/possibles") | ||
@OwnerShopCheck | ||
public List<DeliveryLocationDTO> getDeliveryLocations( | ||
@PathVariable(name = "shopId") Long shopId) { | ||
return shopService.getDeliveryLocations(shopId); | ||
} | ||
|
||
|
||
/** | ||
* 배달 지역 삭제. | ||
* | ||
* @author jun | ||
* @param deliveryLocationId 삭제할 배달 지역 id | ||
* @param session 접속한 사용자의 세션 | ||
* @return | ||
*/ | ||
@DeleteMapping("deliveries/{shopId}/possibles/{deliveryLocationId}") | ||
@OwnerShopCheck | ||
public void deleteDeliveryLocation( | ||
@PathVariable(value = "shopId") Long shopId, | ||
@PathVariable(value = "deliveryLocationId") Long deliveryLocationId, | ||
HttpSession session) { | ||
shopService.deleteDeliveryLocation(deliveryLocationId); | ||
} | ||
|
||
/** | ||
* 도로명 주소를 검색한다. | ||
* | ||
* @author jun | ||
* @param requestInfo 검색할 도로명 주소 정보. | ||
* @return | ||
*/ | ||
@GetMapping("address/road") | ||
public List<AddressDTO> getAddressByRoadInfo( | ||
GetAddressesByRoadRequest requestInfo) { | ||
List<AddressDTO> addresses = addressService.getAddressByRoadName(requestInfo); | ||
return addresses; | ||
} | ||
|
||
|
||
/** | ||
* 지번 주소를 검색한다. | ||
* | ||
* @author jun | ||
* @param requestInfo 검색할 지번 주소 정보. | ||
* @return | ||
*/ | ||
@GetMapping("address/zip") | ||
public List<AddressDTO> getAddressByZipInfo( | ||
GetAddressByZipRequest requestInfo) { | ||
List<AddressDTO> addresses = addressService.getAddressByZipAddress(requestInfo); | ||
return addresses; | ||
} | ||
|
||
|
||
|
||
// ---------------------- Request 객체 ---------------------- | ||
@Getter | ||
@Setter | ||
private static class AddDeliveryLocationRequest { | ||
@NonNull | ||
private Set<String> townCodes; | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.