Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Commit

Permalink
#2 코드리뷰반영 / 로그인체크 aspect 추가 / controllerAdvice 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jsy3831 committed Aug 28, 2021
1 parent aae32d4 commit 3ff18e9
Show file tree
Hide file tree
Showing 25 changed files with 342 additions and 176 deletions.
12 changes: 7 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
id 'java'
}

group = 'com.sns'
group = 'com'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

Expand All @@ -20,13 +20,15 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0'
implementation 'org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16'
implementation 'junit:junit:4.13.1'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.session:spring-session-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-aop'
// implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
// implementation 'junit:junit:4.13.1'
// implementation 'org.springframework.boot:spring-boot-starter-data-redis'
// implementation 'org.springframework.session:spring-session-data-redis'

compileOnly 'org.projectlombok:lombok'
runtimeOnly 'mysql:mysql-connector-java'
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = 'untitled'
rootProject.name = 'photobook'
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);
}

}
9 changes: 9 additions & 0 deletions src/main/java/com/photobook/annotation/LoginCheck.java
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 {
}
37 changes: 37 additions & 0 deletions src/main/java/com/photobook/aop/AuthCheckAspect.java
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("로그인된 사용자 정보가 존재하지 않습니다.");
}

}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sns.untitled.config;
package com.photobook.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
Expand All @@ -11,7 +11,7 @@
import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.sns.untitled.*.mapper")
@MapperScan(basePackages = "com.photobook.*.mapper")
public class MyBatisConfig {

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.sns.untitled.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;

//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 {
Expand Down Expand Up @@ -35,4 +35,4 @@
// return stringRedisTemplate;
// }
//
//}
//}
31 changes: 31 additions & 0 deletions src/main/java/com/photobook/exception/ExceptionAdvice.java
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();
}

}
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 src/main/java/com/photobook/user/controller/UserController.java
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.sns.untitled.user.dto;
package com.photobook.user.dto;

import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;
import java.time.LocalDate;

@Getter
@Setter
Expand All @@ -19,7 +19,7 @@ public class UserDto {

private String email;

private LocalDateTime birth;
private LocalDate birth;

private String profileImageName;

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/photobook/user/mapper/UserMapper.java
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 src/main/java/com/photobook/user/service/LoginService.java
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);

}
11 changes: 11 additions & 0 deletions src/main/java/com/photobook/user/service/UserService.java
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);

}
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 src/main/java/com/photobook/user/service/impl/UserServiceImpl.java
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 src/main/java/com/sns/untitled/user/controller/UserController.java

This file was deleted.

Loading

0 comments on commit 3ff18e9

Please sign in to comment.