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

Commit

Permalink
#2 패키지구조변경 / session 의존성 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
jsy3831 committed Sep 9, 2021
1 parent 3ff18e9 commit 03ba493
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 91 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ dependencies {
implementation 'org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16'
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'

Expand Down
17 changes: 5 additions & 12 deletions src/main/java/com/photobook/aop/AuthCheckAspect.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.photobook.aop;

import com.photobook.user.service.LoginService;
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;
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;
private final LoginService loginService;

public AuthCheckAspect(LoginService loginService) {
this.loginService = loginService;
Expand All @@ -23,15 +20,11 @@ public AuthCheckAspect(LoginService loginService) {
@Before("@annotation(com.photobook.annotation.LoginCheck)")
public void loginCheck() {

HttpSession httpSession = ((ServletRequestAttributes) RequestContextHolder
.currentRequestAttributes()) // RequestAttributes가 없으면 예외 발생
.getRequest()
.getSession();
UserDto userInfo = loginService.getLoginUserInfo();

if (loginService.getLoginUserInfo(httpSession) == null) {
if(userInfo == null) {
throw new UnauthorizedException("로그인된 사용자 정보가 존재하지 않습니다.");
}

}

}
2 changes: 1 addition & 1 deletion src/main/java/com/photobook/config/MyBatisConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import javax.sql.DataSource;

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

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.photobook.user.controller;
package com.photobook.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 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.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;

@RestController
Expand All @@ -25,16 +24,16 @@ public UserController(UserService userService, LoginService loginService) {
}

@PostMapping("/login")
public void login(@RequestParam @NotBlank String id, @RequestParam @NotBlank String password, HttpSession httpSession) {
public void login(@RequestParam @NotBlank String id, @RequestParam @NotBlank String password) {
UserDto userInfo = userService.getUserInfoByIdAndPassword(id, password);

loginService.setLoginUserInfo(httpSession, userInfo);
loginService.setLoginUserInfo(userInfo);
}

@PostMapping("/logout")
@LoginCheck
public void logout(HttpSession httpSession) {
loginService.removeLoginUserInfo(httpSession);
public void logout() {
loginService.removeLoginUserInfo();
}

@GetMapping("/{id}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.photobook.user.dto;
package com.photobook.dto;

import lombok.Getter;
import lombok.Setter;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/photobook/exception/ExceptionAdvice.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
@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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.photobook.user.mapper;
package com.photobook.mapper;

import com.photobook.user.dto.UserDto;
import com.photobook.dto.UserDto;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

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

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.photobook.user.service;
package com.photobook.service;

import com.photobook.user.dto.UserDto;
import com.photobook.dto.UserDto;

public interface UserService {

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/photobook/service/impl/LoginServiceImpl.java
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);
}
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.photobook.user.service.impl;
package com.photobook.service.impl;

import com.photobook.user.mapper.UserMapper;
import com.photobook.user.dto.UserDto;
import com.photobook.user.service.UserService;
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 {
Expand All @@ -19,9 +20,7 @@ public UserDto getUserInfoByIdAndPassword(String id, String password) {

UserDto userInfo = userMapper.getUserInfoByIdAndPassword(id, password);

if(userInfo == null) {
throw new IllegalArgumentException("아이디 또는 비밀번호가 잘못 입력 되었습니다.");
}
Assert.notNull(userInfo, "아이디 또는 비밀번호가 잘못 입력 되었습니다.");

return userInfo;
}
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/com/photobook/user/service/LoginService.java

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/resources/mapper/UserMapper.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.photobook.user.mapper.UserMapper">
<mapper namespace="com.photobook.mapper.UserMapper">

<resultMap id="userDto" type="com.photobook.user.dto.UserDto">
<resultMap id="userDto" type="com.photobook.dto.UserDto">
<result property="userId" column="user_id"/>
<result property="id" column="id"/>
<result property="password" column="password"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.photobook.user.service.impl;
package com.photobook.service.impl;

import com.photobook.user.mapper.UserMapper;
import com.photobook.user.dto.UserDto;
import com.photobook.mapper.UserMapper;
import com.photobook.dto.UserDto;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -59,14 +59,11 @@ public void failureGetUserInfoByIdAndPassword() {
//given
when(userMapper.getUserInfoByIdAndPassword("notExistId", "notExistPassword")).thenReturn(null);

//when
IllegalArgumentException exception =
assertThrows(IllegalArgumentException.class, () -> {
userServiceImpl.getUserInfoByIdAndPassword("notExistId", "notExistPassword");
});

//then
assertEquals("아이디 또는 비밀번호가 잘못 입력 되었습니다.", exception.getMessage());
assertThrows(IllegalArgumentException.class, () -> {
//when
userServiceImpl.getUserInfoByIdAndPassword("notExistId", "notExistPassword");
});

}
}

0 comments on commit 03ba493

Please sign in to comment.