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 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2 코드리뷰반영 / 로그인체크 aspect 추가 / controllerAdvice 추가
- Loading branch information
Showing
25 changed files
with
342 additions
and
176 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
rootProject.name = 'untitled' | ||
rootProject.name = 'photobook' |
6 changes: 3 additions & 3 deletions
6
...com/sns/untitled/UntitledApplication.java → ...a/com/photobook/PhotobookApplication.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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
package com.sns.untitled; | ||
package com.photobook; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class UntitledApplication { | ||
public class PhotobookApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(UntitledApplication.class, args); | ||
SpringApplication.run(PhotobookApplication.class, args); | ||
} | ||
|
||
} |
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.photobook.annotation; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Documented | ||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.CLASS) | ||
public @interface LoginCheck { | ||
} |
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,37 @@ | ||
package com.photobook.aop; | ||
|
||
import com.photobook.user.service.LoginService; | ||
import com.photobook.exception.UnauthorizedException; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
import javax.servlet.http.HttpSession; | ||
|
||
@Component | ||
@Aspect | ||
public class AuthCheckAspect { | ||
|
||
LoginService loginService; | ||
|
||
public AuthCheckAspect(LoginService loginService) { | ||
this.loginService = loginService; | ||
} | ||
|
||
@Before("@annotation(com.photobook.annotation.LoginCheck)") | ||
public void loginCheck() { | ||
|
||
HttpSession httpSession = ((ServletRequestAttributes) RequestContextHolder | ||
.currentRequestAttributes()) // RequestAttributes가 없으면 예외 발생 | ||
.getRequest() | ||
.getSession(); | ||
|
||
if (loginService.getLoginUserInfo(httpSession) == null) { | ||
throw new UnauthorizedException("로그인된 사용자 정보가 존재하지 않습니다."); | ||
} | ||
|
||
} | ||
|
||
} |
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
31 changes: 31 additions & 0 deletions
31
src/main/java/com/photobook/exception/ExceptionAdvice.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,31 @@ | ||
package com.photobook.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import javax.validation.ConstraintViolationException; | ||
|
||
@RestControllerAdvice | ||
public class ExceptionAdvice { | ||
|
||
@ExceptionHandler(IllegalArgumentException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public String illegalArgumentException(IllegalArgumentException e) { | ||
return e.getMessage(); | ||
} | ||
|
||
@ExceptionHandler(ConstraintViolationException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public String constraintViolationException(ConstraintViolationException e) { | ||
return e.getMessage(); | ||
} | ||
|
||
@ExceptionHandler(UnauthorizedException.class) | ||
@ResponseStatus(HttpStatus.UNAUTHORIZED) | ||
public String unauthorizedException(UnauthorizedException e) { | ||
return e.getMessage(); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/photobook/exception/UnauthorizedException.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,8 @@ | ||
package com.photobook.exception; | ||
|
||
public class UnauthorizedException extends RuntimeException { | ||
|
||
public UnauthorizedException(String message) { | ||
super(message); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/photobook/user/controller/UserController.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,46 @@ | ||
package com.photobook.user.controller; | ||
|
||
import com.photobook.annotation.LoginCheck; | ||
import com.photobook.user.dto.UserDto; | ||
import com.photobook.user.service.LoginService; | ||
import com.photobook.user.service.UserService; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.servlet.http.HttpSession; | ||
import javax.validation.constraints.NotBlank; | ||
|
||
@RestController | ||
@RequestMapping("/users") | ||
@Validated | ||
public class UserController { | ||
|
||
private final UserService userService; | ||
|
||
private final LoginService loginService; | ||
|
||
public UserController(UserService userService, LoginService loginService) { | ||
this.userService = userService; | ||
this.loginService = loginService; | ||
} | ||
|
||
@PostMapping("/login") | ||
public void login(@RequestParam @NotBlank String id, @RequestParam @NotBlank String password, HttpSession httpSession) { | ||
UserDto userInfo = userService.getUserInfoByIdAndPassword(id, password); | ||
|
||
loginService.setLoginUserInfo(httpSession, userInfo); | ||
} | ||
|
||
@PostMapping("/logout") | ||
@LoginCheck | ||
public void logout(HttpSession httpSession) { | ||
loginService.removeLoginUserInfo(httpSession); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
@LoginCheck | ||
public UserDto getUserInfoById(@PathVariable @NotBlank String id) { | ||
UserDto userInfo = userService.getUserInfoById(id); | ||
return userInfo; | ||
} | ||
} |
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,14 @@ | ||
package com.photobook.user.mapper; | ||
|
||
import com.photobook.user.dto.UserDto; | ||
import org.apache.ibatis.annotations.Mapper; | ||
import org.apache.ibatis.annotations.Param; | ||
|
||
@Mapper | ||
public interface UserMapper { | ||
|
||
UserDto getUserInfoByIdAndPassword(@Param("id") String id, @Param("password") String password); | ||
|
||
UserDto getUserInfoById(@Param("id") String id); | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/photobook/user/service/LoginService.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,15 @@ | ||
package com.photobook.user.service; | ||
|
||
import com.photobook.user.dto.UserDto; | ||
|
||
import javax.servlet.http.HttpSession; | ||
|
||
public interface LoginService { | ||
|
||
void setLoginUserInfo(HttpSession httpSession, UserDto userDto); | ||
|
||
void removeLoginUserInfo(HttpSession httpSession); | ||
|
||
Object getLoginUserInfo(HttpSession httpSession); | ||
|
||
} |
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,11 @@ | ||
package com.photobook.user.service; | ||
|
||
import com.photobook.user.dto.UserDto; | ||
|
||
public interface UserService { | ||
|
||
UserDto getUserInfoByIdAndPassword(String id, String password); | ||
|
||
UserDto getUserInfoById(String id); | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/photobook/user/service/impl/LoginServiceImpl.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,28 @@ | ||
package com.photobook.user.service.impl; | ||
|
||
import com.photobook.user.dto.UserDto; | ||
import com.photobook.user.service.LoginService; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.servlet.http.HttpSession; | ||
|
||
@Service | ||
public class LoginServiceImpl implements LoginService { | ||
|
||
private static final String LOGIN_USER_INFO = "LOGIN_USER_INFO"; | ||
|
||
@Override | ||
public void setLoginUserInfo(HttpSession httpSession, UserDto userDto) { | ||
httpSession.setAttribute(LOGIN_USER_INFO, userDto); | ||
} | ||
|
||
@Override | ||
public void removeLoginUserInfo(HttpSession httpSession) { | ||
httpSession.invalidate(); | ||
} | ||
|
||
@Override | ||
public Object getLoginUserInfo(HttpSession httpSession) { | ||
return httpSession.getAttribute(LOGIN_USER_INFO); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/photobook/user/service/impl/UserServiceImpl.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,34 @@ | ||
package com.photobook.user.service.impl; | ||
|
||
import com.photobook.user.mapper.UserMapper; | ||
import com.photobook.user.dto.UserDto; | ||
import com.photobook.user.service.UserService; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class UserServiceImpl implements UserService { | ||
|
||
private final UserMapper userMapper; | ||
|
||
public UserServiceImpl(UserMapper userMapper) { | ||
this.userMapper = userMapper; | ||
} | ||
|
||
@Override | ||
public UserDto getUserInfoByIdAndPassword(String id, String password) { | ||
|
||
UserDto userInfo = userMapper.getUserInfoByIdAndPassword(id, password); | ||
|
||
if(userInfo == null) { | ||
throw new IllegalArgumentException("아이디 또는 비밀번호가 잘못 입력 되었습니다."); | ||
} | ||
|
||
return userInfo; | ||
} | ||
|
||
@Override | ||
public UserDto getUserInfoById(String id) { | ||
UserDto userInfo = userMapper.getUserInfoById(id); | ||
return userInfo; | ||
} | ||
} |
40 changes: 0 additions & 40 deletions
40
src/main/java/com/sns/untitled/user/controller/UserController.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.