From ac32aa591a56179a66b8e8ae90e350e3fec4afbe Mon Sep 17 00:00:00 2001 From: kyeong-hyeok Date: Thu, 3 Aug 2023 12:51:51 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20CustomJsonAuthenticationFilter=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20(#4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomJsonAuthenticationFilter.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/main/java/com/project/mapdagu/domain/auth/filter/CustomJsonAuthenticationFilter.java diff --git a/src/main/java/com/project/mapdagu/domain/auth/filter/CustomJsonAuthenticationFilter.java b/src/main/java/com/project/mapdagu/domain/auth/filter/CustomJsonAuthenticationFilter.java new file mode 100644 index 0000000..f3d8a5d --- /dev/null +++ b/src/main/java/com/project/mapdagu/domain/auth/filter/CustomJsonAuthenticationFilter.java @@ -0,0 +1,56 @@ +package com.project.mapdagu.domain.auth.filter; + +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.security.authentication.AuthenticationServiceException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; +import org.springframework.security.web.util.matcher.AntPathRequestMatcher; +import org.springframework.util.StreamUtils; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Map; + +public class CustomJsonAuthenticationFilter extends AbstractAuthenticationProcessingFilter { + + private static final String DEFAULT_LOGIN_REQUEST_URL = "/login"; + private static final String HTTP_METHOD = "POST"; + private static final String CONTENT_TYPE = "application/json"; + private static final String USERNAME_KEY = "email"; // 회원 로그인 시 이메일 요청 JSON Key : "email" + private static final String PASSWORD_KEY = "password"; // 회원 로그인 시 비밀번호 요청 JSon Key : "password" + private static final AntPathRequestMatcher DEFAULT_LOGIN_PATH_REQUEST_MATCHER = + new AntPathRequestMatcher(DEFAULT_LOGIN_REQUEST_URL, HTTP_METHOD); // "/login" + POST로 온 요청 + + private final ObjectMapper objectMapper; + + public CustomJsonAuthenticationFilter(ObjectMapper objectMapper) { + super(DEFAULT_LOGIN_PATH_REQUEST_MATCHER); // "login" + POST로 온 요청 처리 + this.objectMapper = objectMapper; + } + + /** + * 인증 처리 메소드 + */ + @Override + public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException { + if(request.getContentType() == null || !request.getContentType().equals(CONTENT_TYPE) ) { + throw new AuthenticationServiceException("Authentication Content-Type not supported: " + request.getContentType()); + } + + String messageBody = StreamUtils.copyToString(request.getInputStream(), StandardCharsets.UTF_8); + + Map usernamePasswordMap = objectMapper.readValue(messageBody, Map.class); + + String email = usernamePasswordMap.get(USERNAME_KEY); + String password = usernamePasswordMap.get(PASSWORD_KEY); + + UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(email, password); //principal 과 credentials 전달 + + return this.getAuthenticationManager().authenticate(authRequest); + } +} +