Skip to content

Commit

Permalink
Merge pull request #283 from urinaner/feature/282
Browse files Browse the repository at this point in the history
[BE] 세종대 구성원(교수, 교직원, 학생) 통합 로그인 로직 구현
  • Loading branch information
2Jin1031 authored Jan 20, 2025
2 parents 9cab79f + 9052dc9 commit 7004512
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 10 deletions.
2 changes: 2 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ dependencies {
implementation 'ch.qos.logback:logback-classic'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2' //Swagger
implementation 'org.springframework.boot:spring-boot-starter-validation'
//jsoup
implementation 'org.jsoup:jsoup:1.14.3'

// security
implementation 'org.springframework.boot:spring-boot-starter-security'
Expand Down
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;
}
}
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;
}
}
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()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.example.backend.jwt.JWTUtil;
import org.example.backend.professor.controller.ProfessorController;
import org.example.backend.professor.service.ProfessorService;
import org.example.backend.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
Expand All @@ -35,11 +34,9 @@ public abstract class ControllerTestSupport {

@MockBean
protected ProfessorService professorService;
@MockBean
protected AdminService adminService;

@MockBean
protected UserService userService;
protected AdminService adminService;

@MockBean
protected JWTUtil jwtUtil;
Expand Down
14 changes: 9 additions & 5 deletions backend/src/test/java/org/example/backend/SjTest.java
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void saveProfessorWithoutName() {
ProfessorException exception = assertThrows(ProfessorException.class,
() -> professorService.saveProfessor(dto, null));

assertEquals(ProfessorExceptionType.REQUIRED_NAME, exception.exceptionType());
assertEquals(ProfessorExceptionType.NOT_FOUND_PROFESSOR, exception.exceptionType());
}

@Test
Expand Down

0 comments on commit 7004512

Please sign in to comment.