-
Notifications
You must be signed in to change notification settings - Fork 1
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 #27 from CSID-DGU/backend/feature/jwt
- Loading branch information
Showing
16 changed files
with
381 additions
and
68 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
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
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
44 changes: 44 additions & 0 deletions
44
src/backend/Eyesee/src/main/java/com/fortune/eyesee/controller/SessionController.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,44 @@ | ||
package com.fortune.eyesee.controller; | ||
|
||
import com.fortune.eyesee.common.response.BaseResponse; | ||
import com.fortune.eyesee.dto.ExamCodeRequestDTO; | ||
import com.fortune.eyesee.dto.ExamResponseDTO; | ||
import com.fortune.eyesee.dto.TokenResponseDTO; | ||
import com.fortune.eyesee.dto.UserInfoRequestDTO; | ||
import com.fortune.eyesee.service.ExamService; | ||
import com.fortune.eyesee.service.SessionService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/api/sessions") | ||
public class SessionController { | ||
|
||
private final SessionService sessionService; | ||
|
||
@Autowired | ||
public SessionController(SessionService sessionService) { | ||
this.sessionService = sessionService; | ||
} | ||
|
||
@Autowired | ||
private ExamService examService; | ||
|
||
// 시험 세션 입장 | ||
@PostMapping("/join") | ||
public ResponseEntity<BaseResponse<ExamResponseDTO>> joinExam(@RequestBody ExamCodeRequestDTO examCodeRequestDTO) { | ||
// examCode로 시험 정보 조회 | ||
ExamResponseDTO examResponseDTO = examService.getExamByCode(examCodeRequestDTO.getExamCode()); | ||
return ResponseEntity.ok(new BaseResponse<>(examResponseDTO, "시험 세션 입장 성공")); | ||
|
||
} | ||
|
||
// 학생 정보 입력 | ||
@PostMapping("/student") | ||
public ResponseEntity<BaseResponse<TokenResponseDTO>> addUserInfo(@RequestBody UserInfoRequestDTO userInfoRequestDTO) { | ||
TokenResponseDTO tokenResponseDTO = sessionService.addUserInfo(userInfoRequestDTO); | ||
return ResponseEntity.ok(new BaseResponse<>(tokenResponseDTO, "사용자 정보 입력 성공")); | ||
|
||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/backend/Eyesee/src/main/java/com/fortune/eyesee/dto/TokenResponseDTO.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 com.fortune.eyesee.dto; | ||
|
||
public class TokenResponseDTO { | ||
private String access_token; | ||
private String refresh_token; | ||
|
||
public TokenResponseDTO(String accessToken, String refreshToken) { | ||
this.access_token = accessToken; | ||
this.refresh_token = refreshToken; | ||
} | ||
|
||
public String getAccess_token() { | ||
return access_token; | ||
} | ||
|
||
public String getRefresh_token() { | ||
return refresh_token; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/backend/Eyesee/src/main/java/com/fortune/eyesee/dto/UserInfoRequestDTO.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,13 @@ | ||
|
||
package com.fortune.eyesee.dto; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class UserInfoRequestDTO { | ||
private String examCode; // 시험 코드 | ||
private String name; // 사용자 이름 | ||
private String department; // 학과 | ||
private Integer userNum; // 학번 | ||
private Integer seatNum; // 좌석 번호 | ||
} |
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
14 changes: 14 additions & 0 deletions
14
src/backend/Eyesee/src/main/java/com/fortune/eyesee/entity/UserId.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,14 @@ | ||
package com.fortune.eyesee.entity; | ||
|
||
import java.io.Serializable; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.AllArgsConstructor; | ||
import lombok.NoArgsConstructor; | ||
|
||
@EqualsAndHashCode | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class UserId implements Serializable { | ||
private Integer sessionId; // 세션 ID | ||
private Integer userNum; // 학번 | ||
} |
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
47 changes: 47 additions & 0 deletions
47
src/backend/Eyesee/src/main/java/com/fortune/eyesee/security/JwtAuthenticationFilter.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,47 @@ | ||
package com.fortune.eyesee.security; | ||
|
||
import com.fortune.eyesee.utils.JwtUtil; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
|
||
public class JwtAuthenticationFilter extends OncePerRequestFilter { | ||
|
||
private final JwtUtil jwtUtil; | ||
|
||
public JwtAuthenticationFilter(JwtUtil jwtUtil) { | ||
this.jwtUtil = jwtUtil; | ||
} | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) | ||
throws ServletException, IOException { | ||
String token = getJwtFromRequest(request); | ||
|
||
if (token != null && jwtUtil.validateToken(token)) { | ||
Integer id = jwtUtil.getAdminIdFromToken(token); | ||
|
||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( | ||
id, null, null); | ||
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); | ||
|
||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} | ||
chain.doFilter(request, response); | ||
} | ||
|
||
private String getJwtFromRequest(HttpServletRequest request) { | ||
String bearerToken = request.getHeader("Authorization"); | ||
if (bearerToken != null && bearerToken.startsWith("Bearer ")) { | ||
return bearerToken.substring(7); | ||
} | ||
return null; | ||
} | ||
} |
Oops, something went wrong.