Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[김태형] 프리코스 제출합니다. #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
# spring-security-authentication

## 기능 요구 사항
+ POST /login 경로로 로그인 요청을 한다.
+ GET /member 요청 시 사용자 목록을 조회한다.
+ 아이디와 비밀번호를 기반으로 로그인 기능을 구현한다.
+ 세션으로 사용자의 인증 정보를 저장한다.
+ Basic인증을 사용하여 사용자를 식별한다.
+ 요청의 Authorization 헤더에서 Basic 인증 정보를 저장한다.
+ Member Test의 모든 테스트가 통과해야 한다.


## 프로그래밍 요구사항
+ 자바 코드 컨벤션을 지키면서 프로그래밍한다.
+ 기본적으로 Google Java Style Guide를 원칙으로 한다.
+ 단, 들여쓰기는 '2 spaces'가 아닌 '4 spaces'로 한다.
+ indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현한다. 2까지만 허용한다.
+ 예를 들어 while문 안에 if문이 있으면 들여쓰기는 2이다.
+ 힌트: indent(인덴트, 들여쓰기) depth를 줄이는 좋은 방법은 함수(또는 메서드)를 분리하면 된다.
+ 3항 연산자를 쓰지 않는다.
+ 함수(또는 메서드)의 길이가 15라인을 넘어가지 않도록 구현한다.
+ 함수(또는 메서드)가 한 가지 일만 잘 하도록 구현한다.
+ else 예약어를 쓰지 않는다.
+ else를 쓰지 말라고 하니 switch/case로 구현하는 경우가 있는데 switch/case도 허용하지 않는다.
+ 힌트: if 조건절에서 값을 return하는 방식으로 구현하면 else를 사용하지 않아도 된다.
+ JUnit 5와 AssertJ를 이용하여 정리한 기능 목록이 정상적으로 작동하는지 테스트 코드로 확인한다.

19 changes: 19 additions & 0 deletions src/main/java/nextstep/app/service/MemberService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package nextstep.app.service;

import nextstep.app.domain.MemberRepository;
import nextstep.security.service.MemberValidationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MemberService implements MemberValidationService {
private final MemberRepository memberRepository;
@Autowired
public MemberService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
@Override
public boolean isValidMember(String email, String password) {
return memberRepository.findByEmail(email).isPresent();
}
}
1 change: 0 additions & 1 deletion src/main/java/nextstep/app/ui/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

@RestController
public class LoginController {
public static final String SPRING_SECURITY_CONTEXT_KEY = "SPRING_SECURITY_CONTEXT";

private final MemberRepository memberRepository;

Expand Down
31 changes: 31 additions & 0 deletions src/main/java/nextstep/security/config/WebMvcConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nextstep.security.config;
import nextstep.security.interceptor.BasicAuthenticationInterceptor;
import nextstep.security.interceptor.LoginInterceptor;
import nextstep.security.interceptor.MemberValidationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
private final BasicAuthenticationInterceptor basicAuthenticationInterceptor;
private final MemberValidationInterceptor memberValidationInterceptor;
private final LoginInterceptor loginInterceptor;

@Autowired
public WebMvcConfig(BasicAuthenticationInterceptor basicAuthenticationInterceptor,
MemberValidationInterceptor memberValidationInterceptor,
LoginInterceptor loginInterceptor) {
this.basicAuthenticationInterceptor = basicAuthenticationInterceptor;
this.memberValidationInterceptor = memberValidationInterceptor;
this.loginInterceptor = loginInterceptor;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor).addPathPatterns("/login/**");
registry.addInterceptor(basicAuthenticationInterceptor).order(0).addPathPatterns("/members/**");
registry.addInterceptor(memberValidationInterceptor).order(1).addPathPatterns("/members/**");
}
}
13 changes: 13 additions & 0 deletions src/main/java/nextstep/security/constants/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package nextstep.security.constants;

