|
1 | 1 | package com.oronaminc.join.member.security; |
2 | 2 |
|
3 | 3 |
|
4 | | -import static com.oronaminc.join.member.util.MemberMapper.toSessionInfoResponse; |
5 | | - |
6 | | -import java.util.List; |
7 | | - |
8 | | -import org.springframework.http.HttpStatus; |
9 | | -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
10 | | -import org.springframework.security.core.Authentication; |
11 | | -import org.springframework.security.core.annotation.AuthenticationPrincipal; |
12 | | -import org.springframework.security.core.authority.SimpleGrantedAuthority; |
13 | | -import org.springframework.security.core.context.SecurityContext; |
14 | | -import org.springframework.security.core.context.SecurityContextHolder; |
15 | | -import org.springframework.security.web.context.HttpSessionSecurityContextRepository; |
16 | | -import org.springframework.web.bind.annotation.GetMapping; |
17 | | -import org.springframework.web.bind.annotation.PostMapping; |
18 | | -import org.springframework.web.bind.annotation.RequestBody; |
19 | | -import org.springframework.web.bind.annotation.RequestMapping; |
20 | | -import org.springframework.web.bind.annotation.ResponseStatus; |
21 | | -import org.springframework.web.bind.annotation.RestController; |
22 | | - |
23 | 4 | import com.oronaminc.join.member.dto.GuestLoginRequest; |
24 | | -import com.oronaminc.join.member.dto.GuestLoginResponse; |
25 | 5 | import com.oronaminc.join.member.dto.KakaoLoginRequest; |
26 | | -import com.oronaminc.join.member.dto.KakaoLoginResponse; |
27 | | -import com.oronaminc.join.member.dto.SessionInfoResponse; |
28 | | - |
| 6 | +import com.oronaminc.join.member.token.AuthTokenResponse; |
| 7 | +import com.oronaminc.join.member.token.JwtUtils; |
| 8 | +import com.oronaminc.join.member.token.LoginResponse; |
29 | 9 | import io.swagger.v3.oas.annotations.Operation; |
30 | 10 | import io.swagger.v3.oas.annotations.responses.ApiResponse; |
31 | 11 | import io.swagger.v3.oas.annotations.tags.Tag; |
|
34 | 14 | import jakarta.servlet.http.HttpServletResponse; |
35 | 15 | import jakarta.servlet.http.HttpSession; |
36 | 16 | import jakarta.validation.Valid; |
| 17 | +import java.util.Map; |
37 | 18 | import lombok.RequiredArgsConstructor; |
| 19 | +import org.springframework.http.HttpStatus; |
| 20 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 21 | +import org.springframework.web.bind.annotation.PostMapping; |
| 22 | +import org.springframework.web.bind.annotation.RequestBody; |
| 23 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 24 | +import org.springframework.web.bind.annotation.ResponseStatus; |
| 25 | +import org.springframework.web.bind.annotation.RestController; |
38 | 26 |
|
39 | 27 | @RestController |
40 | 28 | @RequestMapping("/api/auth") |
41 | 29 | @Tag(name = "Auth", description = "로그인 관련 API") |
42 | 30 | @RequiredArgsConstructor |
43 | 31 | public class AuthController { |
| 32 | + |
44 | 33 | private final AuthService authService; |
45 | 34 |
|
46 | 35 | @Operation( |
47 | | - summary = "카카오 로그인", |
48 | | - description = "redirect url 에 포함된 파라미터의 code와 state를 입력해주세요. 이후 모든 요청에 세션 인증이 적용됩니다." |
| 36 | + summary = "카카오 로그인" |
49 | 37 | ) |
50 | 38 | @PostMapping("/kakao") |
51 | 39 | @ResponseStatus(HttpStatus.OK) |
52 | | - public SessionInfoResponse kakaoLogin( |
53 | | - @RequestBody KakaoLoginRequest kakaoLoginRequest, |
54 | | - HttpServletRequest request |
| 40 | + public Map<String, AuthTokenResponse> kakaoLogin( |
| 41 | + @RequestBody KakaoLoginRequest kakaoLoginRequest, |
| 42 | + HttpServletResponse response |
55 | 43 | ) { |
56 | | - MemberDetails memberDetails = authService.kakaoLogin(kakaoLoginRequest.code()); |
| 44 | + LoginResponse loginResponse = authService.kakaoLogin(kakaoLoginRequest.code()); |
57 | 45 |
|
58 | | - UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( |
59 | | - memberDetails, null, List.of(new SimpleGrantedAuthority(memberDetails.getRole())) |
60 | | - ); |
| 46 | + String refreshToken = loginResponse.refreshToken(); |
61 | 47 |
|
62 | | - SecurityContext context = SecurityContextHolder.createEmptyContext(); |
63 | | - context.setAuthentication(authentication); |
64 | | - SecurityContextHolder.setContext(context); |
| 48 | + JwtUtils.addRefreshTokenCookie(response, refreshToken, |
| 49 | + loginResponse.refreshTokenExpiresIn()); |
65 | 50 |
|
66 | | - SecurityContextHolder.getContext().setAuthentication(authentication); |
67 | | - |
68 | | - request.getSession(true).setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context); |
69 | | - |
70 | | - return toSessionInfoResponse(memberDetails); |
| 51 | + return Map.of("token", loginResponse.authTokenResponse()); |
71 | 52 | } |
72 | 53 |
|
73 | 54 | @Operation( |
74 | 55 | summary = "비회원 로그인", |
75 | | - description = "닉네임을 입력하면 비회원 세션이 생성되고 인증이 설정됩니다. 이후 모든 요청에 세션 인증이 적용됩니다.", |
76 | 56 | responses = { |
77 | 57 | @ApiResponse(responseCode = "201", description = "비회원 로그인 성공"), |
78 | 58 | @ApiResponse(responseCode = "400", description = "닉네임 누락 또는 유효성 검증 실패") |
79 | 59 | } |
80 | 60 | ) |
81 | 61 | @PostMapping("/guest") |
82 | 62 | @ResponseStatus(HttpStatus.CREATED) |
83 | | - public SessionInfoResponse guestLogin(@RequestBody @Valid GuestLoginRequest guestLoginRequest, HttpServletRequest request) { |
84 | | - MemberDetails guest = authService.loadGuest(guestLoginRequest); |
85 | | - |
86 | | - Authentication authentication = new UsernamePasswordAuthenticationToken( |
87 | | - guest, null, List.of(new SimpleGrantedAuthority(guest.getRole())) |
88 | | - ); |
89 | | - |
90 | | - SecurityContext context = SecurityContextHolder.createEmptyContext(); |
91 | | - context.setAuthentication(authentication); |
92 | | - SecurityContextHolder.setContext(context); |
| 63 | + public Map<String, AuthTokenResponse> guestLogin( |
| 64 | + @RequestBody @Valid GuestLoginRequest guestLoginRequest, |
| 65 | + HttpServletResponse response) { |
| 66 | + LoginResponse loginResponse = authService.loadGuest(guestLoginRequest); |
93 | 67 |
|
94 | | - request.getSession(true).setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context); |
| 68 | + String refreshToken = loginResponse.refreshToken(); |
95 | 69 |
|
96 | | - return toSessionInfoResponse(guest); |
97 | | - } |
98 | | - |
99 | | - @Operation( |
100 | | - summary = "현재 세션 사용자 정보 조회", |
101 | | - description = "로그인한 사용자의 세션 정보를 반환합니다. 로그인하지 않은 경우 403 또는 401이 발생합니다.", |
102 | | - responses = { |
103 | | - @ApiResponse(responseCode = "200", description = "세션 사용자 정보 조회 성공"), |
104 | | - @ApiResponse(responseCode = "401", description = "로그인되지 않은 사용자"), |
105 | | - @ApiResponse(responseCode = "403", description = "인증된 사용자 아님") |
106 | | - } |
107 | | - ) |
108 | | - @GetMapping("/session") |
109 | | - @ResponseStatus(HttpStatus.OK) |
110 | | - public SessionInfoResponse getSessionInfo(@AuthenticationPrincipal MemberDetails memberDetails) { |
| 70 | + JwtUtils.addRefreshTokenCookie(response, refreshToken, |
| 71 | + loginResponse.refreshTokenExpiresIn()); |
111 | 72 |
|
112 | | - return toSessionInfoResponse(memberDetails); |
| 73 | + return Map.of("token", loginResponse.authTokenResponse()); |
113 | 74 | } |
114 | 75 |
|
115 | 76 | @Operation( |
|
0 commit comments