forked from cho-log/spring-learning-test
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSessionLoginController.java
More file actions
64 lines (54 loc) · 2.08 KB
/
SessionLoginController.java
File metadata and controls
64 lines (54 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package cholog.auth.ui;
import cholog.auth.application.AuthService;
import cholog.auth.application.AuthorizationException;
import cholog.auth.dto.MemberResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.http.HttpRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class SessionLoginController {
private static final String SESSION_KEY = "USER";
private static final String USERNAME_FIELD = "email";
private static final String PASSWORD_FIELD = "password";
private final AuthService authService;
public SessionLoginController(AuthService authService) {
this.authService = authService;
}
/**
* ex) request sample
* <p>
* POST /login/session HTTP/1.1
* content-type: application/x-www-form-urlencoded; charset=ISO-8859-1
* host: localhost:55477
* <p>
* email=email@email.com&password=1234
*/
@PostMapping("/login/session")
public ResponseEntity<Void> sessionLogin(HttpServletRequest request, HttpSession session) {
String email = request.getParameter("email");
String password = request.getParameter("password");
if (authService.checkInvalidLogin(email, password)) {
throw new AuthorizationException();
}
session.setAttribute(SESSION_KEY, email);
return ResponseEntity.ok().build();
}
/**
* ex) request sample
* <p>
* GET /members/me/session HTTP/1.1
* cookie: JSESSIONID=E7263AC9557EF658C888F02EEF840A19
* accept: application/json
*/
@GetMapping("/members/me/session")
public ResponseEntity<MemberResponse> findMyInfo(HttpSession session) {
String email = (String) session.getAttribute(SESSION_KEY);
MemberResponse member = authService.findMember(email);
return ResponseEntity.ok().body(member);
}
}