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

Commit

Permalink
Merge pull request #4 from f-lab-edu/feature/#2/login
Browse files Browse the repository at this point in the history
Feature/#2/login
  • Loading branch information
jsy3831 committed Sep 10, 2021
2 parents e5525b5 + 03ba493 commit 0a5d6ec
Show file tree
Hide file tree
Showing 25 changed files with 394 additions and 115 deletions.
9 changes: 6 additions & 3 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,11 +20,14 @@ 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-validation'
implementation 'org.springframework.boot:spring-boot-starter-aop'
// 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'
annotationProcessor 'org.projectlombok:lombok'
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 {
}
30 changes: 30 additions & 0 deletions src/main/java/com/photobook/aop/AuthCheckAspect.java
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("로그인된 사용자 정보가 존재하지 않습니다.");
}
}

}
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
38 changes: 38 additions & 0 deletions src/main/java/com/photobook/config/RedisConfig.java
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 src/main/java/com/photobook/controller/UserController.java
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.sns.untitled.user.model;
package com.photobook.dto;

import lombok.Getter;
import lombok.Setter;

import java.util.Date;
import java.time.LocalDate;

@Getter
@Setter
public class User {
public class UserDto {

private int userId;

Expand All @@ -19,10 +19,12 @@ public class User {

private String email;

private Date birth;
private LocalDate birth;

private String profileImageName;

private String profileImagePath;

private String profileMessage;

}
36 changes: 36 additions & 0 deletions src/main/java/com/photobook/exception/ExceptionAdvice.java
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();
}

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

}
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();

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

33 changes: 33 additions & 0 deletions src/main/java/com/photobook/service/impl/UserServiceImpl.java
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;
}
}
Loading

0 comments on commit 0a5d6ec

Please sign in to comment.