Skip to content

Commit 393b6aa

Browse files
authored
[BUG] 인증 서버 오류 (#193)
* fix: 인증 서버 오류 (#168) * fix: JSON 변환 오류 로깅 (#168)
1 parent 9c8a85b commit 393b6aa

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

src/main/java/com/smunity/server/domain/auth/dto/AuthCourseResponseDto.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
import com.smunity.server.global.common.entity.enums.Category;
55
import com.smunity.server.global.common.entity.enums.SubDomain;
66
import lombok.Builder;
7+
import lombok.extern.slf4j.Slf4j;
78
import org.json.JSONArray;
9+
import org.json.JSONException;
810
import org.json.JSONObject;
911

1012
import java.util.List;
13+
import java.util.Optional;
1114
import java.util.stream.IntStream;
1215

16+
@Slf4j
1317
@Builder
1418
public record AuthCourseResponseDto(
1519
String number,
@@ -25,10 +29,20 @@ public record AuthCourseResponseDto(
2529
public static List<AuthCourseResponseDto> from(JSONArray objs) {
2630
return IntStream.range(0, objs.length())
2731
.mapToObj(i -> from(objs.getJSONObject(i)))
32+
.flatMap(Optional::stream)
2833
.toList();
2934
}
3035

31-
private static AuthCourseResponseDto from(JSONObject obj) {
36+
private static Optional<AuthCourseResponseDto> from(JSONObject obj) {
37+
try {
38+
return Optional.of(of(obj));
39+
} catch (JSONException e) {
40+
log.error("[ERROR] Failed to convert JSON object: '{}'.", obj, e);
41+
return Optional.empty();
42+
}
43+
}
44+
45+
private static AuthCourseResponseDto of(JSONObject obj) {
3246
return AuthCourseResponseDto.builder()
3347
.number(obj.getString("SBJ_NO"))
3448
.name(obj.getString("SBJ_NM"))

src/main/java/com/smunity/server/domain/auth/util/AuthUtil.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
public class AuthUtil {
2222

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

2626
public static JSONArray getCourses(AuthRequestDto requestDto) {
27-
return getData(requestDto, "/UsrRecMatt/list.do", "dsRecMattList");
27+
return getData(requestDto, "UsrRecMatt/list.do", "dsRecMattList");
2828
}
2929

3030
public static JSONObject getInfo(AuthRequestDto requestDto) {
31-
JSONArray response = getData(requestDto, "/UsrSchMng/selectStdInfo.do", "dsStdInfoList");
31+
JSONArray response = getData(requestDto, "UsrSchMng/selectStdInfo.do", "dsStdInfoList");
3232
return response.getJSONObject(0);
3333
}
3434

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

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

7778
private static Map<String, String> getSessionCookies(Connection.Response loginResponse) throws IOException {
78-
return Jsoup.connect(BASE_URL + "/index.do")
79+
Connection.Response response = Jsoup.connect(BASE_URL + "index.do")
7980
.method(Connection.Method.GET)
8081
.cookies(loginResponse.cookies())
81-
.execute()
82-
.cookies();
82+
.execute();
83+
if (!response.url().toString().equals(BASE_URL))
84+
throw new GeneralException(ErrorCode.AUTH_EXCEEDED_LOGIN_ATTEMPTS);
85+
return response.cookies();
8386
}
8487

8588
private static HttpURLConnection createConnection(String url, Map<String, String> session) throws IOException {

src/main/java/com/smunity/server/global/exception/code/ErrorCode.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ public enum ErrorCode {
3535
// Auth Errors
3636
AUTH_UNAUTHORIZED(401, "AUTH001", "아이디 및 비밀번호가 일치하지 않습니다."),
3737
AUTH_LOGIN_FAIL(401, "AUTH002", "샘물포털 로그인에 실패했습니다."),
38-
AUTH_INTERNAL_SERVER_ERROR(500, "AUTH003", "인증 서버 에러, 관리자에게 문의 바랍니다."),
39-
AUTH_INVALID_FORMAT(500, "AUTH004", "응답 형식 오류, 관리자에게 문의 바랍니다."),
38+
AUTH_FETCH_FAILURE(401, "AUTH003", "인증 서버 에러, 관리자에게 문의 바랍니다."),
39+
AUTH_INVALID_FORMAT(401, "AUTH004", "응답 형식 오류, 관리자에게 문의 바랍니다."),
40+
AUTH_EXCEEDED_LOGIN_ATTEMPTS(401, "AUTH005", "로그인 실패 5회 초과, 샘물 포탈을 통해 비밀번호 초기화를 진행해주시기 바랍니다."),
4041

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

0 commit comments

Comments
 (0)