-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: CustomJsonAuthenticationFilter 생성 (#4)
- Loading branch information
1 parent
69b4719
commit ac32aa5
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/main/java/com/project/mapdagu/domain/auth/filter/CustomJsonAuthenticationFilter.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,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<String, String> 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); | ||
} | ||
} | ||
|