-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #283 from urinaner/feature/282
[BE] 세종대 구성원(교수, 교직원, 학생) 통합 로그인 로직 구현
- Loading branch information
Showing
7 changed files
with
112 additions
and
10 deletions.
There are no files selected for viewing
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
19 changes: 19 additions & 0 deletions
19
backend/src/main/java/org/example/backend/users/dto/SjLoginReq.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,19 @@ | ||
package org.example.backend.users.dto; | ||
|
||
public class SjLoginReq { | ||
private final String userId; | ||
private final String password; | ||
|
||
public SjLoginReq(String userId, String password) { | ||
this.userId = userId; | ||
this.password = password; | ||
} | ||
|
||
public String getUserId() { | ||
return userId; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
backend/src/main/java/org/example/backend/users/dto/SjUserProfile.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,19 @@ | ||
package org.example.backend.users.dto; | ||
|
||
public class SjUserProfile { | ||
private final String name; | ||
private final String major; | ||
|
||
public SjUserProfile(String name, String major) { | ||
this.name = name; | ||
this.major = major; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getMajor() { | ||
return major; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
backend/src/main/java/org/example/backend/users/service/SjAuthService.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,61 @@ | ||
package org.example.backend.users.service; | ||
|
||
|
||
import java.io.IOException; | ||
import org.example.backend.users.dto.SjLoginReq; | ||
import org.example.backend.users.dto.SjUserProfile; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.jsoup.Connection; | ||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.nodes.Element; | ||
import org.springframework.stereotype.Component; | ||
|
||
|
||
@Component | ||
public class SjAuthService { | ||
private final String MOODLER_LOGIN_URL = "https://sjulms.moodler.kr/login/index.php"; | ||
private final String AUTH_FAILED = "인증에 실패하였습니다."; | ||
private final String USER_INFO_MISSING = "사용자 정보를 찾을 수 없습니다."; | ||
|
||
public SjUserProfile authenticate(SjLoginReq loginRequest) throws AuthenticationException { | ||
try { | ||
Connection.Response response = executeLoginRequest(loginRequest); | ||
|
||
if (response.statusCode() != 200) { | ||
throw new RuntimeException("서버 오류가 발생했습니다: HTTP " + response.statusCode()); | ||
} | ||
|
||
return parseUserProfile(response.parse()); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Error: " + e.getMessage()); | ||
} | ||
} | ||
|
||
private Connection.Response executeLoginRequest(SjLoginReq loginRequest) throws IOException { | ||
return Jsoup.connect(MOODLER_LOGIN_URL) | ||
.data("username", loginRequest.getUserId()) | ||
.data("password", loginRequest.getPassword()) | ||
.method(Connection.Method.POST) | ||
.execute(); | ||
} | ||
|
||
private SjUserProfile parseUserProfile(Document document) throws AuthenticationException { | ||
Element userInfo = document.selectFirst("div.user-info-picture"); | ||
if (userInfo == null) { | ||
throw new RuntimeException(AUTH_FAILED); | ||
} | ||
|
||
Element nameElement = document.selectFirst("h4"); | ||
Element majorElement = document.selectFirst("p.department"); | ||
|
||
if (nameElement == null || majorElement == null) { | ||
throw new RuntimeException(USER_INFO_MISSING); | ||
} | ||
|
||
return new SjUserProfile( | ||
nameElement.text().trim(), | ||
majorElement.text().trim() | ||
); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,22 +1,26 @@ | ||
package org.example.backend; | ||
|
||
import org.example.backend.users.dto.SjLoginReq; | ||
import org.example.backend.users.dto.SjUserProfile; | ||
import org.example.backend.users.service.SjAuthService; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.yj.sejongauth.controller.Sj; | ||
import org.yj.sejongauth.domain.SjProfile; | ||
|
||
|
||
@SpringBootTest | ||
public class SjTest { | ||
|
||
@Autowired | ||
protected Sj sj; | ||
SjAuthService sjAuthService; | ||
|
||
@Test | ||
@Disabled | ||
void SjTest(){ | ||
SjProfile sjProfile = sj.login("학번", "비번"); | ||
System.out.println(sjProfile); | ||
SjLoginReq test = new SjLoginReq("test", "test"); | ||
SjUserProfile sjProfile = sjAuthService.authenticate(test); | ||
System.out.println(sjProfile.getMajor()); | ||
System.out.println(sjProfile.getName()); | ||
} | ||
} |
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