Skip to content

Commit

Permalink
Merge pull request #270 from team9502/dev
Browse files Browse the repository at this point in the history
๋ฐฐํฌ
  • Loading branch information
daeundada authored Jul 1, 2024
2 parents f9003b7 + 771f278 commit ec47d52
Showing 1 changed file with 62 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.server.Cookie;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseCookie;
Expand Down Expand Up @@ -78,27 +77,74 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
}
}

// @Override
// public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
// try {
// if (request.getRequestURI().contains("/cp-login")) {
// CompanyUserLoginRequestDTO loginRequest = objectMapper.readValue(request.getInputStream(), CompanyUserLoginRequestDTO.class);
// if (loginRequest.getCpEmail() == null || loginRequest.getCpPassword() == null) {
// throw new ApiException(ErrorCode.INVALID_INPUT);
// }
// UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
// loginRequest.getCpEmail(), loginRequest.getCpPassword());
// return getAuthenticationManager().authenticate(authRequest);
// } else {
// UserLoginRequestDTO loginRequest = objectMapper.readValue(request.getInputStream(), UserLoginRequestDTO.class);
// if (loginRequest.getEmail() == null || loginRequest.getPassword() == null) {
// throw new ApiException(ErrorCode.INVALID_INPUT);
// }
// UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
// loginRequest.getEmail(), loginRequest.getPassword());
// return getAuthenticationManager().authenticate(authRequest);
// }
// } catch (IOException e) {
// logger.error("Error reading login request", e);
// throw new ApiException(ErrorCode.INTERNAL_SERVER_ERROR);
// }
// }


@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
String accessToken = tokenProvider.generateToken(authResult);

ResponseCookie cookie = ResponseCookie.from("AUTH_TOKEN", accessToken)
.path("/")
.domain(".sinchulgwinong.site")
.maxAge(60 * 60)
.httpOnly(true)
.secure(true) // ๋กœ์ปฌ ํ…Œ์ŠคํŠธ์‹œ ์ฃผ์„ ํ•„์š”
.sameSite("None") // ๋กœ์ปฌ ํ…Œ์ŠคํŠธ์‹œ ์ฃผ์„ ํ•„์š”
.build();

response.setHeader("Set-Cookie", cookie.toString());
response.setStatus(HttpStatus.OK.value());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding(StandardCharsets.UTF_8.name());

UserDetails userDetails = (UserDetails) authResult.getPrincipal();
Object userResponseDto = getUserResponseDTO(request.getRequestURI(), userDetails.getUsername());

// ResponseCookie cookie = ResponseCookie.from("AUTH_TOKEN", accessToken)
// .path("/")
//// .domain(".sinchulgwinong.site")
// .maxAge(60 * 60)
// .httpOnly(true)
//// .secure(true) // ๋กœ์ปฌ ํ…Œ์ŠคํŠธ์‹œ ์ฃผ์„ ํ•„์š”
//// .sameSite("None") // ๋กœ์ปฌ ํ…Œ์ŠคํŠธ์‹œ ์ฃผ์„ ํ•„์š”
// .build();

// ๋กœ๊ทธ์ธ ๊ฒฝ๋กœ์™€ ์‚ฌ์šฉ์ž ์œ ํ˜•์ด ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ฟ ํ‚ค๋ฅผ ์„ค์ •
boolean isUserTypeMatch = (request.getRequestURI().contains("/auth/login") && userResponseDto instanceof UserLoginResponseDTO) ||
(request.getRequestURI().contains("/auth/cp-login") && userResponseDto instanceof CompanyUserLoginResponseDTO);

if (isUserTypeMatch) {
String accessToken = tokenProvider.generateToken(authResult);
ResponseCookie cookie = ResponseCookie.from("AUTH_TOKEN", accessToken)
.path("/")
.domain(".sinchulgwinong.site") // ๋กœ์ปฌ ํ…Œ์ŠคํŠธ์‹œ ์ฃผ์„ ํ•„์š”
.maxAge(60 * 60)
.httpOnly(true)
.secure(true) // ๋กœ์ปฌ ํ…Œ์ŠคํŠธ์‹œ ์ฃผ์„ ํ•„์š”
.sameSite("None") // ๋กœ์ปฌ ํ…Œ์ŠคํŠธ์‹œ ์ฃผ์„ ํ•„์š”
.build();
response.setHeader("Set-Cookie", cookie.toString());
} else {
// ์‚ฌ์šฉ์ž ์œ ํ˜•๊ณผ ๊ฒฝ๋กœ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ๊ฒฝ๊ณ  ๋กœ๊ทธ๋ฅผ ๋‚จ๊น๋‹ˆ๋‹ค.
logger.warn("์‚ฌ์šฉ์ž ํƒ€์ž…๊ณผ ๊ฒฝ๋กœ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ");
throw new ApiException(ErrorCode.INVALID_USER_TYPE);
}

// response.setHeader("Set-Cookie", cookie.toString());
response.setStatus(HttpStatus.OK.value());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding(StandardCharsets.UTF_8.name());

GlobalApiResponse<Object> globalApiResponse = GlobalApiResponse.of(SuccessCode.OK.getMessage(), userResponseDto);
response.getWriter().write(objectMapper.writeValueAsString(globalApiResponse));
}
Expand Down

0 comments on commit ec47d52

Please sign in to comment.