-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathLoginController.java
37 lines (28 loc) · 1.28 KB
/
LoginController.java
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
package nextstep.app.ui;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import nextstep.app.domain.Member;
import nextstep.app.domain.MemberRepository;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class LoginController {
public static final String SPRING_SECURITY_CONTEXT_KEY = "SPRING_SECURITY_CONTEXT";
private final MemberRepository memberRepository;
public LoginController(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
@PostMapping("/login")
public ResponseEntity<Void> login(HttpServletRequest request, HttpSession session) {
Map<String, String[]> parameterMap = request.getParameterMap();
String username = parameterMap.get("username")[0];
String password = parameterMap.get("password")[0];
Member member = memberRepository.findByEmail(username)
.filter(it -> it.matchPassword(password))
.orElseThrow(AuthenticationException::new);
session.setAttribute(SPRING_SECURITY_CONTEXT_KEY, member);
return ResponseEntity.ok().build();
}
}