public class Constants {
public static class Http {
public static final String AUTHORIZATION = "Authorization";
}
public static class Auth {
public static final String BASIC = "Basic ";
public static final String EMAIL = "email";
public static final String PASSWORD = "password";
public static final String SPRING_SECURITY_CONTEXT_KEY = "SPRING_SECURITY_CONTEXT";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nextstep.security.interceptor;

import nextstep.security.constants.Constants;
import nextstep.security.model.EmailPasswordAuth;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@Component
public class BasicAuthenticationInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
System.out.println("BasicAuthenticationInterceptor preHandle");
String authorization = request.getHeader(Constants.Http.AUTHORIZATION);
if (Objects.isNull(EmailPasswordAuth.from(authorization))) {
response.setStatus(HttpStatus.UNAUTHORIZED.value());
return false;
}
return true;
}
}
31 changes: 31 additions & 0 deletions src/main/java/nextstep/security/interceptor/LoginInterceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nextstep.security.interceptor;

import nextstep.security.constants.Constants;
import nextstep.security.model.EmailPasswordAuth;
import nextstep.security.service.MemberValidationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
private final MemberValidationService memberValidationService;
@Autowired
public LoginInterceptor(MemberValidationService memberValidationService) {
this.memberValidationService = memberValidationService;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String email = request.getParameter(Constants.Auth.EMAIL);
String password = request.getParameter(Constants.Auth.PASSWORD);
if(memberValidationService.isValidMember(email, password)) {
request.getSession().setAttribute(Constants.Auth.SPRING_SECURITY_CONTEXT_KEY,
new EmailPasswordAuth(email, password));
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package nextstep.security.interceptor;

import nextstep.security.constants.Constants;
import nextstep.security.model.EmailPasswordAuth;
import nextstep.security.service.MemberValidationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@Component
public class MemberValidationInterceptor implements HandlerInterceptor {
private final MemberValidationService memberValidationService;
@Autowired
public MemberValidationInterceptor(MemberValidationService memberValidationService) {
this.memberValidationService = memberValidationService;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
System.out.println("MemberValidationInterceptor preHandle");
String authorization = request.getHeader(Constants.Http.AUTHORIZATION);
EmailPasswordAuth emailPasswordAuth = EmailPasswordAuth.from(authorization);
if (Objects.isNull(emailPasswordAuth)) {
response.setStatus(HttpStatus.UNAUTHORIZED.value());
return false;
}
if(memberValidationService.isValidMember(emailPasswordAuth.getEmail(),
emailPasswordAuth.getPassword())) {
response.setStatus(HttpStatus.UNAUTHORIZED.value());
return false;
}
return true;
}
}
52 changes: 52 additions & 0 deletions src/main/java/nextstep/security/model/EmailPasswordAuth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package nextstep.security.model;

import nextstep.security.constants.Constants;
import org.springframework.util.Base64Utils;

import java.util.Objects;

public class EmailPasswordAuth {
private String email;
private String password;

public String getEmail() {
return email;
}
public String getPassword() {
return password;
}

public EmailPasswordAuth(String email, String password) {
this.email = email;
this.password = password;
}

public static EmailPasswordAuth from(String basicAuth) {
if(Objects.isNull(basicAuth) || !basicAuth.startsWith(Constants.Auth.BASIC)) {
return null;
}
String base64Credentials = basicAuth.substring(Constants.Auth.BASIC.length()).trim();
try {
// Base64 문자열을 디코딩
byte[] decoded = Base64Utils.decodeFromString(base64Credentials);
String credentials = new String(decoded);

// 사용자 이름과 비밀번호가 ':'로 구분되는지 확인
String[] parts = credentials.split(":", 2);
if(parts.length == 2) {
return new EmailPasswordAuth(parts[0], parts[1]);
}
return null;
} catch (IllegalArgumentException e) {
e.printStackTrace();
return null;
}
}

public static EmailPasswordAuth from(String email, String password) {
if(email == null || password == null) {
return null;
}
return new EmailPasswordAuth(email, password);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package nextstep.security.service;

public interface MemberValidationService {
boolean isValidMember(String email, String password);
}