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 #6 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 18, 2021
2 parents 0a5d6ec + 60e9721 commit f579dda
Show file tree
Hide file tree
Showing 26 changed files with 471 additions and 73 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# simple_sns
# photobook
instagram과 유사한 sns 서비스 입니다.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ 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-security'
implementation 'org.springframework.boot:spring-boot-starter-cache'
// implementation 'org.springframework.boot:spring-boot-starter-data-redis'
// implementation 'org.springframework.session:spring-session-data-redis'

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/photobook/PhotobookApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class PhotobookApplication {

public static void main(String[] args) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/photobook/annotation/LoginCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginCheck {
}
9 changes: 9 additions & 0 deletions src/main/java/com/photobook/annotation/LoginUser.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.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginUser {
}
2 changes: 1 addition & 1 deletion src/main/java/com/photobook/aop/AuthCheckAspect.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.photobook.aop;

import com.photobook.exception.UnauthorizedException;
import com.photobook.dto.UserDto;
import com.photobook.exception.customException.UnauthorizedException;
import com.photobook.service.LoginService;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/photobook/config/WebMvcConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.photobook.config;

import com.photobook.controller.argumentResolver.LoginUserArgumentResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

private final LoginUserArgumentResolver loginUserArgumentResolver;

public WebMvcConfig(LoginUserArgumentResolver loginUserArgumentResolver) {
this.loginUserArgumentResolver = loginUserArgumentResolver;
}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(loginUserArgumentResolver);
}

}

28 changes: 28 additions & 0 deletions src/main/java/com/photobook/config/WebSecurityConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.photobook.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Bean
public PasswordEncoder getPasswordEncoder() {
return new BCryptPasswordEncoder();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().disable()
.csrf().disable()
.formLogin().disable()
.headers().frameOptions().disable();
}

}
60 changes: 54 additions & 6 deletions src/main/java/com/photobook/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.photobook.controller;

import com.photobook.annotation.LoginCheck;
import com.photobook.annotation.LoginUser;
import com.photobook.controller.response.Response;
import com.photobook.dto.UserDto;
import com.photobook.exception.customException.DuplicatedException;
import com.photobook.service.LoginService;
import com.photobook.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

@RestController
Expand All @@ -24,22 +29,65 @@ public UserController(UserService userService, LoginService loginService) {
}

@PostMapping("/login")
public void login(@RequestParam @NotBlank String id, @RequestParam @NotBlank String password) {
UserDto userInfo = userService.getUserInfoByIdAndPassword(id, password);
public Response<UserDto> login(@RequestParam @NotBlank String id, @RequestParam @NotBlank String password,
@LoginUser UserDto loginUser) {

if(loginUser != null) {
throw new DuplicatedException("이미 로그인된 상태입니다.");
}

UserDto userInfo = userService.validateLogin(id, password);

loginService.setLoginUserInfo(userInfo);

return Response.toResponse(200, "로그인 하였습니다.", userInfo);
}

@PostMapping("/logout")
@LoginCheck
public void logout() {
public Response logout() {

loginService.removeLoginUserInfo();

return Response.toResponse(200, "로그아웃 하였습니다.");
}

@GetMapping("/my-info")
@LoginCheck
public Response<UserDto> getUserInfoById(@LoginUser UserDto loginUser) {

UserDto userInfo = userService.getUserInfoById(loginUser.getId());

return Response.toResponse(200, "회원정보 조회", userInfo);
}

@PostMapping("/signup")
@ResponseStatus(HttpStatus.CREATED)
public Response signup(@RequestBody @Valid UserDto userInfo) {

userService.createUser(userInfo);

return Response.toResponse(201, "회원가입 하였습니다.");
}

@GetMapping("/{id}")
public Response checkUserId(@PathVariable @NotBlank String id) {

userService.validateUserId(id);

return Response.toResponse(200, "사용가능한 아이디입니다.");
}


@DeleteMapping("/my-info")
@LoginCheck
public UserDto getUserInfoById(@PathVariable @NotBlank String id) {
UserDto userInfo = userService.getUserInfoById(id);
return userInfo;
public Response deleteUser(@LoginUser UserDto loginUser) {

userService.deleteUser(loginUser.getId());

loginService.removeLoginUserInfo();

return Response.toResponse(200, "회원탈퇴 하였습니다.");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.photobook.controller.argumentResolver;

import com.photobook.annotation.LoginUser;
import com.photobook.service.LoginService;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

@Component
public class LoginUserArgumentResolver implements HandlerMethodArgumentResolver {

private final LoginService loginService;

public LoginUserArgumentResolver(LoginService loginService) {
this.loginService = loginService;
}

@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(LoginUser.class);
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
return loginService.getLoginUserInfo();
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/photobook/controller/response/Response.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.photobook.controller.response;

import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
public class Response<T> {

private final LocalDateTime timestamp = LocalDateTime.now();

private final int code;

private final String message;

private final T body;

@Builder
Response(final int code, final String message, final T body) {
this.code = code;
this.message = message;
this.body = body;
}

public static <T> Response<T> toResponse(int code, String message) {
return toResponse(code, message, null);
}

public static <T> Response<T> toResponse(int code, String message, T body) {
return Response.<T>builder()
.code(code)
.message(message)
.body(body)
.build();
}
}
13 changes: 12 additions & 1 deletion src/main/java/com/photobook/dto/UserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import javax.validation.constraints.*;
import java.io.Serializable;
import java.time.LocalDate;

@Getter
@Setter
public class UserDto {
@ToString
public class UserDto implements Serializable {

private int userId;

@NotBlank
private String id;

@NotBlank
private String password;

@NotBlank
private String name;

@NotBlank
@Email
private String email;

@NotNull
@Past
private LocalDate birth;

private String profileImageName;
Expand Down
36 changes: 0 additions & 36 deletions src/main/java/com/photobook/exception/ExceptionAdvice.java

This file was deleted.

Loading

0 comments on commit f579dda

Please sign in to comment.