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.
Browse files
Browse the repository at this point in the history
Feature/#2/login
- Loading branch information
Showing
25 changed files
with
394 additions
and
115 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,30 @@ | ||
package com.photobook.aop; | ||
|
||
import com.photobook.exception.UnauthorizedException; | ||
import com.photobook.dto.UserDto; | ||
import com.photobook.service.LoginService; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Aspect | ||
public class AuthCheckAspect { | ||
|
||
private final LoginService loginService; | ||
|
||
public AuthCheckAspect(LoginService loginService) { | ||
this.loginService = loginService; | ||
} | ||
|
||
@Before("@annotation(com.photobook.annotation.LoginCheck)") | ||
public void loginCheck() { | ||
|
||
UserDto userInfo = loginService.getLoginUserInfo(); | ||
|
||
if(userInfo == 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//package com.photobook.config; | ||
// | ||
//import org.springframework.context.annotation.Bean; | ||
//import org.springframework.context.annotation.Configuration; | ||
//import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
//import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
//import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; | ||
//import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer; | ||
// | ||
//@Configuration | ||
//@EnableRedisHttpSession | ||
//public class RedisConfig extends AbstractHttpSessionApplicationInitializer { | ||
// | ||
// @Bean | ||
// public RedisConnectionFactory redisConnectionFactory() { | ||
// LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(); | ||
// return lettuceConnectionFactory; | ||
// } | ||
// | ||
// @Bean | ||
// public RedisTemplate<String, Object> redisTemplate() { | ||
// RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); | ||
// redisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
// redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
// redisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
// return redisTemplate; | ||
// } | ||
// | ||
// @Bean | ||
// public StringRedisTemplate stringRedisTemplate() { | ||
// StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); | ||
// stringRedisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
// stringRedisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
// stringRedisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
// return stringRedisTemplate; | ||
// } | ||
// | ||
//} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/photobook/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,45 @@ | ||
package com.photobook.controller; | ||
|
||
import com.photobook.annotation.LoginCheck; | ||
import com.photobook.dto.UserDto; | ||
import com.photobook.service.LoginService; | ||
import com.photobook.service.UserService; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
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) { | ||
UserDto userInfo = userService.getUserInfoByIdAndPassword(id, password); | ||
|
||
loginService.setLoginUserInfo(userInfo); | ||
} | ||
|
||
@PostMapping("/logout") | ||
@LoginCheck | ||
public void logout() { | ||
loginService.removeLoginUserInfo(); | ||
} | ||
|
||
@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
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
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(Exception.class) | ||
public String exception(Exception e) { | ||
return e.getMessage(); | ||
} | ||
|
||
@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); | ||
} | ||
} |
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.mapper; | ||
|
||
import com.photobook.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); | ||
|
||
} |
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.service; | ||
|
||
import com.photobook.dto.UserDto; | ||
|
||
import javax.servlet.http.HttpSession; | ||
|
||
public interface LoginService { | ||
|
||
void setLoginUserInfo(UserDto userDto); | ||
|
||
void removeLoginUserInfo(); | ||
|
||
UserDto getLoginUserInfo(); | ||
|
||
} |
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.service; | ||
|
||
import com.photobook.dto.UserDto; | ||
|
||
public interface UserService { | ||
|
||
UserDto getUserInfoByIdAndPassword(String id, String password); | ||
|
||
UserDto getUserInfoById(String id); | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/photobook/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,35 @@ | ||
package com.photobook.service.impl; | ||
|
||
import com.photobook.dto.UserDto; | ||
import com.photobook.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"; | ||
|
||
private final HttpSession httpSession; | ||
|
||
public LoginServiceImpl(HttpSession httpSession) { | ||
this.httpSession = httpSession; // Scoped Proxy | ||
} | ||
|
||
@Override | ||
public void setLoginUserInfo(UserDto userDto) { | ||
httpSession.setAttribute(LOGIN_USER_INFO, userDto); | ||
} | ||
|
||
@Override | ||
public void removeLoginUserInfo() { | ||
httpSession.invalidate(); | ||
} | ||
|
||
@Override | ||
public UserDto getLoginUserInfo() { | ||
return (UserDto) httpSession.getAttribute(LOGIN_USER_INFO); | ||
} | ||
} | ||
|
33 changes: 33 additions & 0 deletions
33
src/main/java/com/photobook/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,33 @@ | ||
package com.photobook.service.impl; | ||
|
||
import com.photobook.mapper.UserMapper; | ||
import com.photobook.dto.UserDto; | ||
import com.photobook.service.UserService; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.Assert; | ||
|
||
@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); | ||
|
||
Assert.notNull(userInfo, "아이디 또는 비밀번호가 잘못 입력 되었습니다."); | ||
|
||
return userInfo; | ||
} | ||
|
||
@Override | ||
public UserDto getUserInfoById(String id) { | ||
UserDto userInfo = userMapper.getUserInfoById(id); | ||
return userInfo; | ||
} | ||
} |
Oops, something went wrong.