Skip to content

Commit

Permalink
[BUG] 인증 서버 오류 (#193)
Browse files Browse the repository at this point in the history
* fix: 인증 서버 오류 (#168)

* fix: JSON 변환 오류 로깅 (#168)
  • Loading branch information
hyunmin0317 authored Feb 1, 2025
1 parent 9c8a85b commit 393b6aa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import com.smunity.server.global.common.entity.enums.Category;
import com.smunity.server.global.common.entity.enums.SubDomain;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;

@Slf4j
@Builder
public record AuthCourseResponseDto(
String number,
Expand All @@ -25,10 +29,20 @@ public record AuthCourseResponseDto(
public static List<AuthCourseResponseDto> from(JSONArray objs) {
return IntStream.range(0, objs.length())
.mapToObj(i -> from(objs.getJSONObject(i)))
.flatMap(Optional::stream)
.toList();
}

private static AuthCourseResponseDto from(JSONObject obj) {
private static Optional<AuthCourseResponseDto> from(JSONObject obj) {
try {
return Optional.of(of(obj));
} catch (JSONException e) {
log.error("[ERROR] Failed to convert JSON object: '{}'.", obj, e);
return Optional.empty();
}
}

private static AuthCourseResponseDto of(JSONObject obj) {
return AuthCourseResponseDto.builder()
.number(obj.getString("SBJ_NO"))
.name(obj.getString("SBJ_NM"))
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/com/smunity/server/domain/auth/util/AuthUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
public class AuthUtil {

private static final String LOGIN_URL = "https://smsso.smu.ac.kr/Login.do";
private static final String BASE_URL = "https://smul.smu.ac.kr";
private static final String BASE_URL = "https://smul.smu.ac.kr/";

public static JSONArray getCourses(AuthRequestDto requestDto) {
return getData(requestDto, "/UsrRecMatt/list.do", "dsRecMattList");
return getData(requestDto, "UsrRecMatt/list.do", "dsRecMattList");
}

public static JSONObject getInfo(AuthRequestDto requestDto) {
JSONArray response = getData(requestDto, "/UsrSchMng/selectStdInfo.do", "dsStdInfoList");
JSONArray response = getData(requestDto, "UsrSchMng/selectStdInfo.do", "dsStdInfoList");
return response.getJSONObject(0);
}

Expand All @@ -49,8 +49,8 @@ private static JSONObject getData(AuthRequestDto requestDto, String url) {
connection.getOutputStream().write(createRequestData(requestDto));
return readResponse(connection);
} catch (IOException e) {
log.error("[ERROR] Failed to fetch data from URL: '{}'. Request: {}", url, requestDto, e);
throw new GeneralException(ErrorCode.AUTH_INTERNAL_SERVER_ERROR);
log.error("[ERROR] Failed to fetch data from URL: '{}'.", url, e);
throw new GeneralException(ErrorCode.AUTH_FETCH_FAILURE);
}
}

Expand All @@ -59,6 +59,7 @@ private static Map<String, String> login(AuthRequestDto requestDto) {
Connection.Response loginResponse = executeLogin(requestDto);
return getSessionCookies(loginResponse);
} catch (IOException e) {
log.error("[ERROR] Failed to Login.", e);
throw new GeneralException(ErrorCode.AUTH_LOGIN_FAIL);
}
}
Expand All @@ -75,11 +76,13 @@ private static Connection.Response executeLogin(AuthRequestDto requestDto) throw
}

private static Map<String, String> getSessionCookies(Connection.Response loginResponse) throws IOException {
return Jsoup.connect(BASE_URL + "/index.do")
Connection.Response response = Jsoup.connect(BASE_URL + "index.do")
.method(Connection.Method.GET)
.cookies(loginResponse.cookies())
.execute()
.cookies();
.execute();
if (!response.url().toString().equals(BASE_URL))
throw new GeneralException(ErrorCode.AUTH_EXCEEDED_LOGIN_ATTEMPTS);
return response.cookies();
}

private static HttpURLConnection createConnection(String url, Map<String, String> session) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public enum ErrorCode {
// Auth Errors
AUTH_UNAUTHORIZED(401, "AUTH001", "아이디 및 비밀번호가 일치하지 않습니다."),
AUTH_LOGIN_FAIL(401, "AUTH002", "샘물포털 로그인에 실패했습니다."),
AUTH_INTERNAL_SERVER_ERROR(500, "AUTH003", "인증 서버 에러, 관리자에게 문의 바랍니다."),
AUTH_INVALID_FORMAT(500, "AUTH004", "응답 형식 오류, 관리자에게 문의 바랍니다."),
AUTH_FETCH_FAILURE(401, "AUTH003", "인증 서버 에러, 관리자에게 문의 바랍니다."),
AUTH_INVALID_FORMAT(401, "AUTH004", "응답 형식 오류, 관리자에게 문의 바랍니다."),
AUTH_EXCEEDED_LOGIN_ATTEMPTS(401, "AUTH005", "로그인 실패 5회 초과, 샘물 포탈을 통해 비밀번호 초기화를 진행해주시기 바랍니다."),

// Member Errors
MEMBER_FORBIDDEN(403, "MEMBER001", "사용자 권한이 없습니다."),
Expand Down

0 comments on commit 393b6aa

Please sign in to comment.