forked from EnjoyTripKorea/EnjoyTripBackend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request EnjoyTripKorea#5 from EnjoyTripKorea/feature/login
1. 로그인 기능 구현입니다
- Loading branch information
Showing
11 changed files
with
237 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/member/LoginRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.example.EnjoyTripBackend.dto.member; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class LoginRequestDto { | ||
private String email; | ||
private String password; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/filter/CorsFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.example.EnjoyTripBackend.filter; | ||
|
||
import jakarta.servlet.*; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.IOException; | ||
|
||
@Slf4j | ||
public class CorsFilter implements Filter { | ||
|
||
public void init(FilterConfig filterConfig) throws ServletException {} | ||
|
||
public void destroy() {} | ||
|
||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { | ||
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse; | ||
|
||
httpResponse.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:5500"); | ||
httpResponse.setHeader("Access-Control-Allow-Credentials", "true"); | ||
httpResponse.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS, GET, DELETE, PUT"); | ||
httpResponse.setHeader("Access-Control-Max-Age", "3600"); | ||
httpResponse.setHeader("Access-Control-Allow-Headers", "Authorization, x-requested-with, origin, content-type, accept"); | ||
chain.doFilter(servletRequest, servletResponse); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/filter/LoginCheckFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.example.EnjoyTripBackend.filter; | ||
|
||
import com.example.EnjoyTripBackend.exception.EnjoyTripException; | ||
import com.example.EnjoyTripBackend.exception.ErrorCode; | ||
import com.example.EnjoyTripBackend.util.SessionConst; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.*; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.servlet.http.HttpSession; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.util.PatternMatchUtils; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.springframework.http.MediaType.*; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class LoginCheckFilter implements Filter { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
private static final String[] whitelist = {"/api/signup", "/api/login", "/api/logout"}; | ||
|
||
@Override | ||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { | ||
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; | ||
String requestURI = httpRequest.getRequestURI(); | ||
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse; | ||
|
||
try { | ||
log.info("인증 체크 필터 시작 {}", requestURI); | ||
if (isLoginCheckPath(requestURI)) { | ||
log.info("인증 체크 로직 실행 {}", requestURI); | ||
|
||
HttpSession session = httpRequest.getSession(false); | ||
if ((session == null) || (session.getAttribute(SessionConst.LOGIN_MEMBER) == null)) { | ||
log.info("미인증 사용자 요청 {}", requestURI); | ||
AnonymousUserHandler(httpResponse, ErrorCode.ANONYMOUS_USER.getMessage()); | ||
} | ||
} | ||
filterChain.doFilter(servletRequest, servletResponse); | ||
} catch (Exception e) { | ||
throw new EnjoyTripException(ErrorCode.ANONYMOUS_USER, e.getMessage()); | ||
} finally { | ||
log.info("인증 체크 필터 종료 {}", requestURI); | ||
} | ||
} | ||
|
||
private void AnonymousUserHandler(HttpServletResponse response, String message) throws IOException{ | ||
response.setStatus(HttpStatus.FORBIDDEN.value()); | ||
response.setCharacterEncoding("UTF-8"); | ||
response.setContentType(APPLICATION_JSON_VALUE); | ||
objectMapper.writeValue(response.getWriter(), new ResponseEntity(message, null, HttpStatus.FORBIDDEN.value())); | ||
} | ||
|
||
private boolean isLoginCheckPath(String requestURI) { | ||
return !PatternMatchUtils.simpleMatch(whitelist, requestURI); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/filter/WebConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.example.EnjoyTripBackend.filter; | ||
|
||
import com.example.EnjoyTripBackend.util.SessionUserArgumentResolver; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.Filter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
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; | ||
|
||
@Slf4j | ||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { | ||
resolvers.add(new SessionUserArgumentResolver()); | ||
} | ||
|
||
@Bean | ||
public FilterRegistrationBean LoginCheckFilter() { | ||
FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>(); | ||
filterRegistrationBean.setFilter(new LoginCheckFilter(new ObjectMapper())); | ||
filterRegistrationBean.setOrder(1); | ||
filterRegistrationBean.addUrlPatterns("/*"); | ||
return filterRegistrationBean; | ||
} | ||
|
||
@Bean | ||
public FilterRegistrationBean CorsFilter() { | ||
FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>(); | ||
filterRegistrationBean.setFilter(new CorsFilter()); | ||
filterRegistrationBean.setOrder(0); | ||
filterRegistrationBean.addUrlPatterns("/*"); | ||
return filterRegistrationBean; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/util/SessionConst.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.example.EnjoyTripBackend.util; | ||
|
||
public class SessionConst { | ||
public static final String LOGIN_MEMBER = "loginMember"; | ||
} |
11 changes: 11 additions & 0 deletions
11
EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/util/SessionUser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.example.EnjoyTripBackend.util; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface SessionUser { | ||
} |
28 changes: 28 additions & 0 deletions
28
...pBackend/src/main/java/com/example/EnjoyTripBackend/util/SessionUserArgumentResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.example.EnjoyTripBackend.util; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.core.MethodParameter; | ||
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; | ||
|
||
import static com.example.EnjoyTripBackend.util.SessionConst.LOGIN_MEMBER; | ||
|
||
@Slf4j | ||
public class SessionUserArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
boolean hasSessionUserAnnotation = parameter.hasParameterAnnotation(SessionUser.class); | ||
boolean hasStringType = parameter.getParameterType().equals(String.class); | ||
return hasSessionUserAnnotation && hasStringType; | ||
} | ||
|
||
@Override | ||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { | ||
HttpServletRequest httpRequest = (HttpServletRequest) webRequest.getNativeRequest(); | ||
return (String) httpRequest.getSession().getAttribute(LOGIN_MEMBER); | ||
} | ||
} |